質問編集履歴

2

ファイルの修正

2020/11/09 02:12

投稿

anguraaaa
anguraaaa

スコア21

test CHANGED
File without changes
test CHANGED
@@ -8,8 +8,6 @@
8
8
 
9
9
  URLが /register?に飛んでしまいます
10
10
 
11
- vuexにてaxiosを使用してサーバーへリクエストを送っています。
12
-
13
11
  [画像](https://gyazo.com/2eeb56189eec7d7dcb86902e4c523d49)
14
12
 
15
13
 
@@ -222,310 +220,126 @@
222
220
 
223
221
 
224
222
 
225
- javascript/store/modules/users.js
223
+ javascript/router/index.js
226
224
 
227
225
  ```
228
226
 
229
- import axios from '../../plugins/axios'
230
-
231
-
232
-
233
- const state = {
234
-
235
- authUser: null
236
-
237
- }
238
-
239
-
240
-
241
- const getters = {
242
-
243
- authUser: state => state.authUser
244
-
245
- }
246
-
247
-
248
-
249
- const mutations = {
250
-
251
- setUser: (state, user) => {
252
-
253
- state.authUser = user
254
-
255
- }
256
-
257
- }
258
-
259
-
260
-
261
- const actions = {
262
-
263
- async loginUser({ commit }, user) {
264
-
265
- const sessionsResponse = await axios.post('sessions', user)
266
-
267
- localStorage.auth_token = sessionsResponse.data.token
268
-
269
- axios.defaults.headers.common['Authorization'] = `Bearer ${localStorage.auth_token}`
270
-
271
-
272
-
273
- const userResponce = await axios.get('users/me')
274
-
275
- commit('setUser', userResponce.date)
276
-
277
- },
278
-
279
-
280
-
281
- logoutUser({ commit }) {
282
-
283
- localStorage.removeItem('auth_token')
284
-
285
- axios.defaults.headers.common['Authorization'] = ''
286
-
287
- commit('setUser', null)
288
-
289
- },
290
-
291
-
292
-
293
- async fetchAuthUser({ commit, state }) {
294
-
295
- if (!localStorage.auth_token) return null
296
-
297
- if (state.authUser) return state.authUser
298
-
299
-
300
-
301
- const userResponse = await axios.get('users/me')
302
-
303
- .catch((err) => {
304
-
305
- return null
306
-
307
- })
308
-
309
- if (!userResponse) return null
310
-
311
-
312
-
313
- const authUser = userResponse.data
314
-
315
- if (authUser) {
316
-
317
- commit('setUser', authUser)
318
-
319
- return authUser
227
+ import Vue from 'vue'
228
+
229
+ import Router from 'vue-router'
230
+
231
+ import store from '../store'
232
+
233
+
234
+
235
+ import ItemIndex from '../pages/item/index'
236
+
237
+ import RegisterIndex from '../pages/register/index'
238
+
239
+ import LoginIndex from '../pages/login/index'
240
+
241
+ import DeleteListIndex from '../pages/delete_list/index'
242
+
243
+ import NotFound from '../components/shared/NotFound'
244
+
245
+
246
+
247
+ Vue.use(Router)
248
+
249
+
250
+
251
+ const router = new Router({
252
+
253
+ mode: 'history',
254
+
255
+ routes: [
256
+
257
+ {
258
+
259
+ path: '/',
260
+
261
+ component: ItemIndex,
262
+
263
+ name: 'ItemIndex',
264
+
265
+ meta: { requiredAuth: true },
266
+
267
+ },
268
+
269
+ {
270
+
271
+ path: '/register',
272
+
273
+ component: RegisterIndex,
274
+
275
+ name: 'RegisterIndex',
276
+
277
+ },
278
+
279
+ {
280
+
281
+ path: '/login',
282
+
283
+ component: LoginIndex,
284
+
285
+ name: 'LoginIndex',
286
+
287
+ },
288
+
289
+ {
290
+
291
+ path: '/delete-list',
292
+
293
+ component: DeleteListIndex,
294
+
295
+ name: 'DeliteListIndex',
296
+
297
+ meta: { requiredAuth: true }
298
+
299
+ },
300
+
301
+ {
302
+
303
+ path: '*',
304
+
305
+ component: NotFound,
306
+
307
+ name: 'NotFound'
308
+
309
+ }
310
+
311
+ ],
312
+
313
+ })
314
+
315
+
316
+
317
+ router.beforeEach((to, from, next) => {
318
+
319
+ store.dispatch('users/fetchAuthUser').then((authUser) => {
320
+
321
+ if (to.matched.some(record => record.meta.requiredAuth) && !authUser) {
322
+
323
+ next({ name: 'LoginIndex' });
320
324
 
321
325
  } else {
322
326
 
323
- commit('setUser', null)
324
-
325
- return null
327
+ next();
326
328
 
327
329
  }
328
330
 
329
- },
331
+ })
330
-
332
+
331
- }
333
+ });
332
-
333
-
334
-
334
+
335
+
336
+
335
- export default {
337
+ export default router
336
-
337
- namespaced: true,
338
-
339
- state,
340
-
341
- getters,
342
-
343
- mutations,
344
-
345
- actions
346
-
347
- }
348
338
 
349
339
  ```
350
340
 
351
341
 
352
342
 
353
- controllers/api/users_controllers
354
-
355
- ```
356
-
357
- class Api::UsersController < ApplicationController
358
-
359
- before_action :authenticate!, only: %i[me]
360
-
361
-
362
-
363
- def create
364
-
365
- user = User.new(user_params)
366
-
367
-
368
-
369
- if user.save
370
-
371
- render json: user
372
-
373
- else
374
-
375
- render json: user.errors, status: :bad_request
376
-
377
- end
378
-
379
- end
380
-
381
-
382
-
383
- def me
384
-
385
- render json: current_user
386
-
387
- end
388
-
389
-
390
-
391
- private
392
-
393
-
394
-
395
- def user_params
396
-
397
- params.require(:user).permit(:email, :password, :password_confirmation)
398
-
399
- end
400
-
401
- end
402
-
403
-
404
-
405
- ```
406
-
407
-
408
-
409
- javascript/router/index.js
410
-
411
- ```
412
-
413
- import Vue from 'vue'
414
-
415
- import Router from 'vue-router'
416
-
417
- import store from '../store'
418
-
419
-
420
-
421
- import ItemIndex from '../pages/item/index'
422
-
423
- import RegisterIndex from '../pages/register/index'
424
-
425
- import LoginIndex from '../pages/login/index'
426
-
427
- import DeleteListIndex from '../pages/delete_list/index'
428
-
429
- import NotFound from '../components/shared/NotFound'
430
-
431
-
432
-
433
- Vue.use(Router)
434
-
435
-
436
-
437
- const router = new Router({
438
-
439
- mode: 'history',
440
-
441
- routes: [
442
-
443
- {
444
-
445
- path: '/',
446
-
447
- component: ItemIndex,
448
-
449
- name: 'ItemIndex',
450
-
451
- meta: { requiredAuth: true },
452
-
453
- },
454
-
455
- {
456
-
457
- path: '/register',
458
-
459
- component: RegisterIndex,
460
-
461
- name: 'RegisterIndex',
462
-
463
- },
464
-
465
- {
466
-
467
- path: '/login',
468
-
469
- component: LoginIndex,
470
-
471
- name: 'LoginIndex',
472
-
473
- },
474
-
475
- {
476
-
477
- path: '/delete-list',
478
-
479
- component: DeleteListIndex,
480
-
481
- name: 'DeliteListIndex',
482
-
483
- meta: { requiredAuth: true }
484
-
485
- },
486
-
487
- {
488
-
489
- path: '*',
490
-
491
- component: NotFound,
492
-
493
- name: 'NotFound'
494
-
495
- }
496
-
497
- ],
498
-
499
- })
500
-
501
-
502
-
503
- router.beforeEach((to, from, next) => {
504
-
505
- store.dispatch('users/fetchAuthUser').then((authUser) => {
506
-
507
- if (to.matched.some(record => record.meta.requiredAuth) && !authUser) {
508
-
509
- next({ name: 'LoginIndex' });
510
-
511
- } else {
512
-
513
- next();
514
-
515
- }
516
-
517
- })
518
-
519
- });
520
-
521
-
522
-
523
- export default router
524
-
525
- ```
526
-
527
-
528
-
529
343
 
530
344
 
531
345
  ご教授お願いします

1

ファイルの追加

2020/11/09 02:12

投稿

anguraaaa
anguraaaa

スコア21

test CHANGED
File without changes
test CHANGED
@@ -8,6 +8,8 @@
8
8
 
9
9
  URLが /register?に飛んでしまいます
10
10
 
11
+ vuexにてaxiosを使用してサーバーへリクエストを送っています。
12
+
11
13
  [画像](https://gyazo.com/2eeb56189eec7d7dcb86902e4c523d49)
12
14
 
13
15
 
@@ -218,6 +220,136 @@
218
220
 
219
221
 
220
222
 
223
+
224
+
225
+ javascript/store/modules/users.js
226
+
227
+ ```
228
+
229
+ import axios from '../../plugins/axios'
230
+
231
+
232
+
233
+ const state = {
234
+
235
+ authUser: null
236
+
237
+ }
238
+
239
+
240
+
241
+ const getters = {
242
+
243
+ authUser: state => state.authUser
244
+
245
+ }
246
+
247
+
248
+
249
+ const mutations = {
250
+
251
+ setUser: (state, user) => {
252
+
253
+ state.authUser = user
254
+
255
+ }
256
+
257
+ }
258
+
259
+
260
+
261
+ const actions = {
262
+
263
+ async loginUser({ commit }, user) {
264
+
265
+ const sessionsResponse = await axios.post('sessions', user)
266
+
267
+ localStorage.auth_token = sessionsResponse.data.token
268
+
269
+ axios.defaults.headers.common['Authorization'] = `Bearer ${localStorage.auth_token}`
270
+
271
+
272
+
273
+ const userResponce = await axios.get('users/me')
274
+
275
+ commit('setUser', userResponce.date)
276
+
277
+ },
278
+
279
+
280
+
281
+ logoutUser({ commit }) {
282
+
283
+ localStorage.removeItem('auth_token')
284
+
285
+ axios.defaults.headers.common['Authorization'] = ''
286
+
287
+ commit('setUser', null)
288
+
289
+ },
290
+
291
+
292
+
293
+ async fetchAuthUser({ commit, state }) {
294
+
295
+ if (!localStorage.auth_token) return null
296
+
297
+ if (state.authUser) return state.authUser
298
+
299
+
300
+
301
+ const userResponse = await axios.get('users/me')
302
+
303
+ .catch((err) => {
304
+
305
+ return null
306
+
307
+ })
308
+
309
+ if (!userResponse) return null
310
+
311
+
312
+
313
+ const authUser = userResponse.data
314
+
315
+ if (authUser) {
316
+
317
+ commit('setUser', authUser)
318
+
319
+ return authUser
320
+
321
+ } else {
322
+
323
+ commit('setUser', null)
324
+
325
+ return null
326
+
327
+ }
328
+
329
+ },
330
+
331
+ }
332
+
333
+
334
+
335
+ export default {
336
+
337
+ namespaced: true,
338
+
339
+ state,
340
+
341
+ getters,
342
+
343
+ mutations,
344
+
345
+ actions
346
+
347
+ }
348
+
349
+ ```
350
+
351
+
352
+
221
353
  controllers/api/users_controllers
222
354
 
223
355
  ```