質問編集履歴
4
リポジトリの削除
test
CHANGED
File without changes
|
test
CHANGED
@@ -18,14 +18,6 @@
|
|
18
18
|
|
19
19
|
|
20
20
|
|
21
|
-
### リポジトリ
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
https://github.com/taishi1116/laravel
|
26
|
-
|
27
|
-
|
28
|
-
|
29
21
|
お手数ですがreadmeを見て、環境構築していただき、下記の問題の解決策を提示いただけると幸いです。
|
30
22
|
|
31
23
|
|
3
説明の追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -46,7 +46,7 @@
|
|
46
46
|
|
47
47
|
- 会員登録画面で登録処理実施
|
48
48
|
|
49
|
-
- ログイン画面でログイン処理実施
|
49
|
+
- ログイン画面でログイン処理実施(この認証をlaravel sanctumで実施している)
|
50
50
|
|
51
51
|
- ホーム画面で`fetchLoginUser()`関数で`api/user`を叩くと401エラーとなる
|
52
52
|
|
2
リポジトリの説明追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -26,6 +26,10 @@
|
|
26
26
|
|
27
27
|
|
28
28
|
|
29
|
+
お手数ですがreadmeを見て、環境構築していただき、下記の問題の解決策を提示いただけると幸いです。
|
30
|
+
|
31
|
+
|
32
|
+
|
29
33
|
|
30
34
|
|
31
35
|
### 発生している問題・エラーメッセージ
|
@@ -292,6 +296,8 @@
|
|
292
296
|
|
293
297
|
|
294
298
|
|
299
|
+
|
300
|
+
|
295
301
|
### 試したこと
|
296
302
|
|
297
303
|
|
1
細かいコードを貼り付け
test
CHANGED
File without changes
|
test
CHANGED
@@ -60,6 +60,236 @@
|
|
60
60
|
|
61
61
|
|
62
62
|
|
63
|
+
### コード
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
### api.php
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
```
|
72
|
+
|
73
|
+
<?php
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
use Illuminate\Http\Request;
|
78
|
+
|
79
|
+
use Illuminate\Support\Facades\Route;
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
use App\Http\Controllers\TestAPIController;
|
84
|
+
|
85
|
+
use App\Http\Controllers\PreRegisterUserAPIController;
|
86
|
+
|
87
|
+
use App\Http\Controllers\VerifyTokenController;
|
88
|
+
|
89
|
+
use App\Http\Controllers\ArticleAPIController;
|
90
|
+
|
91
|
+
use App\Http\Controllers\UserAPIController;
|
92
|
+
|
93
|
+
use App\Http\Controllers\AuthAPIController;
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
use App\Models\Article;
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
Route::get('test', [TestAPIController::class,"index"]);
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
//仮会員登録
|
108
|
+
|
109
|
+
Route::post('/user/pre_register', [PreRegisterUserAPIController::class, 'store']);
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
// 仮会員登録のtoken確認
|
114
|
+
|
115
|
+
Route::get('/verify/{token}', [VerifyTokenController::class, 'index']);
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
// 認証
|
122
|
+
|
123
|
+
Route::post('/login', [AuthAPIController::class, 'login']);
|
124
|
+
|
125
|
+
Route::post('/logout', [AuthAPIController::class, 'logout']);
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
// 会員情報
|
130
|
+
|
131
|
+
Route::post('/user', [UserAPIController::class, 'store']);
|
132
|
+
|
133
|
+
Route::get('/user/{user_id}', [UserAPIController::class, 'show'])->middleware('auth:sanctum', 'loginUserCheck');
|
134
|
+
|
135
|
+
Route::put('/user/{user_id}', [UserAPIController::class, 'update'])->middleware('auth:sanctum', 'loginUserCheck');
|
136
|
+
|
137
|
+
Route::delete('/user/{user_id}', [UserAPIController::class, 'destroy'])->middleware('auth:sanctum', 'loginUserCheck');
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
// ユーザー情報の取得
|
144
|
+
|
145
|
+
Route::get('/user', function (Request $request) {
|
146
|
+
|
147
|
+
return $request->user();
|
148
|
+
|
149
|
+
})->middleware('auth:sanctum');
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
```
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
### AuthAPIController(ログインログアウト用のコントローラー)
|
158
|
+
|
159
|
+
```php
|
160
|
+
|
161
|
+
<?php
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
namespace App\Http\Controllers;
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
use Illuminate\Http\Request;
|
170
|
+
|
171
|
+
use App\Http\Requests\AuthRequest;
|
172
|
+
|
173
|
+
use App\Repositories\Auth\AuthRepository;
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
class AuthAPIController extends Controller
|
178
|
+
|
179
|
+
{
|
180
|
+
|
181
|
+
private $auth_repository;
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
public function __construct(AuthRepository $auth_repository)
|
186
|
+
|
187
|
+
{
|
188
|
+
|
189
|
+
$this->auth_repository = $auth_repository;
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
public function login(AuthRequest $request)
|
196
|
+
|
197
|
+
{
|
198
|
+
|
199
|
+
return $this->auth_repository->login($request);
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
public function logout(Request $request)
|
206
|
+
|
207
|
+
{
|
208
|
+
|
209
|
+
return $this->auth_repository->logout($request);
|
210
|
+
|
211
|
+
}
|
212
|
+
|
213
|
+
}
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
```
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
### AuthRepository(ログイン、ログアウト処理)
|
222
|
+
|
223
|
+
```php
|
224
|
+
|
225
|
+
<?php
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
namespace App\Repositories\Auth;
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
use Illuminate\Support\Facades\Auth;
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
class AuthRepository implements AuthInterface {
|
240
|
+
|
241
|
+
public function login($request){
|
242
|
+
|
243
|
+
$credentials = $request->validate([
|
244
|
+
|
245
|
+
'email' => ['required', 'email'],
|
246
|
+
|
247
|
+
'password' => ['required','min:8'],
|
248
|
+
|
249
|
+
]);
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
if(Auth::attempt($credentials)){
|
254
|
+
|
255
|
+
$request->session()->regenerate();
|
256
|
+
|
257
|
+
return response()->messageAndStatusCode('ログインが完了しました。', 200);
|
258
|
+
|
259
|
+
}
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
return response()->messageAndStatusCode('ユーザーが見つかりません。', 404);
|
264
|
+
|
265
|
+
}
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
public function logout($request){
|
270
|
+
|
271
|
+
Auth::logout();
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
$request->session()->invalidate();
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
$request->session()->regenerateToken();
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
return response()->messageAndStatusCode('ログアウトが完了しました', 200);
|
284
|
+
|
285
|
+
}
|
286
|
+
|
287
|
+
}
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
```
|
292
|
+
|
63
293
|
|
64
294
|
|
65
295
|
### 試したこと
|
@@ -74,6 +304,10 @@
|
|
74
304
|
|
75
305
|
|
76
306
|
|
307
|
+
|
308
|
+
|
309
|
+
|
310
|
+
|
77
311
|
### 補足情報(FW/ツールのバージョンなど)
|
78
312
|
|
79
313
|
|