Browse Source

Working password reset emails

Maarten van den Berg 8 years ago
parent
commit
04e8884a3c

+ 2 - 0
.rbenv-vars-sample

@@ -2,3 +2,5 @@ MAILGUN_DOMAIN=
2 2
 MAILGUN_API_KEY=
3 3
 
4 4
 MAIL_FROM_ADDRESS=
5
+
6
+AARDBEI_HOSTNAME=

+ 8 - 1
app/controllers/authentication_controller.rb

@@ -50,7 +50,14 @@ class AuthenticationController < ApplicationController
50 50
   end
51 51
 
52 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 61
     redirect_to action: 'login'
55 62
   end
56 63
 

+ 2 - 4
app/models/token.rb

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

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

@@ -1,9 +1,11 @@
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 11
 Aardbei

+ 1 - 0
config/application.rb

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