質問編集履歴

4

h

2021/10/10 21:32

投稿

pecchan
pecchan

スコア592

test CHANGED
File without changes
test CHANGED
@@ -358,14 +358,6 @@
358
358
 
359
359
  private
360
360
 
361
-
362
-
363
-
364
-
365
-
366
-
367
-
368
-
369
361
  def auth_hash
370
362
 
371
363
  request.env['omniauth.auth']

3

controller追記

2021/10/10 21:32

投稿

pecchan
pecchan

スコア592

test CHANGED
File without changes
test CHANGED
@@ -295,3 +295,83 @@
295
295
 
296
296
 
297
297
  ```
298
+
299
+
300
+
301
+
302
+
303
+ ###翌日追加
304
+
305
+ controllerが抜けてたので追記
306
+
307
+
308
+
309
+ app\controllers\sessions_controller.rb
310
+
311
+ ```ruby
312
+
313
+
314
+
315
+ class SessionsController < ApplicationController
316
+
317
+ def new
318
+
319
+ end
320
+
321
+
322
+
323
+ def create
324
+
325
+ # メールアドレスによるログイン
326
+
327
+ # 省略
328
+
329
+ end
330
+
331
+
332
+
333
+ def destroy
334
+
335
+ log_out if logged_in?
336
+
337
+ redirect_to root_url
338
+
339
+ end
340
+
341
+
342
+
343
+ def create_from_SNS
344
+
345
+ if (user = User.find_or_create_from_auth_hash(auth_hash))
346
+
347
+ log_in(user)
348
+
349
+ binding.pry
350
+
351
+ redirect_back_or mypage_path
352
+
353
+ end
354
+
355
+ end
356
+
357
+
358
+
359
+ private
360
+
361
+
362
+
363
+
364
+
365
+
366
+
367
+
368
+
369
+ def auth_hash
370
+
371
+ request.env['omniauth.auth']
372
+
373
+ end
374
+
375
+ end
376
+
377
+ ```

2

さらに追記

2021/10/10 21:31

投稿

pecchan
pecchan

スコア592

test CHANGED
File without changes
test CHANGED
@@ -235,3 +235,63 @@
235
235
 
236
236
 
237
237
  ```
238
+
239
+
240
+
241
+
242
+
243
+ ###同日さらに追加
244
+
245
+
246
+
247
+ ちょっと分かったので追記です。
248
+
249
+ 合っているか分かりませんが、ソーシャルログイン時のPOST送信は、googleプロバイダにPOSTして返ってきた値をcontrollerへ返しているようです。
250
+
251
+ そのため、viewからの値が渡らないのかもしれません。
252
+
253
+
254
+
255
+ 勘違いしてたらご指摘いただけると幸いです。
256
+
257
+
258
+
259
+ config\routes.rb
260
+
261
+ ```ruby
262
+
263
+ get 'auth/:provider/callback', to: 'sessions#create_from_SNS'
264
+
265
+ ```
266
+
267
+
268
+
269
+ ログ
270
+
271
+ ```ここに言語を入力
272
+
273
+
274
+
275
+ Started POST "/auth/google_oauth2" for ::1 at 2021-10-10 21:54:02 +0900
276
+
277
+ D, [2021-10-10T21:54:02.072597 #3346] DEBUG -- omniauth: (google_oauth2) Request phase initiated.
278
+
279
+
280
+
281
+ Started GET "/auth/google_oauth2/callback?state=b33b81d94be&code=4%2F0AX4XfWga4pb05TboJwU6AFSqUOTdzrJSnS1rzXnW1JCcs5WMhIOwdJtYOL_Qascope=email+profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile+openid&authuser=2&prompt=consent" for ::1 at 2021-10-10 21:55:06 +0900
282
+
283
+ D, [2021-10-10T21:55:06.115749 #3346] DEBUG -- omniauth: (google_oauth2) Callback phase initiated.
284
+
285
+ Processing by SessionsController#create_from_SNS as HTML
286
+
287
+ Parameters: {"state"=>"b33b81d947b10fe981255947c5fb7d127dd65cbe", "code"=>"4/0AX4XfWga4pb05TboJwU6AFSqUOTJCcs5WMhIOwdJtYOL_Qa6x8MaTMIQ", "scope"=>"email profile https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile openid", "authuser"=>"2", "prompt"=>"consent", "provider"=>"google_oauth2"}
288
+
289
+ User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."email" = ? LIMIT ? [["email", "test@gmail.com"], ["LIMIT", 1]]
290
+
291
+
292
+
293
+ From: /home/rails_user/xxxx/app/controllers/sessions_controller.rb:42 SessionsController#login:
294
+
295
+
296
+
297
+ ```

