-
Notifications
You must be signed in to change notification settings - Fork 20
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
33 changed files
with
638 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
class ApplicationController < ActionController::Base | ||
include Authentication | ||
|
||
before_action :authenticate_user! | ||
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,34 @@ | ||
module Authentication | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
helper_method :current_user, :user_signed_in? | ||
|
||
def authenticate_user! | ||
redirect_to root_path, alert: t("controllers.concerns.authentication.unauthorized") unless user_signed_in? | ||
end | ||
|
||
def current_user | ||
Current.user ||= authenticate_user_from_session | ||
end | ||
|
||
def authenticate_user_from_session | ||
User.find_by(id: session[:user_id]) | ||
end | ||
|
||
def user_signed_in? | ||
current_user.present? | ||
end | ||
|
||
def login(user) | ||
Current.user = user | ||
reset_session | ||
session[:user_id] = user.id | ||
end | ||
|
||
def logout | ||
Current.user = nil | ||
reset_session | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class MainController < ApplicationController | ||
skip_before_action :authenticate_user! | ||
def index | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
class PasswordResetsController < ApplicationController | ||
skip_before_action :authenticate_user!, only: [:new, :create] | ||
before_action :set_user_by_token, only: [:edit, :update] | ||
|
||
def new | ||
end | ||
|
||
def edit | ||
end | ||
|
||
def create | ||
@user = User.find_by(email: params[:email]) | ||
|
||
if @user.present? | ||
PasswordMailer.with( | ||
user: @user, | ||
token: @user.generate_token_for(:password_reset) | ||
).password_reset.deliver_now | ||
end | ||
|
||
redirect_to root_path, notice: t("controllers.password_resets.create.notice") | ||
end | ||
|
||
def update | ||
if @user.update(password_params) | ||
redirect_to new_session_path, notice: t("controllers.password_resets.update.notice") | ||
else | ||
render :edit, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
private | ||
|
||
def set_user_by_token | ||
@user = User.find_by_token_for(:password_reset, params[:token]) | ||
|
||
redirect_to new_password_reset_path alert: t("controllers.password_resets.errors.invalid_token") if @user.blank? | ||
end | ||
|
||
def password_params | ||
params.require(:user).permit(:password, :password_confirmation) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class PasswordsController < ApplicationController | ||
def edit | ||
end | ||
|
||
def update | ||
if current_user.update(password_params) | ||
redirect_to edit_password_path, notice: t("controllers.passwords.update.notice") | ||
else | ||
render :edit, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
private | ||
|
||
def password_params | ||
params.require(:user).permit( | ||
:password, | ||
:password_confirmation, | ||
:password_challenge | ||
).with_defaults(password_challenge: "") | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
class RegistrationsController < ApplicationController | ||
skip_before_action :authenticate_user! | ||
|
||
def new | ||
@user = User.new | ||
end | ||
|
||
def create | ||
@user = User.new(registration_params) | ||
|
||
if @user.save | ||
login @user | ||
redirect_to root_path | ||
else | ||
render :new, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
private | ||
|
||
def registration_params | ||
params.require(:user).permit(:email, :password, :password_confirmation) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
class SessionsController < ApplicationController | ||
skip_before_action :authenticate_user! | ||
|
||
def new | ||
end | ||
|
||
def create | ||
@user = User.authenticate_by(email: session_params[:email], password: session_params[:password]) | ||
|
||
if @user | ||
login @user | ||
redirect_to root_path, notice: t("controllers.sessions.create.notice") | ||
else | ||
flash[:alert] = t("controllers.sessions.create.alert") | ||
render :new, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def destroy | ||
logout | ||
redirect_to root_path, notice: t("controllers.sessions.destroy.notice") | ||
end | ||
|
||
private | ||
|
||
def session_params | ||
params.require(:user).permit(:email, :password) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class PasswordMailer < ApplicationMailer | ||
def password_reset | ||
mail to: params[:user].email | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Current < ActiveSupport::CurrentAttributes | ||
attribute :user | ||
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,19 +1,28 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>RailsWorld</title> | ||
<meta name="viewport" content="width=device-width,initial-scale=1"> | ||
<%= csrf_meta_tags %> | ||
<%= csp_meta_tag %> | ||
<%= stylesheet_link_tag "tailwind", "inter-font", "data-turbo-track": "reload" %> | ||
<head> | ||
<title>RailsWorld</title> | ||
<meta name="viewport" content="width=device-width,initial-scale=1"> | ||
<%= csrf_meta_tags %> | ||
<%= csp_meta_tag %> | ||
<%= stylesheet_link_tag "tailwind", "inter-font", "data-turbo-track": "reload" %> | ||
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %> | ||
<%= javascript_importmap_tags %> | ||
</head> | ||
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %> | ||
<%= javascript_importmap_tags %> | ||
</head> | ||
|
||
<body> | ||
<main class="container mx-auto mt-28 px-5 flex"> | ||
<%= yield %> | ||
</main> | ||
</body> | ||
<body> | ||
<main class="container mx-auto mt-28 px-5 flex"> | ||
<div><%= notice %></div> | ||
<div><%= alert %></div> | ||
<% if user_signed_in? %> | ||
<%= link_to "Edit Password", edit_password_path %> | ||
<%= button_to "Log out", session_path, method: :delete %> | ||
<% else %> | ||
<%= link_to "Sign Up", new_registration_path %> | ||
<%= link_to "Log in", new_session_path %> | ||
<% end %> | ||
<%= yield %> | ||
</main> | ||
</body> | ||
</html> |
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 @@ | ||
<h1>Homepage</h1> |
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 @@ | ||
<%= link_to "Reset your password", edit_password_reset_url(token: params[:token]) %> |
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,23 @@ | ||
<h1>Reset Your Password</h1> | ||
|
||
<%= form_with model: @user, url: password_reset_path(token: params[:token]) do |form| %> | ||
<% if form.object.errors.any? %> | ||
<% form.object.errors.full_messages.each do |message| %> | ||
<div> <%= message %></div> | ||
<% end %> | ||
<% end %> | ||
|
||
<div> | ||
<%= form.label :password %> | ||
<%= form.password_field :password %> | ||
</div> | ||
|
||
<div> | ||
<%= form.label :password_confirmation %> | ||
<%= form.password_field :password_confirmation %> | ||
</div> | ||
|
||
<div> | ||
<%= form.submit "Reset Your Password" %> | ||
</div> | ||
<% 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,12 @@ | ||
<h1>Reset Your Password</h1> | ||
|
||
<%= form_with model: @user, url: password_reset_path do |form| %> | ||
<div> | ||
<%= form.label :email %> | ||
<%= form.email_field :email %> | ||
</div> | ||
|
||
<div> | ||
<%= form.submit "Reset password" %> | ||
</div> | ||
<% end %> |
Oops, something went wrong.