-
Notifications
You must be signed in to change notification settings - Fork 0
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
9 changed files
with
106 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
class ApplicationController < ActionController::Base | ||
include SessionsHelper #追記 | ||
include Pagy::Backend #追記 | ||
|
||
private | ||
|
||
def require_user_logged_in | ||
unless logged_in? | ||
redirect_to login_url | ||
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,36 @@ | ||
class SessionsController < ApplicationController | ||
def new | ||
end | ||
|
||
def create | ||
email = params[:session][:email].downcase | ||
password = params[:session][:password] | ||
if login(email, password) | ||
flash[:success] = 'ログインに成功しました。' | ||
redirect_to @user | ||
else | ||
flash.now[:danger] = 'ログインに失敗しました。' | ||
render :new | ||
end | ||
end | ||
|
||
def destroy | ||
session[:user_id] = nil | ||
flash[:success] = 'ログアウトしました。' | ||
redirect_to root_url | ||
end | ||
|
||
private | ||
|
||
def login(email, password) | ||
@user = User.find_by(email: email) | ||
if @user && @user.authenticate(password) | ||
# ログイン成功 | ||
session[:user_id] = @user.id | ||
return true | ||
else | ||
# ログイン失敗 | ||
return false | ||
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,9 @@ | ||
module SessionsHelper | ||
def current_user | ||
@current_user ||= User.find_by(id: session[:user_id]) | ||
end | ||
|
||
def logged_in? | ||
!!current_user | ||
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 @@ | ||
<h1>Sessions#create</h1> | ||
<p>Find me in app/views/sessions/create.html.erb</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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<h1>Sessions#destroy</h1> | ||
<p>Find me in app/views/sessions/destroy.html.erb</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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<div class="text-center"> | ||
<h1>Log in</h1> | ||
</div> | ||
|
||
<div class="row"> | ||
<div class="col-sm-6 offset-sm-3"> | ||
|
||
<%= form_with(url: login_path, scope: :session) do |f| %> | ||
|
||
<div class="mb-3"> | ||
<%= f.label :email, 'Email' %> | ||
<%= f.email_field :email, class: 'form-control' %> | ||
</div> | ||
|
||
<div class="mb-3"> | ||
<%= f.label :password, 'Password' %> | ||
<%= f.password_field :password, class: 'form-control' %> | ||
</div> | ||
|
||
<%= f.submit 'Log in', class: 'btn btn-primary w-100' %> | ||
<% end %> | ||
|
||
<p>New user? <%= link_to 'Sign up now!', signup_path %></p> | ||
</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