質問編集履歴
1
user.rbがsessions_controller.rbとなっていたため、user.rbの内容に修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -24,6 +24,268 @@
|
|
24
24
|
|
25
25
|
```ruby
|
26
26
|
|
27
|
+
class User < ApplicationRecord
|
28
|
+
|
29
|
+
attr_accessor :remember_token
|
30
|
+
|
31
|
+
before_save { email.downcase! }
|
32
|
+
|
33
|
+
validates :name, presence: true, length: { maximum: 50 }
|
34
|
+
|
35
|
+
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(.[a-z\d\-]+)*.[a-z]+\z/i
|
36
|
+
|
37
|
+
validates :email, presence: true, length: { maximum: 255 },
|
38
|
+
|
39
|
+
format: { with: VALID_EMAIL_REGEX },
|
40
|
+
|
41
|
+
uniqueness: { case_sensitive: false }
|
42
|
+
|
43
|
+
has_secure_password
|
44
|
+
|
45
|
+
validates :password, presence: true, length: { minimum: 6 }
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
class << self
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
# 渡された文字列のハッシュ値を返す
|
54
|
+
|
55
|
+
def User.digest(string)
|
56
|
+
|
57
|
+
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
|
58
|
+
|
59
|
+
BCrypt::Engine.cost
|
60
|
+
|
61
|
+
BCrypt::Password.create(string, cost: cost)
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
# ランダムなトークンを返す
|
68
|
+
|
69
|
+
def User.new_token
|
70
|
+
|
71
|
+
SecureRandom.urlsafe_base64
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
# 永続セッションのためにユーザーをデータベースに記憶する
|
78
|
+
|
79
|
+
def remember
|
80
|
+
|
81
|
+
self.remember_token = User.new_token
|
82
|
+
|
83
|
+
self.update_attribute(:remember_digest, User.digest(remember_token))
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
# 渡されたトークンがダイジェストと一致したらtrueを返す
|
90
|
+
|
91
|
+
def authenticated?(remember_token)
|
92
|
+
|
93
|
+
return false if remember_digest.nil?
|
94
|
+
|
95
|
+
BCrypt::Password.new(self.remember_digest).is_password?(remember_token)
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
# ユーザーのログイン情報を破棄する
|
102
|
+
|
103
|
+
def forget
|
104
|
+
|
105
|
+
self.update_attribute(:remember_digest, nil)
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
```
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
app/controllers/users_controller.rb
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
```ruby
|
124
|
+
|
125
|
+
class UsersController < ApplicationController
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
def show
|
130
|
+
|
131
|
+
@user = User.find(params[:id])
|
132
|
+
|
133
|
+
end
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
def new
|
138
|
+
|
139
|
+
@user = User.new
|
140
|
+
|
141
|
+
end
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
def create
|
146
|
+
|
147
|
+
@user = User.new(user_params)
|
148
|
+
|
149
|
+
if @user.save
|
150
|
+
|
151
|
+
log_in @user
|
152
|
+
|
153
|
+
flash[:success] = "Welcome to the Sample App!"
|
154
|
+
|
155
|
+
redirect_to @user
|
156
|
+
|
157
|
+
else
|
158
|
+
|
159
|
+
render 'new'
|
160
|
+
|
161
|
+
end
|
162
|
+
|
163
|
+
end
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
private
|
168
|
+
|
169
|
+
def user_params
|
170
|
+
|
171
|
+
params.require(:user).permit(:name, :email, :password,
|
172
|
+
|
173
|
+
:password_confirmation)
|
174
|
+
|
175
|
+
end
|
176
|
+
|
177
|
+
end
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
```
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
app/helpers/sessions_helper.rb
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
```ruby
|
190
|
+
|
191
|
+
module SessionsHelper
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
# 渡されたユーザーでログインする
|
196
|
+
|
197
|
+
def log_in(user)
|
198
|
+
|
199
|
+
session[:user_id] = user.id
|
200
|
+
|
201
|
+
end
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
# ユーザーのセッションを永続的にする
|
206
|
+
|
207
|
+
def remember(user)
|
208
|
+
|
209
|
+
user.remember
|
210
|
+
|
211
|
+
cookies.permanent.signed[:user_id] = user.id
|
212
|
+
|
213
|
+
cookies.permanent[:remember_token] = user.remember_token
|
214
|
+
|
215
|
+
end
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
# 記憶トークンcookieに対応するユーザーを返す
|
220
|
+
|
221
|
+
def current_user
|
222
|
+
|
223
|
+
if (user_id = session[:user_id])
|
224
|
+
|
225
|
+
@current_user ||= User.find_by(id: user_id)
|
226
|
+
|
227
|
+
elsif (user_id = cookies.signed[:user_id])
|
228
|
+
|
229
|
+
user = User.find_by(id: user_id)
|
230
|
+
|
231
|
+
if user && user.authenticated?(cookies[:remember_token])
|
232
|
+
|
233
|
+
log_in user
|
234
|
+
|
235
|
+
@current_user = user
|
236
|
+
|
237
|
+
end
|
238
|
+
|
239
|
+
end
|
240
|
+
|
241
|
+
end
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
# ユーザーがログインしていればtrue、その他ならfalseを返す
|
246
|
+
|
247
|
+
def logged_in?
|
248
|
+
|
249
|
+
!current_user.nil?
|
250
|
+
|
251
|
+
end
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
# 永続的セッションを破棄する
|
256
|
+
|
257
|
+
def forget(user)
|
258
|
+
|
259
|
+
user.forget
|
260
|
+
|
261
|
+
cookies.delete(:user_id)
|
262
|
+
|
263
|
+
cookies.delete(:remember_token)
|
264
|
+
|
265
|
+
end
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
# 現在のユーザーをログアウトする
|
270
|
+
|
271
|
+
def log_out
|
272
|
+
|
273
|
+
forget(current_user)
|
274
|
+
|
275
|
+
session.delete(:user_id)
|
276
|
+
|
277
|
+
@current_user = nil
|
278
|
+
|
279
|
+
end
|
280
|
+
|
281
|
+
end
|
282
|
+
|
283
|
+
```
|
284
|
+
|
285
|
+
app/controllers/sessions_controller.rb
|
286
|
+
|
287
|
+
```ruby
|
288
|
+
|
27
289
|
class SessionsController < ApplicationController
|
28
290
|
|
29
291
|
def new
|
@@ -70,220 +332,4 @@
|
|
70
332
|
|
71
333
|
|
72
334
|
|
73
|
-
app/controllers/users_controller.rb
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
```ruby
|
78
|
-
|
79
|
-
class UsersController < ApplicationController
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
def show
|
84
|
-
|
85
|
-
@user = User.find(params[:id])
|
86
|
-
|
87
|
-
end
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
def new
|
92
|
-
|
93
|
-
@user = User.new
|
94
|
-
|
95
|
-
end
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
def create
|
100
|
-
|
101
|
-
@user = User.new(user_params)
|
102
|
-
|
103
|
-
if @user.save
|
104
|
-
|
105
|
-
log_in @user
|
106
|
-
|
107
|
-
flash[:success] = "Welcome to the Sample App!"
|
108
|
-
|
109
|
-
redirect_to @user
|
110
|
-
|
111
|
-
else
|
112
|
-
|
113
|
-
render 'new'
|
114
|
-
|
115
|
-
end
|
116
|
-
|
117
|
-
end
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
private
|
122
|
-
|
123
|
-
def user_params
|
124
|
-
|
125
|
-
params.require(:user).permit(:name, :email, :password,
|
126
|
-
|
127
|
-
:password_confirmation)
|
128
|
-
|
129
|
-
end
|
130
|
-
|
131
|
-
end
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
```
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
app/helpers/sessions_helper.rb
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
```ruby
|
144
|
-
|
145
|
-
module SessionsHelper
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
# 渡されたユーザーでログインする
|
150
|
-
|
151
|
-
def log_in(user)
|
152
|
-
|
153
|
-
session[:user_id] = user.id
|
154
|
-
|
155
|
-
end
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
# ユーザーのセッションを永続的にする
|
160
|
-
|
161
|
-
def remember(user)
|
162
|
-
|
163
|
-
user.remember
|
164
|
-
|
165
|
-
cookies.permanent.signed[:user_id] = user.id
|
166
|
-
|
167
|
-
cookies.permanent[:remember_token] = user.remember_token
|
168
|
-
|
169
|
-
end
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
# 記憶トークンcookieに対応するユーザーを返す
|
174
|
-
|
175
|
-
def current_user
|
176
|
-
|
177
|
-
if (user_id = session[:user_id])
|
178
|
-
|
179
|
-
@current_user ||= User.find_by(id: user_id)
|
180
|
-
|
181
|
-
elsif (user_id = cookies.signed[:user_id])
|
182
|
-
|
183
|
-
user = User.find_by(id: user_id)
|
184
|
-
|
185
|
-
if user && user.authenticated?(cookies[:remember_token])
|
186
|
-
|
187
|
-
log_in user
|
188
|
-
|
189
|
-
@current_user = user
|
190
|
-
|
191
|
-
end
|
192
|
-
|
193
|
-
end
|
194
|
-
|
195
|
-
end
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
# ユーザーがログインしていればtrue、その他ならfalseを返す
|
200
|
-
|
201
|
-
def logged_in?
|
202
|
-
|
203
|
-
!current_user.nil?
|
204
|
-
|
205
|
-
end
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
# 永続的セッションを破棄する
|
210
|
-
|
211
|
-
def forget(user)
|
212
|
-
|
213
|
-
user.forget
|
214
|
-
|
215
|
-
cookies.delete(:user_id)
|
216
|
-
|
217
|
-
cookies.delete(:remember_token)
|
218
|
-
|
219
|
-
end
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
# 現在のユーザーをログアウトする
|
224
|
-
|
225
|
-
def log_out
|
226
|
-
|
227
|
-
forget(current_user)
|
228
|
-
|
229
|
-
session.delete(:user_id)
|
230
|
-
|
231
|
-
@current_user = nil
|
232
|
-
|
233
|
-
end
|
234
|
-
|
235
|
-
end
|
236
|
-
|
237
|
-
```
|
238
|
-
|
239
|
-
app/controllers/sessions_controller.rb
|
240
|
-
|
241
|
-
```ruby
|
242
|
-
|
243
|
-
class SessionsController < ApplicationController
|
244
|
-
|
245
|
-
def new
|
246
|
-
|
247
|
-
end
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
def create
|
252
|
-
|
253
|
-
user = User.find_by(email: params[:session][:email].downcase)
|
254
|
-
|
255
|
-
if user && user.authenticate(params[:session][:password])
|
256
|
-
|
257
|
-
log_in user
|
258
|
-
|
259
|
-
remember user
|
260
|
-
|
261
|
-
redirect_to user
|
262
|
-
|
263
|
-
else
|
264
|
-
|
265
|
-
flash.now[:danger] = 'Invalid email/password combination'
|
266
|
-
|
267
|
-
render 'new'
|
268
|
-
|
269
|
-
end
|
270
|
-
|
271
|
-
end
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
def destroy
|
276
|
-
|
277
|
-
log_out if logged_in?
|
278
|
-
|
279
|
-
redirect_to root_url
|
280
|
-
|
281
|
-
end
|
282
|
-
|
283
|
-
end
|
284
|
-
|
285
|
-
```
|
286
|
-
|
287
|
-
|
288
|
-
|
289
335
|
ご指摘いただけますと幸いです。
|