質問編集履歴

2

エラーを更新しました

2018/09/29 09:29

投稿

yamady
yamady

スコア176

test CHANGED
File without changes
test CHANGED
@@ -1,110 +1,50 @@
1
- いつも大変お世話になっております。
1
+ いつもお世話になっております。
2
2
 
3
3
 
4
4
 
5
- Rails APIモードGoogleログインを実装したいと考えています。
5
+ Ruby on Rails APIモードにて、Google認証を実装したいと考えているのですがエラーで苦戦しております。
6
-
7
- 今回はDeviseを使わずに、[こちらのQiita記事](https://qiita.com/daijiro_maeyama/items/8b672ec0721d43f2d044)を参考にしながら進めているのですが、エラーが出てしまい、困っています。
8
-
9
-
10
-
11
- ### 実現したいこと
12
-
13
-
14
-
15
- Rails APIモードによるGoogleログインの実装
16
-
17
-
18
-
19
- ### エラー、実装において困っていること
20
6
 
21
7
 
22
8
 
23
9
  ```
24
10
 
25
- # ターミナルでcurlコマンドを打つと・・・
11
+ OmniAuth::Strategies::OAuth2::CallbackError
26
12
 
27
- $ curl localhost:3000/v1/auth/google_oauth2/callback 'email=XXXX@gmail.com&password=XXXXXXXXX'
28
-
29
-
30
-
31
- # こちらのエラーが発生します
13
+ csrf_detected | CSRF detected
32
-
33
- "status":404,"error":"Not Found","exception":"#\u003cActionController::RoutingError: uninitialized constant V1::SessionsController\u003e","traces":{"Application Trace":[],
34
-
35
- ・・・
36
14
 
37
15
  ```
38
16
 
39
17
 
40
18
 
41
- ### ソースコード
42
-
43
- > セッションコントローラー
44
-
45
- ```Ruby
46
-
47
- # app/controllers/sessions_controller.rb
19
+ このエラーは、Googleのログイン画面に飛んでからログインしようとすると生じてしまいます。
48
-
49
- module V1
50
-
51
- class SessionsController < ApplicationController
52
-
53
- # POST /v1/login
54
-
55
- def create
56
-
57
- @user = User.from_omniauth(request.env["omniauth.auth"])
58
-
59
- if @user.save
60
-
61
- session[:user_id] = @user.id
62
-
63
- render json: @user, serializer: SessionSerializer, root: nil
64
-
65
- else
66
-
67
- invalid_password
68
-
69
- end
70
-
71
- end
72
20
 
73
21
 
74
22
 
75
- # DELETE /v1
23
+ ## 開発環境
76
24
 
77
- def destroy
25
+ rails 5.2.1
78
26
 
79
- session[:user_id] = nil
27
+ omniauth-google-oauth2
80
28
 
81
- end
29
+ (deviseは使っていません)
82
30
 
83
31
 
84
32
 
85
- private
33
+ ソースコードは下記となります
86
34
 
87
35
 
88
36
 
89
- def invalid_email
37
+ ### omniauth.rb
90
38
 
91
- warden.custom_failure!
39
+ ```
92
40
 
93
- render json: { error: t('invalid_email') }
41
+ Rails.application.config.middleware.use OmniAuth::Builder do
94
42
 
95
- end
43
+ provider :google_oauth2,
96
44
 
45
+ Rails.application.credentials.google[:google_client_id],
97
46
 
98
-
99
- def invalid_password
100
-
101
- warden.custom_failure!
102
-
103
- render json: { error: t('invalid_password') }
47
+ Rails.application.credentials.google[:google_client_secret]
104
-
105
- end
106
-
107
- end
108
48
 
109
49
  end
110
50
 
@@ -112,13 +52,9 @@
112
52
 
113
53
 
114
54
 
115
- > モデル
55
+ ### user.rb
116
56
 
117
- ```Ruby
57
+ ```
118
-
119
- # app/models/user.rb
120
-
121
-
122
58
 
123
59
  class User < ApplicationRecord
124
60
 
@@ -126,19 +62,19 @@
126
62
 
127
63
  where(provider: auth.provider, uid: auth.uid).first_or_initialize.tap do |user|
128
64
 
129
- @user.provider = auth.provider
65
+ user.provider = auth.provider
130
66
 
131
- @user.uid = auth.uid
67
+ user.uid = auth.uid
132
68
 
133
- @user.name = auth.info.name
69
+ user.name = auth.info.name
134
70
 
135
- @user.email = auth.info.email
71
+ user.email = auth.info.email
136
72
 
137
- @user.image = auth.info.image
73
+ user.image = auth.info.image
138
74
 
139
- @user.oauth_token = auth.credentials.token
75
+ user.oauth_token = auth.credentials.token
140
76
 
141
- @user.oauth_expires_at = Time.at(auth.credentials.expires_at)
77
+ user.oauth_expires_at = Time.at(auth.credentials.expires_at)
142
78
 
143
79
  return user
144
80
 
@@ -152,17 +88,43 @@
152
88
 
153
89
 
154
90
 
155
- > シリアライズ
91
+ ### sessions_controller.rb
156
92
 
157
- ```Ruby
93
+ ```
158
94
 
159
- # app/serializers/session_serializer.rb
95
+ class SessionsController < ApplicationController
160
96
 
161
- module V1
97
+ def new
162
98
 
163
- class SessionSerializer < ActiveModel::Serializer
99
+ end
164
100
 
101
+
102
+
103
+ def create
104
+
165
- attributes :provider, :uid, :name, :email, :image, :oauth_token, :oauth_expires_at
105
+ @user = User.from_omniauth(request.env["omniauth.auth"])
106
+
107
+ if @user.save
108
+
109
+ session[:user_id] = user.id
110
+
111
+ render json: "succesfully login.\n", status: 200
112
+
113
+ else
114
+
115
+ render json: "fail to login.\n", status: 500
116
+
117
+ end
118
+
119
+ end
120
+
121
+
122
+
123
+ def destroy
124
+
125
+ session[:user_id] = nil
126
+
127
+ render json: "succesfully logout.\n", status: 200
166
128
 
167
129
  end
168
130
 
@@ -172,15 +134,37 @@
172
134
 
173
135
 
174
136
 
175
- > omniauth設定
137
+ ### application.rb
176
138
 
177
- ```Ruby
139
+ ```
178
140
 
179
- # config/omniauth.rb
141
+ ・・・
180
142
 
181
- Rails.application.config.middleware.use OmniAuth::Builder do
143
+ config.api_only = true
182
144
 
145
+ config.middleware.use ActionDispatch::Flash
146
+
147
+ config.middleware.use ActionDispatch::Cookies
148
+
149
+ config.middleware.use ActionDispatch::Session::CookieStore
150
+
151
+ config.middleware.insert_before 0, Rack::Cors do
152
+
153
+ allow do
154
+
183
- provider :google_oauth2, ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_CLIENT_SECRET']
155
+ origins 'http://localhost:3000'
156
+
157
+ resource '*',
158
+
159
+ :headers => :any,
160
+
161
+ :methods => [:get, :post, :patch, :delete, :options]
162
+
163
+ end
164
+
165
+ end
166
+
167
+ end
184
168
 
185
169
  end
186
170
 
@@ -188,27 +172,21 @@
188
172
 
189
173
 
190
174
 
191
- > ルーティング
175
+ ### routes.rb
192
176
 
193
- ```Ruby
177
+ ```
194
-
195
- # config/routes.rb
196
178
 
197
179
  Rails.application.routes.draw do
198
180
 
199
- namespace :v1, defaults: { format: :json } do
181
+ get 'auth/:provider/callback', to: 'sessions#create'
200
182
 
201
- get 'auth/:provider/callback', to: 'sessions#create'
183
+ get 'auth/failure', to: redirect('/')
202
184
 
203
- get 'auth/failure', to: redirect('/')
204
-
205
- get 'signout', to: 'sessions#destroy', as: 'signout'
185
+ get 'signout', to: 'sessions#destroy', as: 'signout'
206
186
 
207
187
 
208
188
 
209
- resources :sessions, only: %i(new create destroy)
189
+ resources :sessions, only: [:new, :create, :destroy]
210
-
211
- end
212
190
 
213
191
  end
214
192
 

1

ソースコードを更新しました

2018/09/29 09:29

投稿

yamady
yamady

スコア176

test CHANGED
@@ -1 +1 @@
1
- Rails APIモードでGoogle Authを実現したい(rails)
1
+ Rails APIモードでGoogle Authを実現したい(rails api
test CHANGED
@@ -22,39 +22,161 @@
22
22
 
23
23
  ```
24
24
 
25
- You must provide a session to use OmniAuth.
25
+ # ターミナルでcurlコマンドを打つと・・・
26
+
26
-
27
+ $ curl localhost:3000/v1/auth/google_oauth2/callback 'email=XXXX@gmail.com&password=XXXXXXXXX'
28
+
29
+
30
+
31
+ # こちらのエラーが発生します
32
+
33
+ "status":404,"error":"Not Found","exception":"#\u003cActionController::RoutingError: uninitialized constant V1::SessionsController\u003e","traces":{"Application Trace":[],
34
+
35
+ ・・・
36
+
27
- ```
37
+ ```
28
-
29
-
30
-
31
- そもそも、Rails APIモードにてGoogleログインの実装ができているかどうかを確認する最も適切な方法を同時に教示していただきたいです。
32
38
 
33
39
 
34
40
 
35
41
  ### ソースコード
36
42
 
37
-
38
-
39
- > gemfile
40
-
41
- ```
42
-
43
- ・・・
44
-
45
- # omniauth-google-oauth2の追加
46
-
47
- gem 'omniauth-google-oauth2'
48
-
49
- ・・・
50
-
51
- ```
52
-
53
-
54
-
55
- > omniauth.rb
56
-
57
- ```Ruby
43
+ > セッションコントローラー
44
+
45
+ ```Ruby
46
+
47
+ # app/controllers/sessions_controller.rb
48
+
49
+ module V1
50
+
51
+ class SessionsController < ApplicationController
52
+
53
+ # POST /v1/login
54
+
55
+ def create
56
+
57
+ @user = User.from_omniauth(request.env["omniauth.auth"])
58
+
59
+ if @user.save
60
+
61
+ session[:user_id] = @user.id
62
+
63
+ render json: @user, serializer: SessionSerializer, root: nil
64
+
65
+ else
66
+
67
+ invalid_password
68
+
69
+ end
70
+
71
+ end
72
+
73
+
74
+
75
+ # DELETE /v1
76
+
77
+ def destroy
78
+
79
+ session[:user_id] = nil
80
+
81
+ end
82
+
83
+
84
+
85
+ private
86
+
87
+
88
+
89
+ def invalid_email
90
+
91
+ warden.custom_failure!
92
+
93
+ render json: { error: t('invalid_email') }
94
+
95
+ end
96
+
97
+
98
+
99
+ def invalid_password
100
+
101
+ warden.custom_failure!
102
+
103
+ render json: { error: t('invalid_password') }
104
+
105
+ end
106
+
107
+ end
108
+
109
+ end
110
+
111
+ ```
112
+
113
+
114
+
115
+ > モデル
116
+
117
+ ```Ruby
118
+
119
+ # app/models/user.rb
120
+
121
+
122
+
123
+ class User < ApplicationRecord
124
+
125
+ def self.from_omniauth(auth)
126
+
127
+ where(provider: auth.provider, uid: auth.uid).first_or_initialize.tap do |user|
128
+
129
+ @user.provider = auth.provider
130
+
131
+ @user.uid = auth.uid
132
+
133
+ @user.name = auth.info.name
134
+
135
+ @user.email = auth.info.email
136
+
137
+ @user.image = auth.info.image
138
+
139
+ @user.oauth_token = auth.credentials.token
140
+
141
+ @user.oauth_expires_at = Time.at(auth.credentials.expires_at)
142
+
143
+ return user
144
+
145
+ end
146
+
147
+ end
148
+
149
+ end
150
+
151
+ ```
152
+
153
+
154
+
155
+ > シリアライズ
156
+
157
+ ```Ruby
158
+
159
+ # app/serializers/session_serializer.rb
160
+
161
+ module V1
162
+
163
+ class SessionSerializer < ActiveModel::Serializer
164
+
165
+ attributes :provider, :uid, :name, :email, :image, :oauth_token, :oauth_expires_at
166
+
167
+ end
168
+
169
+ end
170
+
171
+ ```
172
+
173
+
174
+
175
+ > omniauth設定
176
+
177
+ ```Ruby
178
+
179
+ # config/omniauth.rb
58
180
 
59
181
  Rails.application.config.middleware.use OmniAuth::Builder do
60
182
 
@@ -66,142 +188,28 @@
66
188
 
67
189
 
68
190
 
69
- > .env
191
+ > ルーティング
70
-
192
+
71
- ```
193
+ ```Ruby
72
-
73
- GOOGLE_CLIENT_ID="XXXXXX-XXXXXXXX.apps.googleusercontent.com"
194
+
74
-
75
- GOOGLE_CLIENT_SECRET="XXXXXXXXXXXXXXXXXXXXXXXX"
76
-
77
- ```
78
-
79
-
80
-
81
- > routes.rb
195
+ # config/routes.rb
82
-
83
- ```
84
196
 
85
197
  Rails.application.routes.draw do
86
198
 
199
+ namespace :v1, defaults: { format: :json } do
200
+
87
- get 'auth/:provider/callback', to: 'sessions#create'
201
+ get 'auth/:provider/callback', to: 'sessions#create'
88
-
202
+
89
- get 'auth/failure', to: redirect('/')
203
+ get 'auth/failure', to: redirect('/')
90
-
204
+
91
- get 'signout', to: 'sessions#destroy', as: 'signout'
205
+ get 'signout', to: 'sessions#destroy', as: 'signout'
92
-
93
-
94
-
206
+
207
+
208
+
95
- resources :sessions, only: %i(new create destroy)
209
+ resources :sessions, only: %i(new create destroy)
96
-
210
+
97
- end
211
+ end
212
+
98
-
213
+ end
214
+
99
- ```
215
+ ```
100
-
101
-
102
-
103
- > user.rb
104
-
105
- ```Ruby
106
-
107
- class User < ApplicationRecord
108
-
109
- def self.from_omniauth(auth)
110
-
111
- where(provider: auth.provider, uid: auth.uid).first_or_initialize.tap do |user|
112
-
113
- user.provider = auth.provider
114
-
115
- user.uid = auth.uid
116
-
117
- user.name = auth.info.name
118
-
119
- user.email = auth.info.email
120
-
121
- user.image = auth.info.image
122
-
123
- user.oauth_token = auth.credentials.token
124
-
125
- user.oauth_expires_at = Time.at(auth.credentials.expires_at)
126
-
127
- return user
128
-
129
- end
130
-
131
- end
132
-
133
- end
134
-
135
- ```
136
-
137
-
138
-
139
- > sessions_controler.rb
140
-
141
- ```Ruby
142
-
143
- class SessionsController < ApplicationController
144
-
145
- def new
146
-
147
- end
148
-
149
-
150
-
151
- def create
152
-
153
- user = User.from_omniauth(request.env["omniauth.auth"])
154
-
155
- if user.save
156
-
157
- session[:user_id] = user.id
158
-
159
- render json: @user
160
-
161
- else
162
-
163
- head :no_content
164
-
165
- end
166
-
167
- end
168
-
169
-
170
-
171
- def destroy
172
-
173
- session[:user_id] = nil
174
-
175
- head :no_content
176
-
177
- end
178
-
179
- end
180
-
181
- ```
182
-
183
-
184
-
185
- > application_controller.rb
186
-
187
- ```Ruby
188
-
189
- class ApplicationController < ActionController::API
190
-
191
- helper_method :current_user
192
-
193
-
194
-
195
- def current_user
196
-
197
- User.find(session[:user_id]) if session[:user_id]
198
-
199
- end
200
-
201
- end
202
-
203
- ```
204
-
205
-
206
-
207
- すみませんが、どうぞ宜しくお願いいたします。