Sprankelprachtig aan/afmeldsysteem

authentication_controller.rb 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. class AuthenticationController < ApplicationController
  2. before_action :require_login!, only: [:logout_confirm, :logout]
  3. def login_form
  4. render layout: 'void'
  5. end
  6. def login
  7. if params[:session][:email].blank? || params[:session][:password].blank?
  8. flash[:warning] = "You forgot to add value"
  9. redirect_to action: 'login_form'
  10. else
  11. u = User.find_by(email: params[:session][:email])
  12. if u && u.authenticate(params[:session][:password])
  13. log_in(u, params[:session][:remember_me].to_i)
  14. flash[:success] = "Hello, #{u.person.full_name}!"
  15. redirect_to root_path
  16. else
  17. flash[:danger] = "Invalid username/password combination!"
  18. redirect_to action: 'login_form'
  19. end
  20. end
  21. end
  22. def logout_confirm
  23. render layout: 'void'
  24. end
  25. def logout
  26. log_out
  27. redirect_to login_path
  28. end
  29. def create_password_form
  30. render layout: 'void'
  31. end
  32. def login_status
  33. render text: is_logged_in?
  34. end
  35. def create_password
  36. flash[:danger] = "Not yet implemented."
  37. redirect_to action: 'login'
  38. end
  39. def forgotten_password_form
  40. render layout: 'void'
  41. end
  42. def forgotten_password
  43. flash[:danger] = "Not yet implemented."
  44. redirect_to action: 'login'
  45. end
  46. def reset_password_form
  47. token = Token.find_by(token: params[:token], tokentype: Token::TYPES[:password_reset])
  48. if not password_reset_token_valid? token
  49. return
  50. end
  51. render layout: 'void'
  52. end
  53. def reset_password
  54. token = Token.find_by(token: params[:token], tokentype: Token::TYPES[:password_reset])
  55. if not password_reset_token_valid? token
  56. return
  57. end
  58. if not params[:password] == params[:password_confirmation]
  59. flash[:warning] = "Password confirmation does not match your password!"
  60. redirect_to action: 'reset_password_form', token: params[:token]
  61. return
  62. end
  63. user = token.user
  64. user.password = params[:password_reset][:password]
  65. user.password_confirmation = params[:password_reset][:password_confirmation]
  66. user.save!
  67. token.destroy!
  68. flash[:success] = "Your password has been reset, you may now log in."
  69. redirect_to action: 'login'
  70. end
  71. private
  72. def session_params
  73. params.require(:session).permit(:email, :password, :remember_me)
  74. end
  75. def password_reset_token_valid?(token)
  76. if token.nil?
  77. flash[:warning] = "No valid token specified!"
  78. redirect_to action: 'login'
  79. return false
  80. end
  81. if token.expires and token.expires < DateTime.now
  82. flash[:warning] = "That token has expired, please request a new one."
  83. redirect_to action: 'login'
  84. return false
  85. end
  86. true
  87. end
  88. end