teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

追記

2019/08/13 04:36

投稿

sakana-suki
sakana-suki

スコア14

title CHANGED
File without changes
body CHANGED
@@ -149,4 +149,7 @@
149
149
  ```
150
150
  config.omniauth :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_APP_SECRET'], token_params: { parse: :json }
151
151
  ```
152
- *appIDとシークレットはdotenvに保管して、herokuに直接環境変数を入力しています
152
+ *appIDとシークレットはdotenvに保管して、herokuに直接環境変数を入力しています
153
+
154
+
155
+ 見にくくて恐縮ですが、どなたか何かアドバイスをいただけるとありがたいです。

1

問題のコードを追加します。

2019/08/13 04:36

投稿

sakana-suki
sakana-suki

スコア14

title CHANGED
File without changes
body CHANGED
@@ -30,7 +30,7 @@
30
30
  omniauth_callbacks_controller.rbの作成
31
31
  user.rbに def self.from_omniauth(auth)メソッドを追加しました。
32
32
 
33
- また、facebook for developperのほうでも有効なOAuthリダイレクトURIの設定は行っています。
33
+ また、facebook for developperのほうでも、ドメイン/users/auth/facebook/callback と、設定して有効なOAuthリダイレクトURIの設定は行っています。
34
34
 
35
35
  ちなみにdeviseを使ったログイン、ログアウト自体は実装済みで、問題なく動いています。
36
36
 
@@ -58,4 +58,95 @@
58
58
  ```
59
59
  FATAL -- : [d2a59dc7-2032-4235-9eed-50f1883ca039] vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.3/lib/abstract_controller/base.rb:129:in `process'
60
60
  ```
61
- actionnotfoundエラーと上記のエラーが出てしばらくログがつずいた後にstatus=404になります。
61
+ actionnotfoundエラーと上記のエラーが出てしばらくログがつずいた後にstatus=404になります。
62
+
63
+ ### 該当のコード
64
+
65
+ routes.rb
66
+ ```
67
+ Rails.application.routes.draw do
68
+ devise_for :users, controllers: {
69
+ omniauth_callbacks: 'users/omniauth_callbacks'
70
+ }
71
+
72
+ # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
73
+
74
+ root 'posts#index'
75
+ get '/users/:id', to: 'users#show', as: 'user'
76
+
77
+ resources :posts, only: %i(index new create show destroy) do
78
+ resources :photos, only: %i(create)
79
+ resources :likes, only: %i(create destroy)
80
+ resources :comments, only: %i(create destroy)
81
+ end
82
+
83
+ end
84
+ ```
85
+
86
+ app/controllers/users/omniauth_callbacks_controller.rb
87
+ ```
88
+ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
89
+ def facebook
90
+ # You need to implement the method below in your model (e.g. app/models/user.rb)
91
+ @user = User.from_omniauth(request.env["omniauth.auth"])
92
+
93
+ if @user.persisted?
94
+ sign_in_and_redirect @user, event: :authentication #this will throw if @user is not activated
95
+ set_flash_message(:notice, :success, kind: "Facebook") if is_navigational_format?
96
+ else
97
+ session["devise.facebook_data"] = request.env["omniauth.auth"]
98
+ redirect_to new_user_registration_url
99
+ end
100
+ end
101
+
102
+ def failure
103
+ redirect_to root_path
104
+ end
105
+ end
106
+
107
+ ```
108
+
109
+ app/models/user.rb
110
+ ```
111
+ class User < ApplicationRecord
112
+
113
+ has_many :posts, dependent: :destroy
114
+ has_many :likes
115
+ has_many :comments
116
+
117
+ # Include default devise modules. Others available are:
118
+ # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
119
+ devise :database_authenticatable, :registerable,
120
+ :recoverable, :rememberable, :trackable, :validatable, :omniauthable
121
+
122
+ validates :name, presence: true, length: { maximum: 50 }
123
+
124
+ def self.from_omniauth(auth)
125
+ where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
126
+ user.email = auth.info.email
127
+ user.password = Devise.friendly_token[0, 20]
128
+ user.name = auth.info.name # assuming the user model has a name
129
+ user.image = auth.info.image # assuming the user model has an image
130
+ # If you are using confirmable and the provider(s) you use validate emails,
131
+ # uncomment the line below to skip the confirmation emails.
132
+ # user.skip_confirmation!
133
+ end
134
+ end
135
+
136
+ def self.new_with_session(params, session)
137
+ super.tap do |user|
138
+ if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
139
+ user.email = data["email"] if user.email.blank?
140
+ end
141
+ end
142
+ end
143
+
144
+ end
145
+
146
+ ```
147
+
148
+ config/initializers/devise.rb 一部抜粋
149
+ ```
150
+ config.omniauth :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_APP_SECRET'], token_params: { parse: :json }
151
+ ```
152
+ *appIDとシークレットはdotenvに保管して、herokuに直接環境変数を入力しています