|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yab\FlightDeck\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use Illuminate\Routing\Controller; |
|
7
|
|
|
use Illuminate\Support\Facades\Auth; |
|
8
|
|
|
use Yab\FlightDeck\Http\Requests\LoginRequest; |
|
9
|
|
|
|
|
10
|
|
|
class AuthController extends Controller |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Get a JWT via given credentials. |
|
14
|
|
|
* |
|
15
|
|
|
* @param LoginRequest $request |
|
16
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
17
|
|
|
*/ |
|
18
|
|
|
public function store(LoginRequest $request) |
|
19
|
|
|
{ |
|
20
|
|
|
$credentials = $request->only(['email', 'password']); |
|
21
|
|
|
|
|
22
|
|
|
if (! $token = $this->guard()->attempt($credentials)) { |
|
23
|
|
|
return response()->json(['error' => 'Unauthorized'], 401); |
|
|
|
|
|
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
return $this->respondWithToken($token); |
|
|
|
|
|
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Log the user out (Invalidate the token). |
|
31
|
|
|
* |
|
32
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
33
|
|
|
*/ |
|
34
|
|
|
public function destroy() |
|
35
|
|
|
{ |
|
36
|
|
|
$this->guard()->logout(); |
|
37
|
|
|
|
|
38
|
|
|
return response()->json(['message' => 'Successfully logged out']); |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Refresh a token. |
|
43
|
|
|
* |
|
44
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
45
|
|
|
*/ |
|
46
|
|
|
public function update() |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->respondWithToken($this->guard()->refresh()); |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get the token array structure. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $token |
|
55
|
|
|
* |
|
56
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function respondWithToken($token) |
|
59
|
|
|
{ |
|
60
|
|
|
return response()->json([ |
|
|
|
|
|
|
61
|
|
|
'access_token' => $token, |
|
62
|
|
|
'token_type' => 'bearer', |
|
63
|
|
|
'expires_in' => $this->guard()->factory()->getTTL() * 60 |
|
|
|
|
|
|
64
|
|
|
]); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get the guard to be used for login, logout and token refreshes. |
|
69
|
|
|
* |
|
70
|
|
|
* @return \Illuminate\Contracts\Auth\StatefulGuard |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function guard() |
|
73
|
|
|
{ |
|
74
|
|
|
return auth()->guard(); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
} |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: