|
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 |
|
'country_code' => $request->country_code, |
|
|
|
|
|
|
62
|
1 |
|
'bio' => $request->bio, |
|
|
|
|
|
|
63
|
1 |
|
'blood_type_id' => $request->blood_type_id, |
|
|
|
|
|
|
64
|
1 |
|
'birthdate' => $request->birthdate, |
|
|
|
|
|
|
65
|
1 |
|
'password' => bcrypt($request->password), |
|
|
|
|
|
|
66
|
|
|
] |
|
67
|
|
|
); |
|
68
|
|
|
|
|
69
|
|
|
// Send mail to user |
|
70
|
1 |
|
Mail::to($user->email)->send(new UserCreated($user)); |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
1 |
|
$token = JWTAuth::attempt($request->only('email', 'password')); |
|
75
|
|
|
|
|
76
|
|
|
// all good so return the token |
|
77
|
1 |
|
return response()->json( |
|
78
|
|
|
[ |
|
79
|
1 |
|
'access_token' => $token, |
|
80
|
1 |
|
'token_type' => 'Bearer' |
|
81
|
1 |
|
], 201 |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Invalidate and log out the user |
|
87
|
|
|
* |
|
88
|
|
|
* @return void |
|
89
|
|
|
* |
|
90
|
|
|
*/ |
|
91
|
|
|
public function logout() |
|
92
|
|
|
{ |
|
93
|
|
|
// |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
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@propertyannotation 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.