|
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
|
|
|
public function authenticate(Request $request) |
|
18
|
|
|
{ |
|
19
|
|
|
// grab credentials from the request |
|
20
|
|
|
$credentials = $request->only('email', 'password'); |
|
21
|
|
|
|
|
22
|
|
|
try { |
|
23
|
|
|
// attempt to verify the credentials and create a token for the user |
|
24
|
|
|
if (!$token = JWTAuth::attempt($credentials)) { |
|
25
|
|
|
return response()->json([ 'error' => 'invalid_credentials' ], 401); |
|
26
|
|
|
} |
|
27
|
|
|
} catch (JWTException $e) { |
|
28
|
|
|
// something went wrong whilst attempting to encode the token |
|
29
|
|
|
return response()->json([ 'error' => 'could_not_create_token' ], 500); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
// all good so return the token |
|
33
|
|
|
return response()->json([ |
|
34
|
|
|
'access_token' => $token, |
|
35
|
|
|
'token_type' => 'bearer' |
|
36
|
|
|
]); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
// Register a new user |
|
40
|
|
|
public function register(RegisterUserRequest $request) |
|
41
|
|
|
{ |
|
42
|
|
|
$user = User::create([ |
|
|
|
|
|
|
43
|
|
|
'first_name' => $request->first_name, |
|
|
|
|
|
|
44
|
|
|
'last_name' => $request->last_name, |
|
|
|
|
|
|
45
|
|
|
'email' => $request->email, |
|
|
|
|
|
|
46
|
|
|
// create the username based on first_name and last_name |
|
47
|
|
|
// if not provided |
|
48
|
|
|
/*$userName = trim(strtolower($request->first_name.'-'.$request->last_name)), |
|
|
|
|
|
|
49
|
|
|
'username' => $userName,*/ |
|
50
|
|
|
'username' => $request->username, |
|
|
|
|
|
|
51
|
|
|
'phone' => $request->phone, |
|
|
|
|
|
|
52
|
|
|
'bio' => $request->bio, |
|
|
|
|
|
|
53
|
|
|
'birthdate' => $request->birthdate, |
|
|
|
|
|
|
54
|
|
|
'password' => bcrypt($request->password), |
|
|
|
|
|
|
55
|
|
|
]); |
|
56
|
|
|
|
|
57
|
|
|
if ($user) { |
|
58
|
|
|
$donor = new Donor(); |
|
59
|
|
|
$donor->user_id = $user->id; |
|
|
|
|
|
|
60
|
|
|
$donor->blood_type_id = NULL; |
|
|
|
|
|
|
61
|
|
|
$donor->save(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
// Send mail to user |
|
65
|
|
|
Mail::to($user->email)->send(new UserCreated($user)); |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
$token = JWTAuth::attempt($request->only('email', 'password')); |
|
70
|
|
|
|
|
71
|
|
|
// all good so return the token |
|
72
|
|
|
return response()->json([ |
|
73
|
|
|
'access_token' => $token, |
|
74
|
|
|
'token_type' => 'Bearer' |
|
75
|
|
|
]); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function userInfo() |
|
79
|
|
|
{ |
|
80
|
|
|
$user = JWTAuth::parsetToken()->authenticate(); |
|
81
|
|
|
|
|
82
|
|
|
// If the token is invalid |
|
83
|
|
|
if (! $user) { |
|
84
|
|
|
return response()->json(['invalid_user'], 401); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return response()->json([ |
|
88
|
|
|
'id' => $user->id, |
|
89
|
|
|
'first_name' => $user->first_name, |
|
90
|
|
|
'last_name' => $user->last_name, |
|
91
|
|
|
'email' => $user->email |
|
92
|
|
|
]); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
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.