-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1b350c3
commit 9525df3
Showing
32 changed files
with
288 additions
and
90 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
class PasswordResetsController < ApplicationController | ||
before_action :get_user, only: [ :edit, :update ] | ||
before_action :valid_user, only: [ :edit, :update ] | ||
before_action :check_expiration, only: [ :edit, :update ] | ||
|
||
def new | ||
end | ||
|
||
def update | ||
if params[:user][:password].empty? | ||
@user.errors.add(:password, "can't be empty") | ||
render "edit", status: :unprocessable_entity | ||
elsif @user.update(user_params) | ||
reset_session | ||
log_in @user | ||
@user.update_attribute(:reset_digest, nil) | ||
flash[:success] = "Password has been reset." | ||
redirect_to @user | ||
else | ||
render "edit", status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def create | ||
@user = User.find_by(email: params[:password_reset][:email].downcase) | ||
if @user | ||
@user.create_reset_digest | ||
@user.send_password_reset_email | ||
flash[:info] = "Email sent with password reset instructions" | ||
redirect_to root_url | ||
else | ||
flash.now[:danger] = "Email address not found" | ||
render "new", status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def edit | ||
end | ||
|
||
private | ||
|
||
def get_user | ||
@user = User.find_by(email: params[:email]) | ||
end | ||
|
||
def user_params | ||
params.require(:user).permit(:password, :password_confirmation) | ||
end | ||
|
||
# Confirms a valid user. | ||
def valid_user | ||
unless @user && @user.activated? && | ||
@user.authenticated?(:reset, params[:id]) | ||
redirect_to root_url | ||
end | ||
end | ||
|
||
def check_expiration | ||
if @user.password_reset_expired? | ||
flash[:danger] = "Password reset has expired." | ||
redirect_to new_password_reset_path | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module PasswordResetHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# app/mailers/application_mailer.rb | ||
|
||
class ApplicationMailer < ActionMailer::Base | ||
default from: Rails.env.test? ? "noreply@example.com" : ENV["FROM_MAIL_ADDRESS"] | ||
default from: (Rails.env.test? || Rails.env.development?) ? "noreply@example.com" : ENV["FROM_MAIL_ADDRESS"] | ||
layout "mailer" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<% provide(:title, 'Reset password') %> | ||
<h1>Reset password</h1> | ||
|
||
<div class="row"> | ||
<div class="col-md-6 col-md-offset-3"> | ||
<%= form_with(model: @user, url: password_reset_path(params[:id])) do |f| %> | ||
<%= render 'shared/error_messages' %> | ||
<%= hidden_field_tag :email, @user.email %> | ||
<%= f.label :password %> | ||
<%= f.password_field :password, class: 'form-control' %> | ||
<%= f.label :password_confirmation, "Confirmation" %> | ||
<%= f.password_field :password_confirmation, class: 'form-control' %> | ||
<%= f.submit "Update password", class: "btn btn-primary" %> | ||
<% end %> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<% provide(:title, "Forgot password") %> | ||
<h1>Forgot password</h1> | ||
|
||
<div class="row"> | ||
<div class="col-md-6 col-md-offset-3"> | ||
<%= form_with(url: password_resets_path, scope: :password_reset) do |f| %> | ||
<%= f.label :email %> | ||
<%= f.email_field :email, class: 'form-control' %> | ||
<%= f.submit "Submit", class: "btn btn-primary" %> | ||
<% end %> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,13 @@ | ||
<h1>User#password_reset</h1> | ||
<h1>Password reset</h1> | ||
|
||
<p>To reset your password click the link below:</p> | ||
|
||
<%= link_to "Reset password", edit_password_reset_url(@user.reset_token, | ||
email: @user.email) %> | ||
|
||
<p>This link will expire in two hours.</p> | ||
|
||
<p> | ||
<%= @greeting %>, find me in app/views/user_mailer/password_reset.html.erb | ||
</p> | ||
If you did not request your password to be reset, please ignore this email and | ||
your password will stay as it is. | ||
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
User#password_reset | ||
To reset your password click on the link below: | ||
|
||
<%= @greeting %>, find me in app/views/user_mailer/password_reset.text.erb | ||
<%= edit_password_reset_url(@user.reset_token, email: @user.email) %> | ||
|
||
If you did not request your password to be reset, please ignore this email and | ||
your password will stay as it is. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,25 @@ | ||
Rails.application.routes.draw do | ||
get "password_resets/new" | ||
get "password_resets/edit" | ||
get "account_activations/edit" | ||
get "sessions/new" | ||
root "static_pages#home" | ||
# Pages | ||
get "help", to: "static_pages#help" | ||
get "about", to: "static_pages#about" | ||
get "contact", to: "static_pages#contact" | ||
get "help", to: "static_pages#help" | ||
get "about", to: "static_pages#about" | ||
get "contact", to: "static_pages#contact" | ||
|
||
# Signup | ||
get "signup", to: "users#new" | ||
get "signup", to: "users#new" | ||
|
||
# Sessions | ||
get "/login", to: "sessions#new" | ||
post "/login", to: "sessions#create" | ||
delete "/logout", to: "sessions#destroy" | ||
get "/login", to: "sessions#new" | ||
post "/login", to: "sessions#create" | ||
delete "/logout", to: "sessions#destroy" | ||
resources :users | ||
|
||
# Account Activations | ||
resources :account_activations, only: [:edit] | ||
resources :account_activations, only: [ :edit ] | ||
# Password Reset | ||
resources :password_resets, only: [ :new, :create, :edit, :update ] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class AddResetToUsers < ActiveRecord::Migration[7.2] | ||
def change | ||
add_column :users, :reset_digest, :string | ||
add_column :users, :reset_sent_at, :datetime | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require "test_helper" | ||
|
||
class PasswordResetsControllerTest < ActionDispatch::IntegrationTest | ||
test "should get new" do | ||
get password_resets_new_url | ||
assert_response :success | ||
end | ||
|
||
# test "should get edit" do | ||
# get password_resets_edit_url | ||
# assert_response :success | ||
# end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.