Advertisement
Guest User

Untitled

a guest
Aug 19th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. class SessionsController < ApplicationController
  2. def new
  3. end
  4.  
  5. def create
  6. @list = List.new
  7. @lists = List.all
  8. if auth
  9. #log in via OAuth
  10. # my github email is nil, but I had a nickname
  11. # facebook has name, not nickname -- in case an email was nil
  12. oauth_email = auth["info"]["email"] || auth["info"]["nickname"] || auth["info"]["name"]
  13. #existing user
  14. if user = User.find_by(:email => oauth_email)
  15. session[:user_id] = user.id
  16. @auth = auth
  17. render '/lists/index'
  18. else
  19. #new user
  20. user = User.new(:email => oauth_email, :password => SecureRandom.hex)
  21. if user.save
  22. session[:user_id] = user.id
  23. @auth = auth
  24. render '/lists/index'
  25. else
  26. redirect_to '/login'
  27. end
  28. end
  29.  
  30. else
  31. #Normal login with username and password
  32. user = User.find_or_create_by(:email => params[:email])
  33. if user && user.authenticate(params[:password])
  34. session[:user_id] = user.id
  35. redirect_to root_path
  36. else
  37. render 'sessions/new'
  38. end
  39. end
  40. end
  41.  
  42. def auth
  43. request.env['omniauth.auth']
  44. end
  45.  
  46. def destroy
  47. reset_session
  48. redirect_to login_path
  49. end
  50. helper_method :auth
  51. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement