22
33namespace Leaf ;
44
5+ use Exception ;
56use Firebase \JWT \JWT ;
67use Firebase \JWT \Key ;
78use Leaf \Auth \Config ;
89use Leaf \Auth \User ;
10+ use Leaf \Exception \General ;
911use Leaf \Helpers \Password ;
1012use Leaf \Http \Session ;
13+ use League \OAuth2 \Client \Provider \Google ;
14+ use PDO ;
15+ use Throwable ;
1116
1217/**
1318 * Leaf Simple Auth
@@ -53,31 +58,31 @@ public function __construct()
5358 });
5459
5560 $ this ->middleware ('is ' , function ($ role ) {
56- \ Leaf \ Exception \ General::error (
61+ General::error (
5762 '404 ' ,
5863 '<p>The page you are looking for could not be found.</p> ' ,
5964 403
6065 );
6166 });
6267
6368 $ this ->middleware ('isNot ' , function () {
64- \ Leaf \ Exception \ General::error (
69+ General::error (
6570 '404 ' ,
6671 '<p>The page you are looking for could not be found.</p> ' ,
6772 403
6873 );
6974 });
7075
7176 $ this ->middleware ('can ' , function () {
72- \ Leaf \ Exception \ General::error (
77+ General::error (
7378 '404 ' ,
7479 '<p>The page you are looking for could not be found.</p> ' ,
7580 403
7681 );
7782 });
7883
7984 $ this ->middleware ('cannot ' , function () {
80- \ Leaf \ Exception \ General::error (
85+ General::error (
8186 '404 ' ,
8287 '<p>The page you are looking for could not be found.</p> ' ,
8388 403
@@ -146,10 +151,10 @@ public function autoConnect(array $pdoOptions = [])
146151 /**
147152 * Pass in db connection instance directly
148153 *
149- * @param \ PDO $connection A connection instance of your db
150- * @return $this
154+ * @param PDO $connection A connection instance of your db
155+ * @return $this;
151156 */
152- public function dbConnection (\ PDO $ connection )
157+ public function dbConnection (PDO $ connection )
153158 {
154159 $ this ->db = new Db ();
155160 $ this ->db ->connection ($ connection );
@@ -182,7 +187,7 @@ public function withGoogle(
182187 $ options ['redirectUri ' ] = _env ('APP_URL ' ) . '/auth/google/callback ' ;
183188 }
184189
185- $ this ->withProvider ($ clientName , new \ League \ OAuth2 \ Client \ Provider \ Google (array_merge ([
190+ $ this ->withProvider ($ clientName , new Google (array_merge ([
186191 'clientId ' => $ clientId ,
187192 'clientSecret ' => $ clientSecret ,
188193 'redirectUri ' => $ options ['redirectUri ' ],
@@ -292,8 +297,8 @@ public function login(array $credentials): bool
292297 $ this ->errorsArray ['auth ' ] = Config::get ('messages.loginParamsError ' );
293298 return false ;
294299 }
295- } catch (\ Throwable $ th ) {
296- throw new \ Exception ($ th ->getMessage ());
300+ } catch (Throwable $ th ) {
301+ throw new Exception ($ th ->getMessage ());
297302 }
298303
299304 if ($ passwordKey !== false ) {
@@ -346,7 +351,7 @@ public function register(array $userData): bool
346351 $ userData ['email ' ] = strtolower ($ userData ['email ' ]);
347352 }
348353
349- if (isset ($ credentials [Config::get ('id.key ' )])) {
354+ if (isset ($ userData [Config::get ('id.key ' )])) {
350355 $ userData [Config::get ('id.key ' )] = is_callable ($ userData [Config::get ('id.key ' )])
351356 ? call_user_func ($ userData [Config::get ('id.key ' )])
352357 : $ userData [Config::get ('id.key ' )];
@@ -359,8 +364,8 @@ public function register(array $userData): bool
359364 $ this ->errorsArray = array_merge ($ this ->errorsArray , $ this ->db ->errors ());
360365 return false ;
361366 }
362- } catch (\ Throwable $ th ) {
363- throw new \ Exception ($ th ->getMessage ());
367+ } catch (Throwable $ th ) {
368+ throw new Exception ($ th ->getMessage ());
364369 }
365370
366371 $ user = $ this ->db ->select ($ table )->where ($ userData )->first ();
@@ -429,8 +434,8 @@ public function update(array $userData): bool
429434 $ this ->errorsArray = array_merge ($ this ->errorsArray , $ this ->db ->errors ());
430435 return false ;
431436 }
432- } catch (\ Throwable $ th ) {
433- throw new \ Exception ($ th ->getMessage ());
437+ } catch (Throwable $ th ) {
438+ throw new Exception ($ th ->getMessage ());
434439 }
435440
436441 if (Config::get ('session ' )) {
@@ -492,8 +497,8 @@ public function updatePassword(string $oldPassword, string $newPassword): bool
492497 $ this ->errorsArray = array_merge ($ this ->errorsArray , $ this ->db ->errors ());
493498 return false ;
494499 }
495- } catch (\ Throwable $ th ) {
496- throw new \ Exception ($ th ->getMessage ());
500+ } catch (Throwable $ th ) {
501+ throw new Exception ($ th ->getMessage ());
497502 }
498503
499504 $ this ->user ->{$ passwordKey } = $ newPassword ;
@@ -590,7 +595,7 @@ public function createUserFor($userData)
590595 $ userData ['email ' ] = strtolower ($ userData ['email ' ]);
591596 }
592597
593- if (isset ($ credentials [Config::get ('id.key ' )])) {
598+ if (isset ($ userData [Config::get ('id.key ' )])) {
594599 $ userData [Config::get ('id.key ' )] = is_callable ($ userData [Config::get ('id.key ' )])
595600 ? call_user_func ($ userData [Config::get ('id.key ' )])
596601 : $ userData [Config::get ('id.key ' )];
@@ -603,8 +608,8 @@ public function createUserFor($userData)
603608 $ this ->errorsArray = array_merge ($ this ->errorsArray , $ this ->db ->errors ());
604609 return false ;
605610 }
606- } catch (\ Throwable $ th ) {
607- throw new \ Exception ($ th ->getMessage ());
611+ } catch (Throwable $ th ) {
612+ throw new Exception ($ th ->getMessage ());
608613 }
609614
610615 $ user = $ this ->db ->select ($ table )->where ($ userData )->first ();
@@ -702,8 +707,8 @@ public function user()
702707 $ this ->errorsArray = $ this ->db ->errors ();
703708 return null ;
704709 }
705- } catch (\ Throwable $ th ) {
706- throw new \ Exception ($ th ->getMessage ());
710+ } catch (Throwable $ th ) {
711+ throw new Exception ($ th ->getMessage ());
707712 }
708713
709714 return $ this ->user = (new User (
@@ -753,8 +758,8 @@ public function tokens()
753758 */
754759 public function middleware (string $ middleware , callable $ callback )
755760 {
756- if (!class_exists (\ Leaf \ App::class)) {
757- throw new \ Exception ('This feature is only available for Leaf apps ' );
761+ if (!class_exists (App::class)) {
762+ throw new Exception ('This feature is only available for Leaf apps ' );
758763 }
759764
760765 if ($ middleware === 'auth.required ' ) {
@@ -867,7 +872,7 @@ public function parseToken()
867872 $ bearerToken ,
868873 new Key (Config::get ('token.secret ' ), 'HS256 ' )
869874 );
870- } catch (\ Throwable $ th ) {
875+ } catch (Throwable $ th ) {
871876 $ this ->errorsArray ['token ' ] = $ th ->getMessage ();
872877 return null ;
873878 }
@@ -909,7 +914,7 @@ public function verifyToken(string $token, ?string $purpose = null)
909914 }
910915
911916 return $ user ;
912- } catch (\ Throwable $ th ) {
917+ } catch (Throwable $ th ) {
913918 $ this ->errorsArray ['token ' ] = $ th ->getMessage ();
914919 return null ;
915920 }
@@ -928,13 +933,13 @@ public function db()
928933 protected function checkDbConnection (): void
929934 {
930935 if (!$ this ->db && function_exists ('db ' )) {
931- if (db ()->connection () instanceof \ PDO || db ()->autoConnect ()) {
936+ if (db ()->connection () instanceof PDO || db ()->autoConnect ()) {
932937 $ this ->db = db ();
933938 }
934939 }
935940
936941 if (!$ this ->db ) {
937- throw new \ Exception ('You need to connect to your database first ' );
942+ throw new Exception ('You need to connect to your database first ' );
938943 }
939944 }
940945
@@ -958,7 +963,7 @@ protected function getFromSession($value)
958963 protected function sessionCheck ()
959964 {
960965 if (!Config::get ('session ' )) {
961- throw new \ Exception ('Turn on sessions to use this feature. ' );
966+ throw new Exception ('Turn on sessions to use this feature. ' );
962967 }
963968 }
964969
0 commit comments