-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
20 changed files
with
200 additions
and
5 deletions.
There are no files selected for viewing
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
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,65 @@ | ||
class PasswordResetsController < ApplicationController | ||
before_action :load_user, :valid_user, :check_expiration, | ||
only: %i(update edit) | ||
def new; end | ||
|
||
def edit; end | ||
|
||
def create | ||
@user = User.find_by email: params.dig(:password_reset, :email)&.downcase | ||
if @user | ||
@user.create_reset_digest | ||
@user.send_password_reset_email | ||
flash[:info] = t ".check_email" | ||
|
||
redirect_to root_url | ||
else | ||
flash.now[:danger] = t ".invalid_user" | ||
render :new, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def update | ||
if params.dig(:user, :password).empty? | ||
@user.errors.add :password, t(".cannot_empty") | ||
return render :edit, status: :unprocessable_entity | ||
end | ||
|
||
if @user.update user_params | ||
log_in @user | ||
@user.update_column :reset_digest, nil | ||
flash[:success] = t ".reset_success" | ||
return redirect_to @user | ||
end | ||
|
||
render :edit, status: :unprocessable_entity | ||
end | ||
|
||
private | ||
|
||
def user_params | ||
params.require(:user).permit User::RESET_PARAMS | ||
end | ||
|
||
def load_user | ||
return if @user = User.find_by(email: params[:email]) | ||
|
||
flash[:danger] = t "password_resets.invalid_email" | ||
redirect_to root_path | ||
end | ||
|
||
def check_expiration | ||
return unless @user.password_reset_expired? | ||
|
||
flash[:danger] = t "password_resets.expired_link" | ||
redirect_to root_path | ||
end | ||
|
||
def valid_user | ||
return if @user.activated? && @user.try(:authenticated?, "reset", | ||
params[:id]) | ||
|
||
flash[:danger] = t "password_resets.invalid_user" | ||
redirect_to root_path | ||
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
class ApplicationMailer < ActionMailer::Base | ||
default from: "from@example.com" | ||
default from: "noreply@example.com" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class UserMailer < ApplicationMailer | ||
def password_reset user | ||
@user = user | ||
mail to: user.email, subject: t(".password_reset") | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<% provide :title, t(".password_reset") %> | ||
<div class="flex flex-col items-center justify-center"> | ||
<h1 class="text-2xl font-bold"><%= t ".password_reset" %></h1> | ||
<div class="mt-5"> | ||
<%= form_with model: @user, url: password_reset_path(params[:id]), class: "flex flex-col" do |f| %> | ||
<%= render "shared/error_messages", object: f.object %> | ||
<%= hidden_field_tag :email, @user.email %> | ||
<%= f.label :password, t(".password"), class: "text-base font-bold" %> | ||
<%= f.password_field :password, class: "rounded-lg mt-2" %> | ||
<%= f.label :password_confirmation, t(".confirm"), class: "text-base font-bold" %> | ||
<%= f.password_field :password_confirmation, class: "rounded-lg mt-2" %> | ||
<button type="submit" class="mt-2 px-4 py-2 bg-primary text-white rounded-sm hover:opacity-70 text-lg flex items-center justify-center"> | ||
<%= t ".submit" %> | ||
</button> | ||
<% 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, t(".forgot_password") %> | ||
<div class="flex flex-col justify-center items-center"> | ||
<h1 class="text-2xl font-bold"><%= t ".forgot_password" %></h1> | ||
<div class="mt-5"> | ||
<%= form_for :password_reset, url: password_resets_path, class: "flex flex-col" do |f| %> | ||
<%= f.label :email, class: "text-base font-bold" %> | ||
<%= f.email_field :email, class: "rounded-lg mt-2" %> | ||
<button type="submit" class="mt-2 px-4 py-2 bg-primary text-white rounded-sm hover:opacity-70 text-lg flex items-center justify-center"> | ||
<%= t ".submit" %> | ||
</button> | ||
<% 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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<h1>B-World</h1> | ||
<p><%= t ".greeting", deep_interpolation: true, user_name: @user.user_name %>, </p> | ||
<p><%= t".instruction", deep_interpolation: true, expire_time: Settings.value.reset_expired_time %></p> | ||
<%= link_to t(".reset"), edit_password_reset_url(id: @user.reset_token, email: @user.email) %> |
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,5 @@ | ||
Sample App | ||
<%= t ".greeting", deep_interpolation: true, user_name: @user.user_name %> | ||
<%= t".instruction", deep_interpolation: true, expire_time: Settings.value.reset_expired_time %> | ||
<%= edit_password_reset_url(id: @user.reset_token, email: @user.email) %> | ||
<%= link_to t(".reset"), edit_password_reset_url(id: @user.reset_token, email: @user.email) %> |
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
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