6
6
use Core \{Controller , Router };
7
7
use Core \Helpers \Request ;
8
8
use Core \Helpers \Response ;
9
+ use Exception ;
9
10
use Firebase \JWT \{JWT , Key };
10
11
11
12
/**
@@ -40,18 +41,12 @@ public function registerHeader($data = [])
40
41
41
42
if (!$ this ->model ('Example ' )->add ($ data )) {
42
43
Router::abort (500 , json_encode ([
43
- 'status ' => 'error ' ,
44
44
'message ' => 'Server error '
45
45
]));
46
46
}
47
47
48
- $ example = $ this ->model ('Example ' )->get (
49
- $ this ->model ('Example ' )->getLastInsertedId ()
50
- );
51
-
52
48
Response::send ([
53
- 'status ' => 'success ' ,
54
- 'data ' => $ example
49
+ 'message ' => 'Registered successfully ' ,
55
50
]);
56
51
}
57
52
@@ -65,10 +60,9 @@ public function login($data = [])
65
60
{
66
61
$ example = $ this ->model ('Example ' )->getBy ('headerRef ' , $ data ['headerRef ' ]);
67
62
68
- Response::send ([
69
- 'status ' => 'success ' ,
70
- 'data ' => $ example
71
- ]);
63
+ Response::send (
64
+ $ example
65
+ );
72
66
}
73
67
74
68
/**
@@ -84,16 +78,14 @@ public function registerJWT($data = [])
84
78
85
79
if (!$ this ->model ('Example ' )->add ($ data )) {
86
80
Router::abort (500 , json_encode ([
87
- 'status ' => 'error ' ,
88
81
'message ' => 'Server error '
89
82
]));
90
83
}
91
84
92
85
unset($ data ['password ' ]);
93
86
94
87
Response::send ([
95
- 'status ' => 'success ' ,
96
- 'data ' => $ data
88
+ 'message ' => 'Registered successfully ' ,
97
89
]);
98
90
}
99
91
@@ -109,50 +101,76 @@ public function loginJWT($data = [])
109
101
110
102
if (!password_verify ($ data ['password ' ], $ example ->password )) {
111
103
Router::abort (401 , json_encode ([
112
- 'status ' => 'error ' ,
113
104
'message ' => 'Invalid password '
114
105
]));
115
106
}
116
107
117
- $ secret_key = $ _ENV ['JWT_SECRET_KEY ' ];
118
- $ issuer_claim = $ _ENV ['SERVER_ADDRESS ' ]; // this can be the servername
119
- $ audience_claim = $ _ENV ['CLIENT_ADDRESS ' ];
120
- $ issuedat_claim = time (); // issued at
121
- // $notbefore_claim = $issuedat_claim + 10; //not before in seconds
122
- $ expire_claim = $ issuedat_claim + 600 ; // expire time in seconds (10 minutes)
123
- $ payload = array (
124
- "iss " => $ issuer_claim ,
125
- "aud " => $ audience_claim ,
126
- "iat " => $ issuedat_claim ,
127
- // "nbf" => $notbefore_claim,
128
- "exp " => $ expire_claim ,
129
- "sub " => $ example ->username
130
- );
108
+ // Create Refresh Token
109
+ $ refreshToken = $ this ->createToken ($ example ->username , $ _ENV ['JWT_REFRESH_EXP_DELTA_SECONDS ' ]);
131
110
132
- $ jwt = JWT ::encode ($ payload , $ secret_key , "HS256 " );
111
+ setcookie (
112
+ name: 'auth ' ,
113
+ value: $ refreshToken ,
114
+ expires_or_options: time () + $ _ENV ['JWT_REFRESH_EXP_DELTA_SECONDS ' ],
115
+ httponly: true
116
+ );
117
+ // Create Access Token
118
+ $ accessToken = $ this ->createToken ($ example ->username , $ _ENV ['JWT_ACCESS_EXP_DELTA_SECONDS ' ]);
133
119
134
- // Set expirable cookie for JWT
135
- setcookie (name: 'jwt ' , value: $ jwt , expires_or_options: $ expire_claim , httponly: true );
120
+ unset($ example ->password , $ example ->id );
121
+ $ example ->avatar = file_get_contents (dirname (dirname (__DIR__ )) . "/public/identicons/ " . $ example ->avatar );
122
+ $ example ->accessToken = $ accessToken ;
136
123
137
124
Response::send (
138
- array (
139
- "message " => "Successful login. " ,
140
- "jwt " => $ jwt
141
- )
125
+ $ example
142
126
);
143
127
}
144
128
129
+ /**
130
+ * Refresh Access Token
131
+ *
132
+ * @param array $data
133
+ * @return void
134
+ */
135
+ public function refresh ()
136
+ {
137
+ $ refreshToken = Request::refreshToken ();
138
+
139
+ // Check if refresh token is valid
140
+ try {
141
+ if (!$ refreshToken ) {
142
+ throw new Exception ('No refresh token found ' );
143
+ }
144
+
145
+ $ token = JWT ::decode ($ refreshToken , new Key ($ _ENV ['JWT_SECRET_KEY ' ], $ _ENV ['JWT_ALGORITHM ' ]));
146
+
147
+ // Check if Example exists
148
+ $ example = (new Example ())->getBy ('username ' , $ token ->sub );
149
+ if (!$ example ) {
150
+ throw new Exception ('Example not found ' );
151
+ }
152
+
153
+ Response::send ([
154
+ 'accessToken ' => $ this ->createToken ($ example ->username , $ _ENV ['JWT_ACCESS_EXP_DELTA_SECONDS ' ])
155
+ ]);
156
+ } catch (Exception $ e ) {
157
+ Router::abort (401 , [
158
+ 'message ' => 'Unauthorized: ' . $ e ->getMessage ()
159
+ ]);
160
+ }
161
+ }
162
+
145
163
/**
146
164
* Logout an User
147
165
*
148
166
* @return void
149
167
*/
150
168
public function logoutJWT ()
151
169
{
152
- setcookie (name: 'jwt ' , value: '' , expires_or_options: time () - 3600 , httponly: true );
170
+ setcookie (name: 'auth ' , value: '' , expires_or_options: time () - 1 , httponly: true );
153
171
154
172
Response::send ([
155
- 'status ' => 'Logged out successfully! '
173
+ 'message ' => 'Logged out successfully! '
156
174
]);
157
175
}
158
176
@@ -169,8 +187,37 @@ public static function userJWT()
169
187
return null ;
170
188
}
171
189
172
- $ token = JWT ::decode ($ jwt , new Key ($ _ENV ['JWT_SECRET_KEY ' ], "HS256 " ));
190
+ $ token = JWT ::decode ($ jwt , new Key ($ _ENV ['JWT_SECRET_KEY ' ], $ _ENV ['JWT_ALGORITHM ' ]));
191
+
192
+ $ example = (new Example )->getBy ('username ' , $ token ->sub );
193
+
194
+ unset($ example ->password ,$ example ->id );
195
+
196
+ return $ example ;
197
+ }
198
+
199
+ /**
200
+ * Create token for user
201
+ *
202
+ * @param string $sub
203
+ * @param int $exp
204
+ * @return string
205
+ */
206
+ public static function createToken ($ sub , $ exp )
207
+ {
208
+ $ secret_key = $ _ENV ['JWT_SECRET_KEY ' ];
209
+ $ issuer_claim = $ _ENV ['SERVER_ADDRESS ' ]; // this can be the servername
210
+ $ audience_claim = $ _ENV ['CLIENT_ADDRESS ' ];
211
+ $ issuedat_claim = time (); // issued at
212
+ $ expire_claim = $ issuedat_claim + $ exp ; // expire time in seconds (24 hours from now)
213
+ $ payload = array (
214
+ "iss " => $ issuer_claim ,
215
+ "aud " => $ audience_claim ,
216
+ "iat " => $ issuedat_claim ,
217
+ "exp " => $ expire_claim ,
218
+ "sub " => $ sub
219
+ );
173
220
174
- return ( new Example )-> getBy ( ' username ' , $ token -> sub );
221
+ return JWT :: encode ( $ payload , $ secret_key , $ _ENV [ ' JWT_ALGORITHM ' ] );
175
222
}
176
223
}
0 commit comments