質問編集履歴
2
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -301,3 +301,9 @@
|
|
301
301
|
```
|
302
302
|
|
303
303
|
*appIDとシークレットはdotenvに保管して、herokuに直接環境変数を入力しています
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
見にくくて恐縮ですが、どなたか何かアドバイスをいただけるとありがたいです。
|
1
問題のコードを追加します。
test
CHANGED
File without changes
|
test
CHANGED
@@ -62,7 +62,7 @@
|
|
62
62
|
|
63
63
|
|
64
64
|
|
65
|
-
また、facebook for developperのほうでも有効なOAuthリダイレクトURIの設定は行っています。
|
65
|
+
また、facebook for developperのほうでも、ドメイン/users/auth/facebook/callback と、設定して有効なOAuthリダイレクトURIの設定は行っています。
|
66
66
|
|
67
67
|
|
68
68
|
|
@@ -119,3 +119,185 @@
|
|
119
119
|
```
|
120
120
|
|
121
121
|
actionnotfoundエラーと上記のエラーが出てしばらくログがつずいた後にstatus=404になります。
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
### 該当のコード
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
routes.rb
|
130
|
+
|
131
|
+
```
|
132
|
+
|
133
|
+
Rails.application.routes.draw do
|
134
|
+
|
135
|
+
devise_for :users, controllers: {
|
136
|
+
|
137
|
+
omniauth_callbacks: 'users/omniauth_callbacks'
|
138
|
+
|
139
|
+
}
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
root 'posts#index'
|
148
|
+
|
149
|
+
get '/users/:id', to: 'users#show', as: 'user'
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
resources :posts, only: %i(index new create show destroy) do
|
154
|
+
|
155
|
+
resources :photos, only: %i(create)
|
156
|
+
|
157
|
+
resources :likes, only: %i(create destroy)
|
158
|
+
|
159
|
+
resources :comments, only: %i(create destroy)
|
160
|
+
|
161
|
+
end
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
end
|
166
|
+
|
167
|
+
```
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
app/controllers/users/omniauth_callbacks_controller.rb
|
172
|
+
|
173
|
+
```
|
174
|
+
|
175
|
+
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
|
176
|
+
|
177
|
+
def facebook
|
178
|
+
|
179
|
+
# You need to implement the method below in your model (e.g. app/models/user.rb)
|
180
|
+
|
181
|
+
@user = User.from_omniauth(request.env["omniauth.auth"])
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
if @user.persisted?
|
186
|
+
|
187
|
+
sign_in_and_redirect @user, event: :authentication #this will throw if @user is not activated
|
188
|
+
|
189
|
+
set_flash_message(:notice, :success, kind: "Facebook") if is_navigational_format?
|
190
|
+
|
191
|
+
else
|
192
|
+
|
193
|
+
session["devise.facebook_data"] = request.env["omniauth.auth"]
|
194
|
+
|
195
|
+
redirect_to new_user_registration_url
|
196
|
+
|
197
|
+
end
|
198
|
+
|
199
|
+
end
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
def failure
|
204
|
+
|
205
|
+
redirect_to root_path
|
206
|
+
|
207
|
+
end
|
208
|
+
|
209
|
+
end
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
```
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
app/models/user.rb
|
218
|
+
|
219
|
+
```
|
220
|
+
|
221
|
+
class User < ApplicationRecord
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
has_many :posts, dependent: :destroy
|
226
|
+
|
227
|
+
has_many :likes
|
228
|
+
|
229
|
+
has_many :comments
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
# Include default devise modules. Others available are:
|
234
|
+
|
235
|
+
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
|
236
|
+
|
237
|
+
devise :database_authenticatable, :registerable,
|
238
|
+
|
239
|
+
:recoverable, :rememberable, :trackable, :validatable, :omniauthable
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
validates :name, presence: true, length: { maximum: 50 }
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
def self.from_omniauth(auth)
|
248
|
+
|
249
|
+
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
|
250
|
+
|
251
|
+
user.email = auth.info.email
|
252
|
+
|
253
|
+
user.password = Devise.friendly_token[0, 20]
|
254
|
+
|
255
|
+
user.name = auth.info.name # assuming the user model has a name
|
256
|
+
|
257
|
+
user.image = auth.info.image # assuming the user model has an image
|
258
|
+
|
259
|
+
# If you are using confirmable and the provider(s) you use validate emails,
|
260
|
+
|
261
|
+
# uncomment the line below to skip the confirmation emails.
|
262
|
+
|
263
|
+
# user.skip_confirmation!
|
264
|
+
|
265
|
+
end
|
266
|
+
|
267
|
+
end
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
def self.new_with_session(params, session)
|
272
|
+
|
273
|
+
super.tap do |user|
|
274
|
+
|
275
|
+
if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
|
276
|
+
|
277
|
+
user.email = data["email"] if user.email.blank?
|
278
|
+
|
279
|
+
end
|
280
|
+
|
281
|
+
end
|
282
|
+
|
283
|
+
end
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
end
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
```
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
config/initializers/devise.rb 一部抜粋
|
296
|
+
|
297
|
+
```
|
298
|
+
|
299
|
+
config.omniauth :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_APP_SECRET'], token_params: { parse: :json }
|
300
|
+
|
301
|
+
```
|
302
|
+
|
303
|
+
*appIDとシークレットはdotenvに保管して、herokuに直接環境変数を入力しています
|