Sprankelprachtig aan/afmeldsysteem

authentication_controller.rb 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. user = User.find_by(email: params[:password_reset][:email])
  44. if not user
  45. flash[:danger] = "That email address is not associated with any user."
  46. redirect_to action: 'forgotten_password_form'
  47. return
  48. end
  49. AuthenticationMailer::password_reset_email(user).deliver_later
  50. flash[:success] = "An email has been sent, check your inbox!"
  51. redirect_to action: 'login'
  52. end
  53. def reset_password_form
  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. render layout: 'void'
  59. end
  60. def reset_password
  61. token = Token.find_by(token: params[:token], tokentype: Token::TYPES[:password_reset])
  62. if not password_reset_token_valid? token
  63. return
  64. end
  65. if not params[:password] == params[:password_confirmation]
  66. flash[:warning] = "Password confirmation does not match your password!"
  67. redirect_to action: 'reset_password_form', token: params[:token]
  68. return
  69. end
  70. user = token.user
  71. user.password = params[:password_reset][:password]
  72. user.password_confirmation = params[:password_reset][:password_confirmation]
  73. user.save!
  74. token.destroy!
  75. flash[:success] = "Your password has been reset, you may now log in."
  76. redirect_to action: 'login'
  77. end
  78. private
  79. def session_params
  80. params.require(:session).permit(:email, :password, :remember_me)
  81. end
  82. def password_reset_token_valid?(token)
  83. if token.nil?
  84. flash[:warning] = "No valid token specified!"
  85. redirect_to action: 'login'
  86. return false
  87. end
  88. if token.expires and token.expires < DateTime.now
  89. flash[:warning] = "That token has expired, please request a new one."
  90. redirect_to action: 'login'
  91. return false
  92. end
  93. true
  94. end
  95. end