Browse Source

Add password resetting

Maarten van den Berg 8 years ago
parent
commit
467aeb8d32

+ 45 - 0
app/controllers/authentication_controller.rb

@@ -54,8 +54,53 @@ class AuthenticationController < ApplicationController
54 54
     redirect_to action: 'login'
55 55
   end
56 56
 
57
+  def reset_password_form
58
+    token = Token.find_by(token: params[:token], tokentype: Token::TYPES[:password_reset])
59
+    if not password_reset_token_valid? token
60
+      return
61
+    end
62
+    render layout: 'void'
63
+  end
64
+
65
+  def reset_password
66
+    token = Token.find_by(token: params[:token], tokentype: Token::TYPES[:password_reset])
67
+    if not password_reset_token_valid? token
68
+      return
69
+    end
70
+
71
+    if not params[:password] == params[:password_confirmation]
72
+      flash[:warning] = "Password confirmation does not match your password!"
73
+      redirect_to action: 'reset_password_form', token: params[:token]
74
+      return
75
+    end
76
+
77
+    user = token.user
78
+    user.password = params[:password_reset][:password]
79
+    user.password_confirmation = params[:password_reset][:password_confirmation]
80
+    user.save!
81
+
82
+    token.destroy!
83
+
84
+    flash[:success] = "Your password has been reset, you may now log in."
85
+    redirect_to action: 'login'
86
+  end
87
+
57 88
   private
58 89
   def session_params
59 90
     params.require(:session).permit(:email, :password, :remember_me)
60 91
   end
92
+
93
+  def password_reset_token_valid?(token)
94
+    if token.nil?
95
+      flash[:warning] = "No valid token specified!"
96
+      redirect_to action: 'login'
97
+      return false
98
+    end
99
+    if token.expires and token.expires < DateTime.now
100
+      flash[:warning] = "That token has expired, please request a new one."
101
+      redirect_to action: 'login'
102
+      return false
103
+    end
104
+    true
105
+  end
61 106
 end

+ 18 - 0
app/views/authentication/reset_password_form.html.haml

@@ -0,0 +1,18 @@
1
+- content_for :title do
2
+  Reset password
3
+.container
4
+  = render 'shared/alerts'
5
+  = form_for :password_reset, url: {action: 'reset_password', token: params[:token]}, html: { class: 'central-form'} do |f|
6
+    %h2.central-form-header.text-center
7
+      Reset password
8
+
9
+    = f.password_field :password, placeholder: "New password", class: 'form-control input-top'
10
+    = f.password_field :password_confirmation, placeholder: "Confirm new password", class: 'form-control input-bottom'
11
+    = f.submit "Reset password", class: 'btn btn-primary btn-lg btn-block'
12
+
13
+  .central-form
14
+    %ul.hdis
15
+      %li
16
+        = link_to "Login",          {action: 'login',           controller: 'authentication'}, {class: 'btn btn-secondary'}
17
+      %li
18
+        = link_to "Create account", {action: 'create_password', controller: 'authentication'}, {class: 'btn btn-secondary'}

+ 3 - 0
config/routes.rb

@@ -12,6 +12,9 @@ Rails.application.routes.draw do
12 12
   get  'forgot', to: 'authentication#forgotten_password_form'
13 13
   post 'forgot', to: 'authentication#forgotten_password'
14 14
 
15
+  get  'reset_password', to: 'authentication#reset_password_form'
16
+  post 'reset_password', to: 'authentication#reset_password'
17
+
15 18
   get 'logout', to: 'authentication#logout_confirm'
16 19
   delete 'logout', to: 'authentication#logout'
17 20