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
|
|
|
[ |
35
|
|
|
"data" { |
|
|
|
|
36
|
|
|
'access_token' => $token, |
|
|
|
|
37
|
|
|
'token_type' => 'Bearer' |
38
|
|
|
} |
39
|
|
|
] |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// Register a new user |
44
|
|
|
public function register(RegisterUserRequest $request) |
45
|
|
|
{ |
46
|
|
|
$user = User::create( |
47
|
|
|
[ |
48
|
|
|
'first_name' => $request->first_name, |
49
|
|
|
'last_name' => $request->last_name, |
50
|
|
|
'email' => $request->email, |
51
|
|
|
// create the username based on first_name and last_name |
52
|
|
|
// if not provided |
53
|
|
|
/*$userName = trim(strtolower($request->first_name.'-'.$request->last_name)), |
|
|
|
|
54
|
|
|
'username' => $userName,*/ |
55
|
|
|
'username' => $request->username, |
56
|
|
|
'phone' => $request->phone, |
57
|
|
|
'bio' => $request->bio, |
58
|
|
|
'birthdate' => $request->birthdate, |
59
|
|
|
'password' => bcrypt($request->password), |
60
|
|
|
] |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
if ($user) { |
64
|
|
|
$donor = new Donor(); |
65
|
|
|
$donor->user_id = $user->id; |
66
|
|
|
$donor->blood_type_id = null; |
67
|
|
|
$donor->save(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// Send mail to user |
71
|
|
|
Mail::to($user->email)->send(new UserCreated($user)); |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
|
75
|
|
|
$token = JWTAuth::attempt($request->only('email', 'password')); |
76
|
|
|
|
77
|
|
|
// all good so return the token |
78
|
|
|
return response()->json( |
79
|
|
|
[ |
80
|
|
|
'access_token' => $token, |
81
|
|
|
'token_type' => 'Bearer' |
82
|
|
|
], 201 |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Invalidate and log out the user |
88
|
|
|
*/ |
89
|
|
|
public function logout() |
90
|
|
|
{ |
91
|
|
|
// |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function userInfo() |
95
|
|
|
{ |
96
|
|
|
$user = JWTAuth::parseToken()->authenticate(); |
97
|
|
|
|
98
|
|
|
// If the token is invalid |
99
|
|
|
if (! $user) { |
100
|
|
|
return response()->json(['invalid_user'], 401); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return response()->json( |
104
|
|
|
[ |
105
|
|
|
'first_name' => $user->first_name, |
106
|
|
|
'last_name' => $user->last_name, |
107
|
|
|
// 'email' => $user->email, |
|
|
|
|
108
|
|
|
'username' => $user->username |
109
|
|
|
], 200 |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.