質問編集履歴

6

syusei

2022/03/13 12:56

投稿

tkoide1213
tkoide1213

スコア7

test CHANGED
File without changes
test CHANGED
@@ -93,7 +93,7 @@
93
93
  public function login(Request $request): JsonResponse
94
94
  {
95
95
  $credentials = $request->validate([
96
- 'EM_FIKSID' => 'required',
96
+ 'id => 'required',
97
97
  'password' => 'required',
98
98
  ]);
99
99
 

5

修正

2022/03/13 12:52

投稿

tkoide1213
tkoide1213

スコア7

test CHANGED
File without changes
test CHANGED
@@ -88,3 +88,45 @@
88
88
  https://yutaro-blog.net/2021/09/07/nextjs-laravel-sanctum-spa/
89
89
  - 419のtoken mismatch が出た時
90
90
  https://stackoverflow.com/questions/58910214/react-to-laravel-csrf-token-mismatch-error-not-working-even-after-trying-lot
91
+
92
+ ```LoginController.php
93
+ public function login(Request $request): JsonResponse
94
+ {
95
+ $credentials = $request->validate([
96
+ 'EM_FIKSID' => 'required',
97
+ 'password' => 'required',
98
+ ]);
99
+
100
+ $credentials = $request->only('EM_FIKSID', 'password');
101
+
102
+ if (Auth::attempt($credentials)) {
103
+ $request->session()->regenerate();
104
+ return response()->json(Auth::user());
105
+ }
106
+
107
+ throw new Exception('ログインに失敗しました。再度お試しください');
108
+ }
109
+ ```
110
+ ```pages/login.js
111
+ const handleClick = () => {
112
+ const Data = { params: { 'id': id, 'password': password }}
113
+ axios
114
+ // CSRF保護の初期化
115
+ .get('http://127.0.0.1:8000/sanctum/csrf-cookie')
116
+ .then((res) => {
117
+      // ログイン処理
118
+ console.log(res)
119
+ axios
120
+ .post(
121
+ 'http://127.0.0.1:8000/login',
122
+ Data,
123
+ )
124
+ .then((res) => {
125
+ console.log(res)
126
+ })
127
+ .catch((err) => {
128
+ console.log(err)
129
+ })
130
+ })
131
+ }
132
+ ```

4

修正

2022/03/09 09:07

投稿

tkoide1213
tkoide1213

スコア7

test CHANGED
File without changes
test CHANGED
@@ -56,11 +56,33 @@
56
56
  \Illuminate\Routing\Middleware\SubstituteBindings::class,
57
57
  ],
58
58
  ```
59
+ ```Controllers/Auth/Logincontroller.php
60
+ public function login(Request $request): JsonResponse
61
+ {
62
+ $credentials = $request->validate([
63
+ $this->username() => 'required',
64
+ 'password' => 'required',
65
+ ]);
66
+
67
+ $credentials = $request->only($this->username(), 'password');
68
+
69
+ if (Auth::attempt($credentials)) {
70
+ $request->session()->regenerate();
71
+ return response()->json(Auth::user());
72
+ }
73
+
74
+ throw new Exception('ログインに失敗しました。再度お試しください');
75
+ }
76
+ ```
59
- ### 現在のエラー
77
+ ### 当初のエラー
60
78
  ```
61
79
  local.ERROR: Session store not set on request. {"exception":"[object] (RuntimeException(code: 0): Session store not set on request
62
80
  ```
63
-
81
+ ### 現在のエラー(ブラウザのコンソール)
82
+ ```
83
+ {message: "CSRF token mismatch."}
84
+  message: "CSRF token mismatch."
85
+ ```
64
86
  ### 記事
65
87
  - sanctum導入時
66
88
  https://yutaro-blog.net/2021/09/07/nextjs-laravel-sanctum-spa/

3

修正

2022/03/09 03:33

投稿

tkoide1213
tkoide1213

スコア7

test CHANGED
File without changes
test CHANGED
@@ -1,7 +1,7 @@
1
1
  Next.js Larvelを使用してSPAを開発しています。
2
- Laravel側ではREST APIで値受け渡しをしております。
2
+ LaravelはAPIサーバーとしてREST APIで値受け渡しをしております。
3
- GETでの値取得はできましたが、POST、PUT、DELETEといった処理がうまくできません。
3
+ GETでの値取得はできましたが、POST、PUT、DELETEといった処理がうまく動作しません。
4
- Laraveは8.1使用しています。
4
+ Laraveはのバージョンは8.1使用しています。
5
5
  また、ログイン認証関連でsanctumを使用しました。
6
6
  この辺も影響しているのかな?とも思います。
7
7
  ご教授いただけますと幸いです。

2

文字

2022/03/09 03:16

投稿

tkoide1213
tkoide1213

スコア7

test CHANGED
File without changes
test CHANGED
@@ -29,7 +29,7 @@
29
29
  ```
30
30
  ### Laravel側
31
31
  ```routes/api.php
32
- Route::apiResource('test', testController::class);
32
+ Route::apiResource('test', TestController::class);
33
33
 
34
34
  ```
35
35
 

1

レイアウト

2022/03/09 03:15

投稿

tkoide1213
tkoide1213

スコア7

test CHANGED
File without changes
test CHANGED
@@ -7,7 +7,7 @@
7
7
  ご教授いただけますと幸いです。
8
8
 
9
9
 
10
- ### Next側 
10
+ ### Next側 
11
11
  ```front/pages/top.js
12
12
  const Axios = axios.create({
13
13
  xsrfHeaderName: 'X-CSRF-Token',