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 DoeSangue\Http\Requests\RegisterUserRequest; |
10
|
|
|
use DoeSangue\Mail\UserCreated; |
11
|
|
|
use Illuminate\Support\Facades\Mail; |
12
|
|
|
use DoeSangue\Models\User; |
13
|
|
|
|
14
|
|
|
class AuthenticateController extends Controller |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Authenticate the user |
18
|
|
|
* |
19
|
|
|
* @param Request $request |
20
|
|
|
* @return \Illuminate\Http\JsonResponse |
21
|
|
|
*/ |
22
|
2 |
|
public function authenticate(Request $request) |
23
|
|
|
{ |
24
|
|
|
// grab credentials from the request |
25
|
2 |
|
$credentials = $request->only('email', 'password'); |
26
|
|
|
|
27
|
|
|
try { |
28
|
|
|
// attempt to verify the credentials and create a token for the user |
29
|
2 |
|
if (!$token = JWTAuth::attempt($credentials)) { |
30
|
1 |
|
return response()->json([ 'error' => 'invalid_credentials' ], 401); |
31
|
|
|
} |
32
|
|
|
} catch (JWTException $e) { |
33
|
|
|
// something went wrong whilst attempting to encode the token |
34
|
|
|
return response()->json([ 'error' => 'could_not_create_token' ], 500); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// all good so return the token |
38
|
1 |
|
return response()->json( |
39
|
|
|
[ |
40
|
1 |
|
'access_token' => $token, |
41
|
1 |
|
'token_type' => 'Bearer' |
42
|
1 |
|
], 200 |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Register a new User |
48
|
|
|
* |
49
|
|
|
* @param RegisterUserRequest $request |
50
|
|
|
* @return \Illuminate\Http\JsonResponse |
51
|
|
|
*/ |
52
|
1 |
|
public function register(RegisterUserRequest $request) |
53
|
|
|
{ |
54
|
1 |
|
$user = User::create( |
55
|
|
|
[ |
56
|
1 |
|
'first_name' => $request->first_name, |
|
|
|
|
57
|
1 |
|
'last_name' => $request->last_name, |
|
|
|
|
58
|
1 |
|
'email' => $request->email, |
|
|
|
|
59
|
1 |
|
'username' => $request->username, |
|
|
|
|
60
|
1 |
|
'phone' => $request->phone, |
|
|
|
|
61
|
1 |
|
'bio' => $request->bio, |
|
|
|
|
62
|
1 |
|
'blood_type_id' => $request->blood_type_id, |
|
|
|
|
63
|
1 |
|
'birthdate' => $request->birthdate, |
|
|
|
|
64
|
1 |
|
'password' => bcrypt($request->password), |
|
|
|
|
65
|
|
|
] |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
// Send mail to user |
69
|
1 |
|
Mail::to($user->email)->send(new UserCreated($user)); |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
|
73
|
1 |
|
$token = JWTAuth::attempt($request->only('email', 'password')); |
74
|
|
|
|
75
|
|
|
// all good so return the token |
76
|
1 |
|
return response()->json( |
77
|
|
|
[ |
78
|
1 |
|
'access_token' => $token, |
79
|
1 |
|
'token_type' => 'Bearer' |
80
|
1 |
|
], 201 |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Invalidate and log out the user |
86
|
|
|
* |
87
|
|
|
* @return void |
88
|
|
|
* |
89
|
|
|
*/ |
90
|
|
|
public function logout() |
91
|
|
|
{ |
92
|
|
|
// |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.