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
|
|
|
use DoeSangue\Models\Donor; |
14
|
|
|
|
15
|
|
|
class AuthenticateController extends Controller |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Authenticate the user |
19
|
|
|
* |
20
|
|
|
* @param Request $request |
21
|
|
|
* @return void |
22
|
|
|
*/ |
23
|
|
|
public function authenticate(Request $request) |
24
|
|
|
{ |
25
|
|
|
// grab credentials from the request |
26
|
|
|
$credentials = $request->only('email', 'password'); |
27
|
|
|
|
28
|
|
|
try { |
29
|
|
|
// attempt to verify the credentials and create a token for the user |
30
|
|
|
if (!$token = JWTAuth::attempt($credentials)) { |
31
|
|
|
return response()->json([ 'error' => 'invalid_credentials' ], 401); |
32
|
|
|
} |
33
|
|
|
} catch (JWTException $e) { |
34
|
|
|
// something went wrong whilst attempting to encode the token |
35
|
|
|
return response()->json([ 'error' => 'could_not_create_token' ], 500); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
// all good so return the token |
39
|
|
|
return response()->json( |
40
|
|
|
[ |
41
|
|
|
'access_token' => $token, |
42
|
|
|
'token_type' => 'Bearer' |
43
|
|
|
], 200 |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// Register a new user |
48
|
|
|
public function register(RegisterUserRequest $request) |
49
|
|
|
{ |
50
|
|
|
$user = User::create( |
|
|
|
|
51
|
|
|
[ |
52
|
|
|
'first_name' => $request->first_name, |
|
|
|
|
53
|
|
|
'last_name' => $request->last_name, |
|
|
|
|
54
|
|
|
'email' => $request->email, |
|
|
|
|
55
|
|
|
// create the username based on first_name and last_name |
56
|
|
|
// if not provided |
57
|
|
|
/*$userName = trim(strtolower($request->first_name.'-'.$request->last_name)), |
|
|
|
|
58
|
|
|
'username' => $userName,*/ |
59
|
|
|
'username' => $request->username, |
|
|
|
|
60
|
|
|
'phone' => $request->phone, |
|
|
|
|
61
|
|
|
'bio' => $request->bio, |
|
|
|
|
62
|
|
|
'birthdate' => $request->birthdate, |
|
|
|
|
63
|
|
|
'password' => bcrypt($request->password), |
|
|
|
|
64
|
|
|
] |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
if ($user) { |
68
|
|
|
$donor = new Donor(); |
69
|
|
|
$donor->user_id = $user->id; |
|
|
|
|
70
|
|
|
$donor->blood_type_id = null; |
|
|
|
|
71
|
|
|
$donor->save(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// Send mail to user |
75
|
|
|
Mail::to($user->email)->send(new UserCreated($user)); |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
|
79
|
|
|
$token = JWTAuth::attempt($request->only('email', 'password')); |
80
|
|
|
|
81
|
|
|
// all good so return the token |
82
|
|
|
return response()->json( |
83
|
|
|
[ |
84
|
|
|
'access_token' => $token, |
85
|
|
|
'token_type' => 'Bearer' |
86
|
|
|
], 201 |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Invalidate and log out the user |
92
|
|
|
* |
93
|
|
|
* @return void |
94
|
|
|
* |
95
|
|
|
*/ |
96
|
|
|
public function logout() |
97
|
|
|
{ |
98
|
|
|
// |
99
|
|
|
} |
100
|
|
|
/** |
101
|
|
|
* Get the current user (logged in) information |
102
|
|
|
* |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
|
View Code Duplication |
public function userInfo() |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
$user = JWTAuth::parseToken()->authenticate(); |
108
|
|
|
|
109
|
|
|
// If the token is invalid |
110
|
|
|
if (!$user) { |
111
|
|
|
return response()->json([ 'invalid_user' ], 401); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return response()->json( |
115
|
|
|
[ |
116
|
|
|
'first_name' => $user->first_name, |
117
|
|
|
'last_name' => $user->last_name, |
118
|
|
|
// 'email' => $user->email, |
|
|
|
|
119
|
|
|
'username' => $user->username |
120
|
|
|
], 200 |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.