1

html追記

2021/10/10 13:25

投稿

pecchan
pecchan

スコア592

test CHANGED
File without changes
test CHANGED
@@ -135,3 +135,103 @@
135
135
  <%= javascript_pack_tag 'common/toggle-password' %>
136
136
 
137
137
  ```
138
+
139
+
140
+
141
+
142
+
143
+ ###同日追加
144
+
145
+ 展開されたHTMLを追記致します。
146
+
147
+ ```html
148
+
149
+ <!-- コンテンツ -->
150
+
151
+ <div class="content content-bgcolor">
152
+
153
+ <div class="content__inner-1col">
154
+
155
+ <div class="form__inner">
156
+
157
+ <h2 class="lv2-h">ログイン</h2>
158
+
159
+ </div>
160
+
161
+
162
+
163
+ <form action="/auth/google_oauth2" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="&#x2713;" /><input type="hidden" name="authenticity_token" value="3SJaySs6hbnnBTIxJL8zHPjINBg7xQIc0eYD+aOQn2j0VIe7GuBv+vm0aOnAXEjdJfJCl+0kerHfRa6/xUVmmw==" />
164
+
165
+ <input type="submit" name="commit" value="Googleでログイン" class="btn btn-primary btn-block" data-disable-with="Googleでログイン" />
166
+
167
+ <label>
168
+
169
+ <input name="sns[remember_me]" type="hidden" value="0" /><input class="chk" type="checkbox" value="1" name="sns[remember_me]" id="sns_remember_me" />
170
+
171
+ <span class="chk-text fs14">ログインしたままにする</span>
172
+
173
+ </label>
174
+
175
+ </form>
176
+
177
+ <form action="/login" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="&#x2713;" /><input type="hidden" name="authenticity_token" value="kDmpn6Rj0nndDQbdmgP1ZCOVxNnOmKVtMAuQJ7VMqgCgx72qItpNt7yyIC7QZEWrdN+pBNzRa2E+4CHKgnFexA==" />
178
+
179
+
180
+
181
+ <div class="form__inner">
182
+
183
+ <label class="label label-default fs14" for="login-mail">メールアドレス</label>
184
+
185
+ <input class="input-text" id="login-mail" type="email" name="session[email]" />
186
+
187
+ </div>
188
+
189
+ <div class="form__inner">
190
+
191
+ <label class="label label-default fs14" for="login-password">パスワード</label>
192
+
193
+ <a class="link fs12" href="/password_resets/new">(パスワードを忘れた場合はこちら)</a>
194
+
195
+ <div class="toggle-password">
196
+
197
+ <input class="input-text" id="login-password" type="password" name="session[password]" />
198
+
199
+ <i class="toggle-password-i fa fa-eye-slash"></i>
200
+
201
+ </div>
202
+
203
+ <label>
204
+
205
+ <input name="session[remember_me]" type="hidden" value="0" /><input class="chk" type="checkbox" value="1" name="session[remember_me]" id="session_remember_me" />
206
+
207
+ <span class="chk-text fs14">ログインしたままにする</span>
208
+
209
+ </label>
210
+
211
+ </div>
212
+
213
+ <div class="form__inner">
214
+
215
+ <input type="submit" name="commit" value="ログイン" class="btn btn-primary btn-block" data-disable-with="ログイン" />
216
+
217
+ <br>
218
+
219
+ <div style="text-align: center; padding-top: 20px;">
220
+
221
+ <p class="fs12" >会員登録はお済みですか? <a class="link" href="/signup">会員登録はこちら</a><p>
222
+
223
+ </div>
224
+
225
+ </div>
226
+
227
+ </form> </div>
228
+
229
+ <!-- content__inner-1col -->
230
+
231
+ </div>
232
+
233
+ <!-- content content-bgcolor -->
234
+
235
+
236
+
237
+ ```