質問編集履歴
5
一部修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -422,7 +422,7 @@
|
|
422
422
|
|
423
423
|
```php
|
424
424
|
|
425
|
-
/app/Http/Resources/User.php
|
425
|
+
//app/Http/Resources/User.php
|
426
426
|
|
427
427
|
|
428
428
|
|
4
一部修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -328,7 +328,7 @@
|
|
328
328
|
|
329
329
|
```php
|
330
330
|
|
331
|
-
/app/Http/Controllers/UserController.php
|
331
|
+
//app/Http/Controllers/UserController.php
|
332
332
|
|
333
333
|
namespace App\Http\Controllers;
|
334
334
|
|
3
一部文字修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -250,7 +250,7 @@
|
|
250
250
|
|
251
251
|
```php
|
252
252
|
|
253
|
-
/app/Http/Controllers/AuthController.php
|
253
|
+
//app/Http/Controllers/AuthController.php
|
254
254
|
|
255
255
|
namespace App\Http\Controllers;
|
256
256
|
|
2
コード追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -185,3 +185,385 @@
|
|
185
185
|
</script>
|
186
186
|
|
187
187
|
```
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
# Laravelコード
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
```php
|
196
|
+
|
197
|
+
//app/User.php
|
198
|
+
|
199
|
+
namespace App;
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
use Tymon\JWTAuth\Contracts\JWTSubject;
|
204
|
+
|
205
|
+
use Illuminate\Notifications\Notifiable;
|
206
|
+
|
207
|
+
use Illuminate\Foundation\Auth\User as Authenticatable;
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
class User extends Authenticatable implements JWTSubject
|
212
|
+
|
213
|
+
{
|
214
|
+
|
215
|
+
use Notifiable;
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
protected $fillable = ['name', 'email', 'password'];
|
220
|
+
|
221
|
+
protected $guarded = ['id' ];
|
222
|
+
|
223
|
+
protected $hidden = ['remember_token',];
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
public function getJWTIdentifier()
|
228
|
+
|
229
|
+
{
|
230
|
+
|
231
|
+
return $this->getKey();
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
public function getJWTCustomClaims()
|
238
|
+
|
239
|
+
{
|
240
|
+
|
241
|
+
return [];
|
242
|
+
|
243
|
+
}
|
244
|
+
|
245
|
+
}
|
246
|
+
|
247
|
+
```
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
```php
|
252
|
+
|
253
|
+
/app/Http/Controllers/AuthController.php
|
254
|
+
|
255
|
+
namespace App\Http\Controllers;
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
class AuthController extends Controller
|
260
|
+
|
261
|
+
{
|
262
|
+
|
263
|
+
function login() {
|
264
|
+
|
265
|
+
$credentials = request(['email', 'password']);
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
if (!$token = auth('api')->attempt($credentials)) {
|
270
|
+
|
271
|
+
return response()->json(['error' => 'Unauthorized'], 401);
|
272
|
+
|
273
|
+
}
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
return $this->respondWithToken($token);
|
278
|
+
|
279
|
+
}
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
public function logout()
|
284
|
+
|
285
|
+
{
|
286
|
+
|
287
|
+
auth()->logout();
|
288
|
+
|
289
|
+
return response()->json(['message' => 'ログアウトしました。']);
|
290
|
+
|
291
|
+
}
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
public function me()
|
296
|
+
|
297
|
+
{
|
298
|
+
|
299
|
+
return response()->json(auth()->user());
|
300
|
+
|
301
|
+
}
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
protected function respondWithToken($token)
|
306
|
+
|
307
|
+
{
|
308
|
+
|
309
|
+
return response()->json([
|
310
|
+
|
311
|
+
'access_token' => $token,
|
312
|
+
|
313
|
+
'token_type' => 'bearer',
|
314
|
+
|
315
|
+
'expires_in' => auth("api")->factory()->getTTL() * 60
|
316
|
+
|
317
|
+
]);
|
318
|
+
|
319
|
+
}
|
320
|
+
|
321
|
+
}
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
```
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
```php
|
330
|
+
|
331
|
+
/app/Http/Controllers/UserController.php
|
332
|
+
|
333
|
+
namespace App\Http\Controllers;
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
use App\User;
|
338
|
+
|
339
|
+
use Illuminate\Http\Request;
|
340
|
+
|
341
|
+
use App\Http\Resources\User AS UserResource;
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
class UserController extends Controller
|
346
|
+
|
347
|
+
{
|
348
|
+
|
349
|
+
// 一覧表示
|
350
|
+
|
351
|
+
public function index() {
|
352
|
+
|
353
|
+
return UserResource::collection(User::all());
|
354
|
+
|
355
|
+
}
|
356
|
+
|
357
|
+
|
358
|
+
|
359
|
+
// 保存
|
360
|
+
|
361
|
+
public function store(Request $request) {
|
362
|
+
|
363
|
+
$user = new User;
|
364
|
+
|
365
|
+
$user->name = $request->input('name');
|
366
|
+
|
367
|
+
$user->email = $request->input('email');
|
368
|
+
|
369
|
+
// bcrypt関数でハッシュ化してくれる
|
370
|
+
|
371
|
+
$user->password = bcrypt($request->input('password'));
|
372
|
+
|
373
|
+
|
374
|
+
|
375
|
+
$user->save();
|
376
|
+
|
377
|
+
}
|
378
|
+
|
379
|
+
|
380
|
+
|
381
|
+
// 1データ表示
|
382
|
+
|
383
|
+
public function show(User $user) {
|
384
|
+
|
385
|
+
return new UserResource($user);
|
386
|
+
|
387
|
+
}
|
388
|
+
|
389
|
+
|
390
|
+
|
391
|
+
// 更新
|
392
|
+
|
393
|
+
public function update(Request $request, User $user) {
|
394
|
+
|
395
|
+
$user->name = $request->input('name');
|
396
|
+
|
397
|
+
$user->email = $request->input('email');
|
398
|
+
|
399
|
+
// bcrypt関数でハッシュ化してくれる
|
400
|
+
|
401
|
+
$user->password = bcrypt($request->input('password'));
|
402
|
+
|
403
|
+
$user->save();
|
404
|
+
|
405
|
+
}
|
406
|
+
|
407
|
+
|
408
|
+
|
409
|
+
// 削除
|
410
|
+
|
411
|
+
public function destroy(User $user) {
|
412
|
+
|
413
|
+
$user->delete();
|
414
|
+
|
415
|
+
}
|
416
|
+
|
417
|
+
}
|
418
|
+
|
419
|
+
```
|
420
|
+
|
421
|
+
|
422
|
+
|
423
|
+
```php
|
424
|
+
|
425
|
+
/app/Http/Resources/User.php
|
426
|
+
|
427
|
+
|
428
|
+
|
429
|
+
namespace App\Http\Resources;
|
430
|
+
|
431
|
+
|
432
|
+
|
433
|
+
use Illuminate\Http\Resources\Json\Resource;
|
434
|
+
|
435
|
+
|
436
|
+
|
437
|
+
class User extends Resource
|
438
|
+
|
439
|
+
{
|
440
|
+
|
441
|
+
public function toArray($request)
|
442
|
+
|
443
|
+
{
|
444
|
+
|
445
|
+
return [
|
446
|
+
|
447
|
+
'id' => $this->id,
|
448
|
+
|
449
|
+
'name' => $this->name,
|
450
|
+
|
451
|
+
'email' => $this->email,
|
452
|
+
|
453
|
+
'password' => $this->password,
|
454
|
+
|
455
|
+
'date' => $this->created_at->format('Y-m-d H:i:s'),
|
456
|
+
|
457
|
+
];
|
458
|
+
|
459
|
+
}
|
460
|
+
|
461
|
+
}
|
462
|
+
|
463
|
+
```
|
464
|
+
|
465
|
+
|
466
|
+
|
467
|
+
```php
|
468
|
+
|
469
|
+
//routes/api.php
|
470
|
+
|
471
|
+
|
472
|
+
|
473
|
+
use Illuminate\Http\Request;
|
474
|
+
|
475
|
+
|
476
|
+
|
477
|
+
Route::middleware('auth:api')->get('/user', function (Request $request) {
|
478
|
+
|
479
|
+
return $request->user();
|
480
|
+
|
481
|
+
});
|
482
|
+
|
483
|
+
|
484
|
+
|
485
|
+
Route::post('/login', 'AuthController@login');
|
486
|
+
|
487
|
+
|
488
|
+
|
489
|
+
Route::group(['middleware' => 'auth:api'], function () {
|
490
|
+
|
491
|
+
Route::get('/me', 'AuthController@me');
|
492
|
+
|
493
|
+
Route::post('/logout', 'AuthController@logout');
|
494
|
+
|
495
|
+
});
|
496
|
+
|
497
|
+
|
498
|
+
|
499
|
+
Route::resource('users', 'UserController');
|
500
|
+
|
501
|
+
```
|
502
|
+
|
503
|
+
|
504
|
+
|
505
|
+
```php
|
506
|
+
|
507
|
+
//routes/web.php
|
508
|
+
|
509
|
+
|
510
|
+
|
511
|
+
Route::get('/home', 'HomeController@index')->name('home');
|
512
|
+
|
513
|
+
|
514
|
+
|
515
|
+
Route::any('{all}', function () {
|
516
|
+
|
517
|
+
return view('app');
|
518
|
+
|
519
|
+
})->where(['all' => '.*']);
|
520
|
+
|
521
|
+
```
|
522
|
+
|
523
|
+
|
524
|
+
|
525
|
+
```php
|
526
|
+
|
527
|
+
//resources/views/app.blade.php
|
528
|
+
|
529
|
+
|
530
|
+
|
531
|
+
<!doctype html>
|
532
|
+
|
533
|
+
<html lang="ja">
|
534
|
+
|
535
|
+
<head>
|
536
|
+
|
537
|
+
<meta charset="utf-8">
|
538
|
+
|
539
|
+
<title>Job+</title>
|
540
|
+
|
541
|
+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
542
|
+
|
543
|
+
<link href="{{ mix('/css/app.css') }}" rel="stylesheet">
|
544
|
+
|
545
|
+
<link href="{{ asset('/css/clearfix.css') }}" rel="stylesheet">
|
546
|
+
|
547
|
+
<link href="{{ asset('/css/style.css') }}" rel="stylesheet">
|
548
|
+
|
549
|
+
<meta name="csrf-token" content="{{ csrf_token() }}">
|
550
|
+
|
551
|
+
</head>
|
552
|
+
|
553
|
+
<body>
|
554
|
+
|
555
|
+
<div id="app">
|
556
|
+
|
557
|
+
<app></app>
|
558
|
+
|
559
|
+
<router-view></router-view>
|
560
|
+
|
561
|
+
</div>
|
562
|
+
|
563
|
+
<script src="{{ mix('/js/app.js') }}"></script>
|
564
|
+
|
565
|
+
</body>
|
566
|
+
|
567
|
+
</html>
|
568
|
+
|
569
|
+
```
|
1
誤字修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -10,7 +10,7 @@
|
|
10
10
|
|
11
11
|
Vue
|
12
12
|
|
13
|
-
ログインにはjt
|
13
|
+
ログインにはjwt-auth使用 [参考](https://www.webopixel.net/php/1444.html)
|
14
14
|
|
15
15
|
|
16
16
|
|