質問編集履歴
2
config>application.rbを追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -62,123 +62,353 @@
|
|
62
62
|
|
63
63
|
railsから返ってきたlogged_in:の値によってログイン済みユーザか(セッションが残っているか)を判断しています
|
64
64
|
|
65
|
+
|
66
|
+
|
67
|
+
railsでセッションを記録させるリクエストを送るreactコード
|
68
|
+
|
69
|
+
```
|
70
|
+
|
71
|
+
const signIn = () => {
|
72
|
+
|
73
|
+
axios
|
74
|
+
|
75
|
+
.post(
|
76
|
+
|
77
|
+
process.env.REACT_APP_HOST + ":3001" + "/login",
|
78
|
+
|
79
|
+
{
|
80
|
+
|
81
|
+
user: {
|
82
|
+
|
83
|
+
email: email,
|
84
|
+
|
85
|
+
password: password,
|
86
|
+
|
87
|
+
},
|
88
|
+
|
89
|
+
},
|
90
|
+
|
91
|
+
{ withCredentials: true }
|
92
|
+
|
93
|
+
)
|
94
|
+
|
95
|
+
.then((response) => {
|
96
|
+
|
97
|
+
console.log("registration res", response);
|
98
|
+
|
99
|
+
const createdId = response.data.id;
|
100
|
+
|
101
|
+
console.log(createdId);
|
102
|
+
|
103
|
+
props.login();
|
104
|
+
|
105
|
+
history.push({ pathname: "/users/" + createdId });
|
106
|
+
|
107
|
+
})
|
108
|
+
|
109
|
+
.catch((error) => {
|
110
|
+
|
111
|
+
console.log("registration error", error);
|
112
|
+
|
113
|
+
alert("メールアドレスとパスワードの組み合わせが正しくありません。");
|
114
|
+
|
115
|
+
});
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
```
|
120
|
+
|
121
|
+
railsにセッションを確認させるリクエストを送るreactコード
|
122
|
+
|
123
|
+
```
|
124
|
+
|
125
|
+
const checkLoginStatus = () => {
|
126
|
+
|
127
|
+
axios
|
128
|
+
|
129
|
+
.get(process.env.REACT_APP_HOST + ":3001" + "/login", {
|
130
|
+
|
131
|
+
withCredentials: true,
|
132
|
+
|
133
|
+
})
|
134
|
+
|
135
|
+
.then((response) => {
|
136
|
+
|
137
|
+
console.log(response);
|
138
|
+
|
139
|
+
setLoggedInStatus(response.data.logged_in);
|
140
|
+
|
141
|
+
setLoaded(true);
|
142
|
+
|
143
|
+
if (response.data.logged_in) {
|
144
|
+
|
145
|
+
return;
|
146
|
+
|
147
|
+
} else {
|
148
|
+
|
149
|
+
alert("ログインして下さい");
|
150
|
+
|
151
|
+
history.push("/signin");
|
152
|
+
|
153
|
+
}
|
154
|
+
|
155
|
+
});
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
```
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
rails routes.rb
|
164
|
+
|
165
|
+
上記リクエストが以下のルートによって、sessions_controllerのnewに届きます
|
166
|
+
|
65
167
|
```ここに言語を入力
|
66
168
|
|
67
|
-
|
169
|
+
get '/login', to: 'sessions#new'
|
68
|
-
|
69
|
-
|
170
|
+
|
70
|
-
|
71
|
-
|
171
|
+
post '/login', to: 'sessions#create'
|
72
|
-
|
73
|
-
|
172
|
+
|
74
|
-
|
75
|
-
})
|
76
|
-
|
77
|
-
.then((response) => {
|
78
|
-
|
79
|
-
console.log(response);
|
80
|
-
|
81
|
-
setLoggedInStatus(response.data.logged_in);
|
82
|
-
|
83
|
-
setLoaded(true);
|
84
|
-
|
85
|
-
if (response.data.logged_in) {
|
86
|
-
|
87
|
-
return;
|
88
|
-
|
89
|
-
} else {
|
90
|
-
|
91
|
-
alert("ログインして下さい");
|
92
|
-
|
93
|
-
history.push("/signin");
|
94
|
-
|
95
|
-
}
|
96
|
-
|
97
|
-
});
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
```
|
173
|
+
```
|
102
|
-
|
103
|
-
|
104
|
-
|
174
|
+
|
175
|
+
|
176
|
+
|
105
|
-
|
177
|
+
sessions_controller.rb
|
106
|
-
|
107
|
-
上記リクエストが以下のルートによって、sessions_controllerのnewに届きます
|
108
178
|
|
109
179
|
```ここに言語を入力
|
110
180
|
|
181
|
+
before_action :logged_in_user, only: [:new]
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
def create
|
186
|
+
|
187
|
+
@user = User.find_by(email: signin_params[:email])
|
188
|
+
|
189
|
+
if @user && @user.authenticate(signin_params[:password])
|
190
|
+
|
191
|
+
session[:user_id] = @user.id
|
192
|
+
|
193
|
+
render json: current_user
|
194
|
+
|
195
|
+
else
|
196
|
+
|
197
|
+
render status: 404
|
198
|
+
|
199
|
+
end
|
200
|
+
|
201
|
+
end
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
def new
|
206
|
+
|
207
|
+
if current_user
|
208
|
+
|
209
|
+
render json: { logged_in: true, user: current_user, session: session[:user_id] }
|
210
|
+
|
211
|
+
else
|
212
|
+
|
111
|
-
|
213
|
+
render json: { logged_in: false, message: 'ユーザーが存在しません', session: session[:user_id] }
|
214
|
+
|
112
|
-
|
215
|
+
end
|
216
|
+
|
217
|
+
end
|
218
|
+
|
113
|
-
```
|
219
|
+
```
|
114
|
-
|
115
|
-
|
116
|
-
|
220
|
+
|
221
|
+
|
222
|
+
|
117
|
-
|
223
|
+
application_controller.rb
|
118
224
|
|
119
225
|
```ここに言語を入力
|
120
226
|
|
121
|
-
before_action :logged_in_user, only: [:new]
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
def new
|
126
|
-
|
127
|
-
|
227
|
+
def current_user
|
228
|
+
|
128
|
-
|
229
|
+
@current_user ||= User.find_by(id: session[:user_id]) if session[:user_id]
|
230
|
+
|
231
|
+
end
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
def logged_in?
|
236
|
+
|
237
|
+
!current_user.nil?
|
238
|
+
|
239
|
+
end
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
def logged_in_user
|
244
|
+
|
129
|
-
|
245
|
+
render json: { logged_in: false, message: current_user } unless logged_in?
|
130
|
-
|
246
|
+
|
131
|
-
|
247
|
+
end
|
248
|
+
|
132
|
-
|
249
|
+
```
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
ブラウザのコンソール画面に表示されたresponse.data
|
254
|
+
|
255
|
+
```ここに言語を入力
|
256
|
+
|
257
|
+
logged_in: false
|
258
|
+
|
259
|
+
message: null
|
260
|
+
|
261
|
+
session: null
|
262
|
+
|
263
|
+
```
|
264
|
+
|
265
|
+
config>application.rb
|
266
|
+
|
267
|
+
```ここに言語を入力
|
268
|
+
|
269
|
+
require_relative 'boot'
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
require 'rails'
|
274
|
+
|
275
|
+
# Pick the frameworks you want:
|
276
|
+
|
277
|
+
require 'active_model/railtie'
|
278
|
+
|
279
|
+
require 'active_job/railtie'
|
280
|
+
|
281
|
+
require 'active_record/railtie'
|
282
|
+
|
283
|
+
require 'active_storage/engine'
|
284
|
+
|
285
|
+
require 'action_controller/railtie'
|
286
|
+
|
287
|
+
require 'action_mailer/railtie'
|
288
|
+
|
289
|
+
require 'action_mailbox/engine'
|
290
|
+
|
291
|
+
require 'action_text/engine'
|
292
|
+
|
293
|
+
require 'action_view/railtie'
|
294
|
+
|
295
|
+
require 'action_cable/engine'
|
296
|
+
|
297
|
+
# require "sprockets/railtie"
|
298
|
+
|
299
|
+
require 'rails/test_unit/railtie'
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
# Require the gems listed in Gemfile, including any gems
|
304
|
+
|
305
|
+
# you've limited to :test, :development, or :production.
|
306
|
+
|
307
|
+
Bundler.require(*Rails.groups)
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
module Myapp
|
312
|
+
|
313
|
+
class Application < Rails::Application
|
314
|
+
|
315
|
+
# Initialize configuration defaults for originally generated Rails version.
|
316
|
+
|
317
|
+
config.load_defaults 6.1
|
318
|
+
|
319
|
+
# Configuration for the application, engines, and railties goes here.
|
320
|
+
|
321
|
+
#
|
322
|
+
|
133
|
-
|
323
|
+
# These settings can be overridden in specific environments using the files
|
324
|
+
|
325
|
+
# in config/environments, which are processed later.
|
326
|
+
|
327
|
+
#
|
328
|
+
|
329
|
+
# config.time_zone = "Central Time (US & Canada)"
|
330
|
+
|
331
|
+
# config.eager_load_paths << Rails.root.join("extras")
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
# Only loads a smaller set of middleware suitable for API only apps.
|
336
|
+
|
337
|
+
# Middleware like session, flash, cookies can be added back manually.
|
338
|
+
|
339
|
+
# Skip views, helpers and assets when generating a new resource.
|
340
|
+
|
341
|
+
config.api_only = true
|
342
|
+
|
343
|
+
config.middleware.insert_before 0, Rack::Cors do
|
344
|
+
|
345
|
+
allow do
|
346
|
+
|
347
|
+
origins 'http://localhost:80', 'http://独自ドメイン'
|
348
|
+
|
349
|
+
resource '*',
|
350
|
+
|
351
|
+
headers: :any,
|
352
|
+
|
353
|
+
methods: %i[get post patch delete options],
|
354
|
+
|
355
|
+
credentials: true
|
356
|
+
|
357
|
+
end
|
134
358
|
|
135
359
|
end
|
136
360
|
|
361
|
+
|
362
|
+
|
363
|
+
config.hosts << '.example.com'
|
364
|
+
|
365
|
+
config.hosts << '独自ドメイン'
|
366
|
+
|
367
|
+
config.hosts << 'localhost'
|
368
|
+
|
369
|
+
|
370
|
+
|
371
|
+
# セッションメソッドを有効にする
|
372
|
+
|
373
|
+
config.middleware.use ActionDispatch::Cookies
|
374
|
+
|
375
|
+
config.middleware.use ActionDispatch::Session::CookieStore
|
376
|
+
|
377
|
+
config.middleware.use ActionDispatch::ContentSecurityPolicy::Middleware
|
378
|
+
|
379
|
+
|
380
|
+
|
381
|
+
config.time_zone = 'Tokyo'
|
382
|
+
|
383
|
+
config.active_record.default_timezone = :local
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
config.action_dispatch.default_headers = {
|
388
|
+
|
389
|
+
'Access-Control-Allow-Credentials' => 'true',
|
390
|
+
|
391
|
+
'Access-Control-Allow-Origin' =>
|
392
|
+
|
393
|
+
if Rails.env.production?
|
394
|
+
|
395
|
+
'http://独自ドメイン'
|
396
|
+
|
397
|
+
else
|
398
|
+
|
399
|
+
'http://localhost'
|
400
|
+
|
401
|
+
end,
|
402
|
+
|
403
|
+
'Access-Control-Request-Method' => '*'
|
404
|
+
|
405
|
+
}
|
406
|
+
|
137
|
-
end
|
407
|
+
end
|
138
|
-
|
139
|
-
|
408
|
+
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
application_controller.rb
|
144
|
-
|
145
|
-
```ここに言語を入力
|
146
|
-
|
147
|
-
def current_user
|
148
|
-
|
149
|
-
@current_user ||= User.find_by(id: session[:user_id]) if session[:user_id]
|
150
|
-
|
151
|
-
|
409
|
+
end
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
410
|
+
|
156
|
-
|
157
|
-
|
411
|
+
|
158
|
-
|
159
|
-
end
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
def logged_in_user
|
164
|
-
|
165
|
-
render json: { logged_in: false, message: current_user } unless logged_in?
|
166
|
-
|
167
|
-
end
|
168
|
-
|
169
|
-
```
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
ブラウザのコンソール画面に表示されたresponse.data
|
174
|
-
|
175
|
-
```ここに言語を入力
|
176
|
-
|
177
|
-
logged_in: false
|
178
|
-
|
179
|
-
message: null
|
180
|
-
|
181
|
-
session: null
|
182
412
|
|
183
413
|
```
|
184
414
|
|
1
reactでセッションを確認するリクエストを送る部分、railsでそのリクエストを処理する部分のコードを追加いたしました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -51,3 +51,141 @@
|
|
51
51
|
|
52
52
|
|
53
53
|
よろしくお願いいたします。
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
### 追記
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
フロントエンド(react)で以下のコードにより、railsにリクエストを投げてセッションを確認しています。
|
62
|
+
|
63
|
+
railsから返ってきたlogged_in:の値によってログイン済みユーザか(セッションが残っているか)を判断しています
|
64
|
+
|
65
|
+
```ここに言語を入力
|
66
|
+
|
67
|
+
const checkLoginStatus = () => {
|
68
|
+
|
69
|
+
axios
|
70
|
+
|
71
|
+
.get(process.env.REACT_APP_HOST + ":3001" + "/login", {
|
72
|
+
|
73
|
+
withCredentials: true,
|
74
|
+
|
75
|
+
})
|
76
|
+
|
77
|
+
.then((response) => {
|
78
|
+
|
79
|
+
console.log(response);
|
80
|
+
|
81
|
+
setLoggedInStatus(response.data.logged_in);
|
82
|
+
|
83
|
+
setLoaded(true);
|
84
|
+
|
85
|
+
if (response.data.logged_in) {
|
86
|
+
|
87
|
+
return;
|
88
|
+
|
89
|
+
} else {
|
90
|
+
|
91
|
+
alert("ログインして下さい");
|
92
|
+
|
93
|
+
history.push("/signin");
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
});
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
```
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
rails routes.rb
|
106
|
+
|
107
|
+
上記リクエストが以下のルートによって、sessions_controllerのnewに届きます
|
108
|
+
|
109
|
+
```ここに言語を入力
|
110
|
+
|
111
|
+
get '/login', to: 'sessions#new'
|
112
|
+
|
113
|
+
```
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
sessions_controller.rb
|
118
|
+
|
119
|
+
```ここに言語を入力
|
120
|
+
|
121
|
+
before_action :logged_in_user, only: [:new]
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
def new
|
126
|
+
|
127
|
+
if current_user
|
128
|
+
|
129
|
+
render json: { logged_in: true, user: current_user, session: session[:user_id] }
|
130
|
+
|
131
|
+
else
|
132
|
+
|
133
|
+
render json: { logged_in: false, message: 'ユーザーが存在しません', session: session[:user_id] }
|
134
|
+
|
135
|
+
end
|
136
|
+
|
137
|
+
end
|
138
|
+
|
139
|
+
```
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
application_controller.rb
|
144
|
+
|
145
|
+
```ここに言語を入力
|
146
|
+
|
147
|
+
def current_user
|
148
|
+
|
149
|
+
@current_user ||= User.find_by(id: session[:user_id]) if session[:user_id]
|
150
|
+
|
151
|
+
end
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
def logged_in?
|
156
|
+
|
157
|
+
!current_user.nil?
|
158
|
+
|
159
|
+
end
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
def logged_in_user
|
164
|
+
|
165
|
+
render json: { logged_in: false, message: current_user } unless logged_in?
|
166
|
+
|
167
|
+
end
|
168
|
+
|
169
|
+
```
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
ブラウザのコンソール画面に表示されたresponse.data
|
174
|
+
|
175
|
+
```ここに言語を入力
|
176
|
+
|
177
|
+
logged_in: false
|
178
|
+
|
179
|
+
message: null
|
180
|
+
|
181
|
+
session: null
|
182
|
+
|
183
|
+
```
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
message: nullとなっているのはapplicaion_controllerのlogged_in_userでcurrent_userが存在したいため。
|
188
|
+
|
189
|
+
current_userが存在しないのはsession[:user_id]が存在しないため。
|
190
|
+
|
191
|
+
つまり、セッションが有効になっていないことが原因と考えております。
|