ソースを参照

Working password reset emails

Maarten van den Berg 8 年 前
コミット
04e8884a3c
共有5 個のファイルを変更した20 個の追加10 個の削除を含む
  1. 2 0
      .rbenv-vars-sample
  2. 8 1
      app/controllers/authentication_controller.rb
  3. 2 4
      app/models/token.rb
  4. 7 5
      app/views/authentication_mailer/password_reset_email.text.erb
  5. 1 0
      config/application.rb

+ 2 - 0
.rbenv-vars-sample

2
 MAILGUN_API_KEY=
2
 MAILGUN_API_KEY=
3
 
3
 
4
 MAIL_FROM_ADDRESS=
4
 MAIL_FROM_ADDRESS=
5
+
6
+AARDBEI_HOSTNAME=

+ 8 - 1
app/controllers/authentication_controller.rb

50
   end
50
   end
51
 
51
 
52
   def forgotten_password
52
   def forgotten_password
53
-    flash[:danger] = "Not yet implemented."
53
+    user = User.find_by(email: params[:password_reset][:email])
54
+    if not user
55
+      flash[:danger] = "That email address is not associated with any user."
56
+      redirect_to action: 'forgotten_password_form'
57
+      return
58
+    end
59
+    AuthenticationMailer::password_reset_email(user).deliver_later
60
+    flash[:success] = "An email has been sent, check your inbox!"
54
     redirect_to action: 'login'
61
     redirect_to action: 'login'
55
   end
62
   end
56
 
63
 

+ 2 - 4
app/models/token.rb

54
   def generate_expiry
54
   def generate_expiry
55
     case self.tokentype
55
     case self.tokentype
56
     when TYPES[:password_reset]
56
     when TYPES[:password_reset]
57
-      1.days.since
57
+      self.expires = 1.days.since
58
     when TYPES[:account_confirmation]
58
     when TYPES[:account_confirmation]
59
-      7.days.since
60
-    else
61
-      nil
59
+      self.expires = 7.days.since
62
     end
60
     end
63
   end
61
   end
64
 end
62
 end

+ 7 - 5
app/views/authentication_mailer/password_reset_email.text.erb

1
-Hallo <%= @user.person.first_name %>,
1
+Hello <%= @user.person.first_name %>,
2
 
2
 
3
-Er is een verzoek binnengekomen om je wachtwoord opnieuw in te stellen.
4
-Als jij dit hebt gedaan, open dan deze link:
3
+You (or someone pretending to be you) have requested to reset your password.
4
+If this was you, please open the following link:
5
 
5
 
6
-Als je dit niet was kan je deze mail negeren.
6
+<%= reset_password_url(token: @token.token) %>
7
+
8
+This link will expire at <%= @token.expires %>, and is usable only once.
9
+If you did not request this password reset, ignore this message.
7
 
10
 
8
-Doei,
9
 Aardbei
11
 Aardbei

+ 1 - 0
config/application.rb

11
     # Settings in config/environments/* take precedence over those specified here.
11
     # Settings in config/environments/* take precedence over those specified here.
12
     # Application configuration should go into files in config/initializers
12
     # Application configuration should go into files in config/initializers
13
     # -- all .rb files in that directory are automatically loaded.
13
     # -- all .rb files in that directory are automatically loaded.
14
+    default_url_options[:host] = ENV['AARDBEI_HOSTNAME']
14
   end
15
   end
15
 end
16
 end