Skip to content

Commit c9e9542

Browse files
committed
Googleでログイン
1 parent 16f9c18 commit c9e9542

20 files changed

+157
-564
lines changed

Gemfile

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ gem 'bootsnap', '>= 1.4.4', require: false
3131

3232
gem 'sorcery'
3333

34+
gem 'sorcery-oauth'
35+
3436
gem 'bootstrap', '~> 5.3.0'
3537

3638
gem 'ruby-openai'

Gemfile.lock

+5
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,10 @@ GEM
338338
bcrypt (~> 3.1)
339339
oauth (>= 0.6)
340340
oauth2 (~> 2.0)
341+
sorcery-core (0.0.0)
342+
bcrypt (~> 3.0)
343+
sorcery-oauth (0.0.0)
344+
sorcery-core (= 0.0.0)
341345
spring (4.2.1)
342346
sprockets (4.2.1)
343347
concurrent-ruby (~> 1.0)
@@ -413,6 +417,7 @@ DEPENDENCIES
413417
sass-rails (>= 6)
414418
selenium-webdriver
415419
sorcery
420+
sorcery-oauth
416421
spring
417422
turbolinks (~> 5)
418423
tzinfo-data

app/controllers/oauths_controller.rb

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class OauthsController < ApplicationController
2+
skip_before_action :require_login
3+
4+
def oauth
5+
login_at(auth_params[:provider])
6+
end
7+
8+
def callback
9+
provider = auth_params[:provider]
10+
if @user = login_from(provider)
11+
flash[:success] = "認証に成功しました。健康維持のための指示に従ってください。"
12+
redirect_to root_path
13+
else
14+
begin
15+
@user = create_from(provider)
16+
reset_session
17+
auto_login(@user)
18+
flash[:success] = '認証に成功しました。健康維持のための指示に従ってください。'
19+
redirect_to root_path
20+
rescue
21+
flash[:danger] = "認証失敗。Googleアカウントでのログインに失敗しました"
22+
redirect_to root_path
23+
end
24+
end
25+
end
26+
27+
private
28+
29+
def auth_params
30+
params.permit(:code, :provider)
31+
end
32+
end

app/helpers/oauths_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module OauthsHelper
2+
end

app/javascript/stylesheets/application.scss

+14
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,18 @@ ul {
107107
width: 100%;
108108
height: 1300px;
109109
overflow: hidden;
110+
}
111+
112+
.google-login-btn {
113+
color: #666666;
114+
text-decoration: none;
115+
font-size: 1rem;
116+
display: block;
117+
text-align: center;
118+
margin: 30px auto 0;
119+
width: fit-content;
120+
121+
&:hover {
122+
color: #cccccc;
123+
}
110124
}

app/models/authentication.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Authentication < ApplicationRecord
2+
belongs_to :user
3+
end

app/models/user.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
class User < ApplicationRecord
22
authenticates_with_sorcery!
3+
has_many :authentications, dependent: :destroy
4+
accepts_nested_attributes_for :authentications
35

46
has_many :favorites
57
has_many :favorite_recipes, through: :favorites, source: :recipe

app/views/oauths/callback.html.erb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h1>Oauths#callback</h1>
2+
<p>Find me in app/views/oauths/callback.html.erb</p>

app/views/oauths/oauth.html.erb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h1>Oauths#oauth</h1>
2+
<p>Find me in app/views/oauths/oauth.html.erb</p>

app/views/user_sessions/new.html.erb

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
<% end %>
2323
</div>
2424

25+
<%= link_to 'Googleアカウントでログイン', auth_at_provider_path(provider: :google), class: 'google-login-btn' %>
26+
2527
<div class="password-reset-link">
2628
<%= link_to "パスワードをお忘れの方はこちら", new_password_reset_path %>
2729
</div>
28-
2930
<% end %>
3031
</div>

0 commit comments

Comments
 (0)