commit | author | age
|
9f6455
|
1 |
<?php |
DC |
2 |
|
|
3 |
namespace App\Http\Controllers\Auth; |
|
4 |
|
|
5 |
use App\Http\Controllers\Controller; |
|
6 |
|
|
7 |
class ApiAuthController extends Controller |
|
8 |
{ |
|
9 |
/** |
|
10 |
* Create a new AuthController instance. |
|
11 |
* |
|
12 |
* @return void |
|
13 |
*/ |
|
14 |
public function __construct() |
|
15 |
{ |
|
16 |
$this->middleware(['api','jwt.auth'], ['except' => ['login']]); |
|
17 |
} |
|
18 |
|
|
19 |
/** |
|
20 |
* Get a JWT via given credentials. |
|
21 |
* |
|
22 |
* @return \Illuminate\Http\JsonResponse |
|
23 |
*/ |
|
24 |
public function login() |
|
25 |
{ |
|
26 |
$credentials = request(['username', 'password']); |
|
27 |
|
|
28 |
if (! $token = auth()->attempt($credentials)) { |
|
29 |
return response()->json(['error' => 'invalid_credentials'], 401); |
|
30 |
} |
|
31 |
|
|
32 |
return $this->respondWithToken($token); |
|
33 |
} |
|
34 |
|
|
35 |
/** |
|
36 |
* Get the authenticated User. |
|
37 |
* |
|
38 |
* @return \Illuminate\Http\JsonResponse |
|
39 |
*/ |
|
40 |
public function me() |
|
41 |
{ |
|
42 |
return response()->json(auth()->user()); |
|
43 |
} |
|
44 |
|
|
45 |
/** |
|
46 |
* Log the user out (Invalidate the token). |
|
47 |
* |
|
48 |
* @return \Illuminate\Http\JsonResponse |
|
49 |
*/ |
|
50 |
public function logout() |
|
51 |
{ |
|
52 |
auth()->logout(); |
|
53 |
|
|
54 |
return response()->json(['message' => 'Successfully logged out']); |
|
55 |
} |
|
56 |
|
|
57 |
/** |
|
58 |
* Refresh a token. |
|
59 |
* |
|
60 |
* @return \Illuminate\Http\JsonResponse |
|
61 |
*/ |
|
62 |
public function refresh() |
|
63 |
{ |
|
64 |
return $this->respondWithToken(auth()->refresh()); |
|
65 |
} |
|
66 |
|
|
67 |
/** |
|
68 |
* Get the token array structure. |
|
69 |
* |
|
70 |
* @param string $token |
|
71 |
* |
|
72 |
* @return \Illuminate\Http\JsonResponse |
|
73 |
*/ |
|
74 |
protected function respondWithToken($token) |
|
75 |
{ |
|
76 |
return response()->json([ |
|
77 |
'token' => $token |
|
78 |
]); |
|
79 |
} |
|
80 |
} |