Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

finishing oauth for sinatra

  • Loading branch information...
commit ff4eb489c9e3c58a76db96afa059f11d79197167 1 parent 9175746
@NoamB authored
Showing with 14 additions and 8 deletions.
  1. BIN  dummy
  2. +12 −6 myapp.rb
  3. +2 −2 views/layout.erb
View
BIN  dummy
Binary file not shown
View
18 myapp.rb
@@ -182,16 +182,22 @@ def not_authenticated
auth_at_provider(params[:provider])
end
-get '/oauth/:provider/callback' do
+get '/oauth/callback' do
provider = params[:provider]
@user = login_from_access_token(provider)
- unless @user
+ if @user
+ session[:notice] = "Success!"
+ redirect '/'
+ else
if @user = create_from_provider!(provider)
- erb "Success!"
+ @user.activate!
+ session.clear # protect from session fixation attack
+ login_user(@user)
+ session[:notice] = "User created!"
+ redirect '/'
else
- erb "Failed!"
+ session[:alert] = "Failed!"
+ redirect '/'
end
- else
-
end
end
View
4 views/layout.erb
@@ -13,8 +13,8 @@
<a href=<%= url('/users/new') %>>Register</a> |
<a href=<%= url('/login') %>>Login</a> |
<a href=<%= url('/login/http') %>>Login from HTTP</a> |
- <a href=<%= url('/login/twitter') %>>Login with Twitter</a> |
- <a href=<%= url('/login/facebook') %>>Login with Facebook</a>
+ <a href=<%= url('/auth_at_provider?provider=twitter') %>>Login with Twitter</a> |
+ <a href=<%= url('/auth_at_provider?provider=facebook') %>>Login with Facebook</a>
<% end %>
</div>
<% if current_user %>
Please sign in to comment.
Something went wrong with that request. Please try again.