Переглянути джерело

Basic password reset setup

master
Adrian Short 12 роки тому
джерело
коміт
0a34ef0c8d
17 змінених файлів з 151 додано та 3 видалено
  1. +3
    -0
      app/assets/javascripts/password_resets.js.coffee
  2. +3
    -0
      app/assets/stylesheets/password_resets.css.scss
  3. +25
    -0
      app/controllers/password_resets_controller.rb
  4. +2
    -0
      app/helpers/password_resets_helper.rb
  5. +14
    -0
      app/mailers/user_mailer.rb
  6. +2
    -0
      app/views/password_resets/create.html.haml
  7. +32
    -0
      app/views/password_resets/edit.html.haml
  8. +2
    -0
      app/views/password_resets/update.html.haml
  9. +7
    -0
      app/views/sessions/_forgot_password_form.html.haml
  10. +5
    -1
      app/views/sessions/new.html.haml
  11. +10
    -0
      app/views/user_mailer/reset_password_email.text.haml
  12. +8
    -0
      config/environments/development.rb
  13. +2
    -2
      config/initializers/sorcery.rb
  14. +1
    -0
      config/routes.rb
  15. +19
    -0
      test/functional/password_resets_controller_test.rb
  16. +12
    -0
      test/functional/user_mailer_test.rb
  17. +4
    -0
      test/unit/helpers/password_resets_helper_test.rb

+ 3
- 0
app/assets/javascripts/password_resets.js.coffee Переглянути файл

@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

+ 3
- 0
app/assets/stylesheets/password_resets.css.scss Переглянути файл

@@ -0,0 +1,3 @@
// Place all the styles related to the PasswordResets controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

+ 25
- 0
app/controllers/password_resets_controller.rb Переглянути файл

@@ -0,0 +1,25 @@
class PasswordResetsController < ApplicationController
def create
@user = User.find_by_email(params[:email])
@user.deliver_reset_password_instructions! if @user
redirect_to(root_path, :notice => "Instructions have been sent to your email.")
end

def edit
@user = User.load_from_reset_password_token(params[:id])
@token = params[:id]
not_authenticated unless @user
end

def update
@token = params[:token]
@user = User.load_from_reset_password_token(params[:token])
not_authenticated unless @user
@user.password_confirmation = params[:user][:password_confirmation]
if @user.change_password!(params[:user][:password])
redirect_to(root_path, :notice => "Password changed OK")
else
render :action => "edit"
end
end
end

+ 2
- 0
app/helpers/password_resets_helper.rb Переглянути файл

@@ -0,0 +1,2 @@
module PasswordResetsHelper
end

+ 14
- 0
app/mailers/user_mailer.rb Переглянути файл

@@ -0,0 +1,14 @@
class UserMailer < ActionMailer::Base
default from: "from@example.com"

# Subject can be set in your I18n file at config/locales/en.yml
# with the following lookup:
#
# en.user_mailer.reset_password_email.subject
#
def reset_password_email(user)
@user = user
@url = "http://localhost:3000/password_resets/#{user.reset_password_token}/edit"
mail(:to => user.email, :subject => "Your password has been reset")
end
end

+ 2
- 0
app/views/password_resets/create.html.haml Переглянути файл

@@ -0,0 +1,2 @@
%h1 PasswordResets#create
%p Find me in app/views/password_resets/create.html.haml

+ 32
- 0
app/views/password_resets/edit.html.haml Переглянути файл

@@ -0,0 +1,32 @@
%h1 Choose a new password

= form_for @user, :url => password_reset_path(@user), :html => { :method => :put } do |f|
- if @user.errors.any?
#error_explanation
%h2
= pluralize(@user.errors.count, "error")
prohibited this user from being saved:
%ul
- @user.errors.full_messages.each do |msg|
%li= msg
.field
= f.label :email
%br
= @user.email
.field
= f.label :password
%br
= f.password_field :password
.field
= f.label :password_confirmation
%br
= f.password_field :password_confirmation
= hidden_field_tag :token, @token
.actions
= f.submit

+ 2
- 0
app/views/password_resets/update.html.haml Переглянути файл

@@ -0,0 +1,2 @@
%h1 PasswordResets#update
%p Find me in app/views/password_resets/update.html.haml

+ 7
- 0
app/views/sessions/_forgot_password_form.html.haml Переглянути файл

@@ -0,0 +1,7 @@
= form_tag password_resets_path, :method => :post do
.field
= label_tag :email
%br
= text_field_tag :email
= submit_tag "Reset my password"

+ 5
- 1
app/views/sessions/new.html.haml Переглянути файл

@@ -16,4 +16,8 @@
= label_tag :remember_me

.actions
= submit_tag "Log in"
= submit_tag "Log in"
%h1 Forgotten your password?

= render "forgot_password_form"

+ 10
- 0
app/views/user_mailer/reset_password_email.text.haml Переглянути файл

@@ -0,0 +1,10 @@
Hello,
= @user.email

You have requested to reset your password.

To choose a new password, just follow this link:

= @url

Have a great day!

+ 8
- 0
config/environments/development.rb Переглянути файл

@@ -28,4 +28,12 @@ Apollo::Application.configure do

# Expands the lines which load the assets
config.assets.debug = true
config.action_mailer.delivery_method = :smtp

ActionMailer::Base.smtp_settings = {
:address => "localhost",
:port => 1025,
:domain => "localhost:3000"
}
end

+ 2
- 2
config/initializers/sorcery.rb Переглянути файл

@@ -3,7 +3,7 @@
# Available submodules are: :user_activation, :http_basic_auth, :remember_me,
# :reset_password, :session_timeout, :brute_force_protection, :activity_logging, :external
# Rails.application.config.sorcery.submodules = [:remember_me, :reset_password]
Rails.application.config.sorcery.submodules = [:remember_me]
Rails.application.config.sorcery.submodules = [:remember_me, :reset_password]

# Here you can configure each submodule's features.
Rails.application.config.sorcery.configure do |config|
@@ -269,7 +269,7 @@ Rails.application.config.sorcery.configure do |config|
# mailer class. Needed.
# Default: `nil`
#
# user.reset_password_mailer =
user.reset_password_mailer = UserMailer


# reset password email method on your mailer class.


+ 1
- 0
config/routes.rb Переглянути файл

@@ -4,6 +4,7 @@ Apollo::Application.routes.draw do

resources :users
resources :sessions
resources :password_resets
get "posts/near"



+ 19
- 0
test/functional/password_resets_controller_test.rb Переглянути файл

@@ -0,0 +1,19 @@
require 'test_helper'

class PasswordResetsControllerTest < ActionController::TestCase
test "should get create" do
get :create
assert_response :success
end

test "should get edit" do
get :edit
assert_response :success
end

test "should get update" do
get :update
assert_response :success
end

end

+ 12
- 0
test/functional/user_mailer_test.rb Переглянути файл

@@ -0,0 +1,12 @@
require 'test_helper'

class UserMailerTest < ActionMailer::TestCase
test "reset_password_email" do
mail = UserMailer.reset_password_email
assert_equal "Reset password email", mail.subject
assert_equal ["to@example.org"], mail.to
assert_equal ["from@example.com"], mail.from
assert_match "Hi", mail.body.encoded
end

end

+ 4
- 0
test/unit/helpers/password_resets_helper_test.rb Переглянути файл

@@ -0,0 +1,4 @@
require 'test_helper'

class PasswordResetsHelperTest < ActionView::TestCase
end

Завантаження…
Відмінити
Зберегти