質問編集履歴
2
コードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -50,7 +50,7 @@
|
|
50
50
|
|
51
51
|
|
52
52
|
|
53
|
-
|
53
|
+
|
54
54
|
|
55
55
|
|
56
56
|
|
@@ -404,26 +404,14 @@
|
|
404
404
|
|
405
405
|
### 試したこと
|
406
406
|
|
407
|
-
o
|
407
|
+
また、route.rbの get 'auth/:provider/callback' => 'users#create'を get 'auth/:provider/callback' => 'twitter#create'に変えてみたのですが
|
408
|
-
|
408
|
+
|
409
|
-
```
|
409
|
+
```
|
410
|
+
|
410
|
-
|
411
|
+
OAuth::Unauthorized
|
412
|
+
|
411
|
-
|
413
|
+
401 Authorization Required
|
412
|
-
|
413
|
-
If you are the application owner check the logs for more information.
|
414
414
|
|
415
415
|
```
|
416
416
|
|
417
417
|
というエラーが出て認証できませんでした
|
418
|
-
|
419
|
-
また、route.rbの get 'auth/:provider/callback' => 'users#create'を get 'auth/:provider/callback' => 'twitter#create'に変えてみたのですが
|
420
|
-
|
421
|
-
```
|
422
|
-
|
423
|
-
OAuth::Unauthorized
|
424
|
-
|
425
|
-
401 Authorization Required
|
426
|
-
|
427
|
-
```
|
428
|
-
|
429
|
-
というエラーが出て認証できませんでした
|
1
コード追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -60,7 +60,343 @@
|
|
60
60
|
|
61
61
|
```
|
62
62
|
|
63
|
+
**controllers/users_controller.rb**
|
64
|
+
|
65
|
+
class UsersController < ApplicationController
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
before_action :authenticate_user,{only:[:index,:show,:edit,:update]}
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
before_action :forbid_login_user,{only:[:new,:create,:login_form,:login]}
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
before_action :ensure_correct_user,{only:[:edit,:update]}
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
def show
|
82
|
+
|
83
|
+
@user = User.find_by(id: params[:id])
|
84
|
+
|
85
|
+
@post = Post.where(user_id: @user.id)
|
86
|
+
|
87
|
+
|
88
|
+
|
63
|
-
|
89
|
+
end
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
def new
|
94
|
+
|
95
|
+
@user = User.new
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
def create
|
106
|
+
|
107
|
+
@user = User.new(
|
108
|
+
|
109
|
+
name: params[:name],
|
110
|
+
|
111
|
+
email: params[:email],
|
112
|
+
|
113
|
+
image_name: "pile-up3.jpg",
|
114
|
+
|
115
|
+
password: params[:password]
|
116
|
+
|
117
|
+
)
|
118
|
+
|
119
|
+
if @user.save
|
120
|
+
|
121
|
+
session[:user_id] = @user.id
|
122
|
+
|
123
|
+
redirect_to("/users/#{@user.id}")
|
124
|
+
|
125
|
+
else
|
126
|
+
|
127
|
+
render("users/new")
|
128
|
+
|
129
|
+
end
|
130
|
+
|
131
|
+
end
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
def edit
|
136
|
+
|
137
|
+
@user = User.find_by(id: params[:id])
|
138
|
+
|
139
|
+
end
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
def update
|
144
|
+
|
145
|
+
@user = User.find_by(id: params[:id])
|
146
|
+
|
147
|
+
@user.name = params[:name]
|
148
|
+
|
149
|
+
@user.email = params[:email]
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
if params[:image]
|
154
|
+
|
155
|
+
@user.image_name = "#{@user.id}.jpg"
|
156
|
+
|
157
|
+
image = params[:image]
|
158
|
+
|
159
|
+
File.binwrite("public/user_images/#{@user.image_name}",image.read)
|
160
|
+
|
161
|
+
end
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
if @user.save
|
166
|
+
|
167
|
+
redirect_to("/users/#{@user.id}")
|
168
|
+
|
169
|
+
else
|
170
|
+
|
171
|
+
render("users/edit")
|
172
|
+
|
173
|
+
end
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
end
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
def login_form
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
end
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
def login
|
192
|
+
|
193
|
+
@user = User.find_by(email: params[:email])
|
194
|
+
|
195
|
+
if @user && @user.authenticate(params[:password])
|
196
|
+
|
197
|
+
session[:user_id] = @user.id
|
198
|
+
|
199
|
+
redirect_to("/users/#{@user.id}")
|
200
|
+
|
201
|
+
else
|
202
|
+
|
203
|
+
@error_message = "メールアドレスまたはパスワードが間違っています"
|
204
|
+
|
205
|
+
@email = params[:email]
|
206
|
+
|
207
|
+
@password = params[:password]
|
208
|
+
|
209
|
+
render("users/login_form")
|
210
|
+
|
211
|
+
end
|
212
|
+
|
213
|
+
end
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
def logout
|
218
|
+
|
219
|
+
session[:user_id] = nil
|
220
|
+
|
221
|
+
redirect_to("/login")
|
222
|
+
|
223
|
+
end
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
def ensure_correct_user
|
230
|
+
|
231
|
+
if @current_user.id != params[:id].to_i
|
232
|
+
|
233
|
+
flash[:notice] = "権限がありません"
|
234
|
+
|
235
|
+
redirect_to("/posts/index")
|
236
|
+
|
237
|
+
end
|
238
|
+
|
239
|
+
end
|
240
|
+
|
241
|
+
end
|
242
|
+
|
243
|
+
```
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
```
|
248
|
+
|
249
|
+
**.env**
|
250
|
+
|
251
|
+
TWITTER_CONSUMER_KEY = "Consumer Key"
|
252
|
+
|
253
|
+
TWITTER_CONSUMER_SECRET = "Consumer Secret"
|
254
|
+
|
255
|
+
```
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
```
|
260
|
+
|
261
|
+
**secrets.yml**
|
262
|
+
|
263
|
+
default_twitter: &default_twitter
|
264
|
+
|
265
|
+
twitter_api_key: <%= ENV["TWITTER_CONSUMER_KEY"] %>
|
266
|
+
|
267
|
+
twitter_api_secret: <%= ENV["TWITTER_CONSUMER_SECRET"] %>
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
development:
|
276
|
+
|
277
|
+
secret_key_base: #最初から記入されているはず
|
278
|
+
|
279
|
+
<<: *default_twitter
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
test:
|
284
|
+
|
285
|
+
secret_key_base: #最初から記入されているはず
|
286
|
+
|
287
|
+
<<: *default_twitter
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
production:
|
292
|
+
|
293
|
+
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
|
294
|
+
|
295
|
+
twitter_api_key: <%= ENV["TWITTER_CONSUMER_KEY"] %>
|
296
|
+
|
297
|
+
twitter_api_secret: <%= ENV["TWITTER_CONSUMER_SECRET"] %>
|
298
|
+
|
299
|
+
```
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
```
|
304
|
+
|
305
|
+
**initializers/omniauth.rb**
|
306
|
+
|
307
|
+
Rails.application.config.middleware.use OmniAuth::Builder do
|
308
|
+
|
309
|
+
provider :twitter, ENV['TWITTER_CONSUMER_KEY'], ENV['TWITTER_CONSUMER_SECRET']
|
310
|
+
|
311
|
+
end
|
312
|
+
|
313
|
+
```
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
```
|
318
|
+
|
319
|
+
**model/twitter.rb**
|
320
|
+
|
321
|
+
class Twitter < ApplicationRecord
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
def self.find_or_create_from_auth_hash(auth_hash)
|
326
|
+
|
327
|
+
provider = auth_hash[:provider]
|
328
|
+
|
329
|
+
uid = auth_hash[:uid]
|
330
|
+
|
331
|
+
name = auth_hash[:info][:name]
|
332
|
+
|
333
|
+
image_url = auth_hash[:info][:image]
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
self.find_or_create_by(provider: provider,uid: uid) do |user|
|
340
|
+
|
341
|
+
user.username = name
|
342
|
+
|
343
|
+
user.image_url = image_url
|
344
|
+
|
345
|
+
end
|
346
|
+
|
347
|
+
end
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
end
|
352
|
+
|
353
|
+
```
|
354
|
+
|
355
|
+
|
356
|
+
|
357
|
+
```
|
358
|
+
|
359
|
+
**controllers/twitter_controller.rb**
|
360
|
+
|
361
|
+
class TwitterController < ApplicationController
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
def create
|
366
|
+
|
367
|
+
user = User.find_or_create_from_auth_hash(request.env['omniauth.auth'])
|
368
|
+
|
369
|
+
if user
|
370
|
+
|
371
|
+
session[:user_id] = user.id
|
372
|
+
|
373
|
+
redirect_to '/posts/index', notice: "ログインしました。"
|
374
|
+
|
375
|
+
else
|
376
|
+
|
377
|
+
redirect_to root_path, notice: "失敗しました。"
|
378
|
+
|
379
|
+
end
|
380
|
+
|
381
|
+
end
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
end
|
386
|
+
|
387
|
+
```
|
388
|
+
|
389
|
+
|
390
|
+
|
391
|
+
```
|
392
|
+
|
393
|
+
**route.rb**
|
394
|
+
|
395
|
+
get 'auth/:provider/callback' => 'users#create'
|
396
|
+
|
397
|
+
|
398
|
+
|
399
|
+
root 'home#top'
|
64
400
|
|
65
401
|
```
|
66
402
|
|
@@ -79,3 +415,15 @@
|
|
79
415
|
```
|
80
416
|
|
81
417
|
というエラーが出て認証できませんでした
|
418
|
+
|
419
|
+
また、route.rbの get 'auth/:provider/callback' => 'users#create'を get 'auth/:provider/callback' => 'twitter#create'に変えてみたのですが
|
420
|
+
|
421
|
+
```
|
422
|
+
|
423
|
+
OAuth::Unauthorized
|
424
|
+
|
425
|
+
401 Authorization Required
|
426
|
+
|
427
|
+
```
|
428
|
+
|
429
|
+
というエラーが出て認証できませんでした
|