1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DoeSangue\Http\Controllers\Auth; |
4
|
|
|
|
5
|
|
|
use JWTAuth; |
6
|
|
|
use Tymon\JWTAuth\Exceptions\JWTException; |
7
|
|
|
use DoeSangue\Http\Controllers\Controller; |
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
use Carbon\Carbon; |
10
|
|
|
use DoeSangue\Http\Requests\RegisterUserRequest; |
11
|
|
|
use DoeSangue\Mail\UserCreated; |
12
|
|
|
use Illuminate\Support\Facades\Mail; |
13
|
|
|
use DoeSangue\Models\User; |
14
|
|
|
|
15
|
|
|
class AuthenticateController extends Controller |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Authenticate the user |
19
|
|
|
* |
20
|
|
|
* @param Request $request |
21
|
|
|
* @return \Illuminate\Http\JsonResponse |
22
|
|
|
*/ |
23
|
2 |
|
public function authenticate(Request $request) |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
try { |
27
|
|
|
|
28
|
|
|
// grab credentials from the request |
29
|
|
|
// attempt to verify the credentials and create a token for the user |
30
|
2 |
|
if (!$token = JWTAuth::attempt( |
31
|
2 |
|
$request->only('email', 'password'), [ |
32
|
2 |
|
'exp' => Carbon::now()->addWeek()->timestamp, |
33
|
|
|
] |
34
|
|
|
) |
35
|
|
|
) { |
36
|
2 |
|
return response()->json([ 'error' => 'invalid_credentials' ], 401); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
} catch (JWTException $e) { |
39
|
|
|
// something went wrong whilst attempting to encode the token |
40
|
|
|
return response()->json([ 'error' => 'could_not_create_token' ], 500); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// all good so return the token |
44
|
1 |
|
return response()->json( |
45
|
|
|
[ |
46
|
1 |
|
'access_token' => $token, |
47
|
1 |
|
'token_type' => 'Bearer' |
48
|
1 |
|
], 200 |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Register a new User |
54
|
|
|
* |
55
|
|
|
* @param RegisterUserRequest $request |
56
|
|
|
* @return \Illuminate\Http\JsonResponse |
57
|
|
|
*/ |
58
|
|
|
public function register(RegisterUserRequest $request) |
59
|
|
|
{ |
60
|
|
|
$user = User::create( |
61
|
|
|
[ |
62
|
|
|
'first_name' => $request->first_name, |
|
|
|
|
63
|
|
|
'last_name' => $request->last_name, |
|
|
|
|
64
|
|
|
'email' => $request->email, |
|
|
|
|
65
|
|
|
'username' => $request->username, |
|
|
|
|
66
|
|
|
'phone' => $request->phone, |
|
|
|
|
67
|
|
|
'country_code' => $request->country_code, |
|
|
|
|
68
|
|
|
'bio' => $request->bio, |
|
|
|
|
69
|
|
|
'blood_type_id' => $request->blood_type_id, |
|
|
|
|
70
|
|
|
'birthdate' => $request->birthdate, |
|
|
|
|
71
|
|
|
'password' => bcrypt($request->password), |
|
|
|
|
72
|
|
|
] |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
// Send mail to user |
76
|
|
|
Mail::to($user->email)->send(new UserCreated($user)); |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
|
80
|
|
|
$token = JWTAuth::attempt($request->only('email', 'password')); |
81
|
|
|
|
82
|
|
|
// all good so return the token |
83
|
|
|
return response()->json( |
|
|
|
|
84
|
|
|
[ |
85
|
|
|
'access_token' => $token, |
86
|
|
|
'token_type' => 'Bearer' |
87
|
|
|
], 201 |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Invalidate and log out the user |
93
|
|
|
* |
94
|
|
|
* @param Request $request |
95
|
|
|
* @return \Illuminate\Http\JsonResponse |
96
|
|
|
*/ |
97
|
|
|
public function logout(Request $request) |
98
|
|
|
{ |
99
|
|
|
$this->validate($request, [ 'token' => 'required' ]); |
100
|
|
|
|
101
|
|
|
try { |
102
|
|
|
JWTAuth::invalidate($request->input('token')); |
103
|
|
|
return response()->json( |
|
|
|
|
104
|
|
|
[ |
105
|
|
|
'success' => true |
106
|
|
|
] |
107
|
|
|
); |
108
|
|
|
} catch (JWTException $e) { |
109
|
|
|
// Something went wrong whilst attemping to encode the token |
110
|
|
|
return response()->json([ 'success' => false, 'error' => 'Failed to logout, please try again.', 500 ]); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
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: