1212 * Perform simple authentication tasks.
1313 */
1414class Auth extends Mysqli {
15+ protected $ errorsArray = [];
16+
1517 public function __construct () {
1618 $ this ->form = new Form ;
1719 $ this ->response = new Response ;
@@ -40,12 +42,12 @@ public function login(string $table, array $credentials, string $password_encode
4042 $ data = [];
4143
4244 foreach ($ credentials as $ key => $ value ) {
43- try {
44- !$ this ->select ($ table , "* " , "$ key = ? " , [$ value ]);
45- } catch (\Throwable $ th ) {
46- $ this ->response ->throwErr (["error " => "$ key is not a valid column in the $ table table " ]);
47- exit ();
48- }
45+ // try {
46+ // !$this->select($table, "*", "$key = ?", [$value]);
47+ // } catch (\Throwable $th) {
48+ // $this->response->throwErr(["error" => "$key is not a valid column in the $table table"]);
49+ // exit();
50+ // }
4951
5052 array_push ($ keys , $ key );
5153 array_push ($ data , $ value );
@@ -59,8 +61,10 @@ public function login(string $table, array $credentials, string $password_encode
5961 $ data_length = count ($ data );
6062
6163 if (!empty ($ this ->form ->errors ())) {
62- $ this ->response ->throwErr ($ this ->form ->errors ());
63- exit ();
64+ foreach ($ this ->form ->errors () as $ key => $ value ) {
65+ $ this ->errorsArray [$ key ] = $ value ;
66+ }
67+ return false ;
6468 } else {
6569 $ condition = "" ;
6670
@@ -74,10 +78,19 @@ public function login(string $table, array $credentials, string $password_encode
7478 $ user = $ this ->select ($ table , "* " , $ condition , $ data )->fetchObj ();
7579
7680 if (!$ user ) {
77- $ this ->response -> throwErr ( " Incorrect credentials, please check and try again ") ;
78- exit () ;
81+ $ this ->errorsArray [ " auth " ] = " Incorrect credentials, please check and try again " ;
82+ return false ;
7983 }
84+
8085 $ token = $ this ->token ->generateSimpleToken ($ user ->id , "User secret key " );
86+
87+ if ($ token == false ) {
88+ foreach ($ this ->token ->errors () as $ key => $ value ) {
89+ $ this ->errorsArray [$ key ] = $ value ;
90+ }
91+ return false ;
92+ }
93+
8194 $ user ->token = $ token ;
8295 unset($ user ->password );
8396
@@ -103,12 +116,12 @@ public function register(string $table, array $credentials, array $uniques = nul
103116 $ data = [];
104117
105118 foreach ($ credentials as $ key => $ value ) {
106- try {
107- ! $ this ->select ($ table , "* " , "$ key = ? " , [$ value ]);
108- } catch (\Throwable $ th ) {
109- $ this ->response ->throwErr (["error " => "$ key is not a valid column in the $ table table " ]);
110- exit ();
111- }
119+ // try {
120+ // $this->select($table, "*", "$key = ?", [$value]);
121+ // } catch (\Throwable $th) {
122+ // $this->response->throwErr(["error" => "$key is not a valid column in the $table table"]);
123+ // exit();
124+ // }
112125
113126 array_push ($ keys , $ key );
114127 array_push ($ data , $ value );
@@ -135,8 +148,8 @@ public function register(string $table, array $credentials, array $uniques = nul
135148 }
136149
137150 if (!empty ($ this ->form ->errors ())) {
138- $ this ->response -> throwErr ( $ this ->form ->errors ());
139- exit () ;
151+ array_push ( $ this ->errorsArray , $ this ->form ->errors ());
152+ return false ;
140153 } else {
141154 $ table_names = "" ;
142155 $ table_values = "" ;
@@ -153,22 +166,74 @@ public function register(string $table, array $credentials, array $uniques = nul
153166 }
154167 }
155168
156- $ this ->insert ($ table , $ table_names , $ table_values , $ data );
169+ try {
170+ $ this ->insert ($ table , $ table_names , $ table_values , $ data );
171+ } catch (\Throwable $ th ) {
172+ $ this ->errorsArray ["error " ] = $ th ;
173+ return false ;
174+ }
157175 }
158176 }
159177
178+ /**
179+ * Validate Json Web Token
180+ */
181+ public function validate ($ token ) {
182+ $ payload = $ this ->token ->validate ($ token );
183+
184+ if ($ payload == false ) {
185+ foreach ($ this ->token ->errors () as $ key => $ value ) {
186+ $ this ->errorsArray [$ key ] = $ value ;
187+ }
188+ return false ;
189+ }
190+
191+ return $ payload ;
192+ }
193+
194+ /**
195+ * Validate Bearer Token
196+ */
160197 public function validateToken () {
161- try {
162- $ bearerToken = $ this -> token -> getBearerToken ();
163- $ payload = $ this -> token -> decode ( $ bearerToken , JWT_KEY , [ ' HS256 ' ]);
164- return $ payload ;
165- } catch ( Exception $ e ) {
166- $ this -> response -> respond ([ " auth_error " => " Authentication failed. " . $ e ]);;
167- exit () ;
198+ $ payload = $ this -> token -> validateToken ();
199+
200+ if ( $ payload == false ) {
201+ foreach ( $ this -> token -> errors () as $ key => $ value ) {
202+ $ this -> errorsArray [ $ key ] = $ value ;
203+ }
204+ return false ;
168205 }
206+
207+ return $ payload ;
169208 }
170209
210+ /**
211+ * Get Bearer token
212+ */
213+ public function getBearerToken () {
214+ $ token = $ this ->token ->getBearerToken ();
215+
216+ if ($ token == false ) {
217+ foreach ($ this ->token ->errors () as $ key => $ value ) {
218+ $ this ->errorsArray [$ key ] = $ value ;
219+ }
220+ return false ;
221+ }
222+
223+ return $ token ;
224+ }
225+
226+ /**
227+ * Return form field
228+ */
171229 public function get ($ param ) {
172230 return $ this ->form ->get ($ param );
173231 }
232+
233+ /**
234+ * Get all authentication errors as associative array
235+ */
236+ public function errors () {
237+ return $ this ->errorsArray ;
238+ }
174239}
0 commit comments