質問編集履歴
1
コードを追記しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -29,3 +29,143 @@
|
|
29
29
|
やはり同じエラーが出てしまいました。
|
30
30
|
|
31
31
|
なぜでしょうか。
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
失礼いたしました。
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
##該当するソースコード
|
40
|
+
|
41
|
+
▼ User.rb
|
42
|
+
|
43
|
+
```Ruby
|
44
|
+
|
45
|
+
class User < ApplicationRecord
|
46
|
+
|
47
|
+
# Include default devise modules. Others available are:
|
48
|
+
|
49
|
+
# :confirmable, :lockable, :timeoutable and :omniauthable
|
50
|
+
|
51
|
+
devise :database_authenticatable, :registerable,
|
52
|
+
|
53
|
+
:recoverable, :rememberable, :trackable, :validatable,
|
54
|
+
|
55
|
+
:omniauthable
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
class << self
|
60
|
+
|
61
|
+
def find_for_facebook_oauth(auth)
|
62
|
+
|
63
|
+
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
|
64
|
+
|
65
|
+
user.email = auth.info.email
|
66
|
+
|
67
|
+
user.password = Devise.friendly_token[0,20]
|
68
|
+
|
69
|
+
user.name = auth.info.name
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
def create_unique_string
|
78
|
+
|
79
|
+
SecureRandom.uuid
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
def create_unique_email
|
86
|
+
|
87
|
+
User.create_unique_string + "@example.com"
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
```
|
96
|
+
|
97
|
+
▼ users/omniauth_callbacks_controller.rb
|
98
|
+
|
99
|
+
```Ruby
|
100
|
+
|
101
|
+
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
|
102
|
+
|
103
|
+
def facebook
|
104
|
+
|
105
|
+
@user = User.find_for_facebook_oauth(request.env["omniauth.auth"])
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
if @user.persisted?
|
110
|
+
|
111
|
+
sign_in_and_redirect @user, :event => :authentication
|
112
|
+
|
113
|
+
set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
|
114
|
+
|
115
|
+
else
|
116
|
+
|
117
|
+
session["devise.facebook_data"] = request.env["omniauth.auth"]
|
118
|
+
|
119
|
+
redirect_to new_user_registration_url
|
120
|
+
|
121
|
+
end
|
122
|
+
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|
126
|
+
|
127
|
+
```
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
▼ Devise.rb
|
132
|
+
|
133
|
+
```Ruby
|
134
|
+
|
135
|
+
Devise.setup do |config|
|
136
|
+
|
137
|
+
config.omniauth :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_APP_SECRET']
|
138
|
+
|
139
|
+
end
|
140
|
+
|
141
|
+
```
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
▼ .env
|
146
|
+
|
147
|
+
```ここに言語を入力
|
148
|
+
|
149
|
+
FACEBOOK_APP_ID="XXXXXXXXX"
|
150
|
+
|
151
|
+
FACEBOOK_APP_SECRET="XXXXXXXXXXXXXX"
|
152
|
+
|
153
|
+
```
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
▼ routes.rb
|
158
|
+
|
159
|
+
```Ruby
|
160
|
+
|
161
|
+
Rails.application.routes.draw do
|
162
|
+
|
163
|
+
devise_for :users, controllers: { sessions: 'users/sessions', registrations: 'users/registrations', omniauth_callbacks: 'users/omniauth_callbacks' }
|
164
|
+
|
165
|
+
root 'pages#index'
|
166
|
+
|
167
|
+
get 'pages/show'
|
168
|
+
|
169
|
+
end
|
170
|
+
|
171
|
+
```
|