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
@@ -55,31 +60,31 @@ public function __construct()
5560 });
5661
5762 $ this ->middleware ('is ' , function ($ role ) {
58- \ Leaf \ Exception \ General::error (
63+ General::error (
5964 '404 ' ,
6065 '<p>The page you are looking for could not be found.</p> ' ,
6166 403
6267 );
6368 });
6469
6570 $ this ->middleware ('isNot ' , function () {
66- \ Leaf \ Exception \ General::error (
71+ General::error (
6772 '404 ' ,
6873 '<p>The page you are looking for could not be found.</p> ' ,
6974 403
7075 );
7176 });
7277
7378 $ this ->middleware ('can ' , function () {
74- \ Leaf \ Exception \ General::error (
79+ General::error (
7580 '404 ' ,
7681 '<p>The page you are looking for could not be found.</p> ' ,
7782 403
7883 );
7984 });
8085
8186 $ this ->middleware ('cannot ' , function () {
82- \ Leaf \ Exception \ General::error (
87+ General::error (
8388 '404 ' ,
8489 '<p>The page you are looking for could not be found.</p> ' ,
8590 403
@@ -141,10 +146,10 @@ public function autoConnect(array $pdoOptions = [])
141146 /**
142147 * Pass in db connection instance directly
143148 *
144- * @param \ PDO $connection A connection instance of your db
149+ * @param PDO $connection A connection instance of your db
145150 * @return $this;
146151 */
147- public function dbConnection (\ PDO $ connection )
152+ public function dbConnection (PDO $ connection )
148153 {
149154 $ this ->db = new Db ();
150155 $ this ->db ->connection ($ connection );
@@ -174,7 +179,7 @@ public function withGoogle(
174179 $ options ['redirectUri ' ] = _env ('APP_URL ' ) . '/auth/google/callback ' ;
175180 }
176181
177- $ this ->withProvider ($ clientName , new \ League \ OAuth2 \ Client \ Provider \ Google (array_merge ([
182+ $ this ->withProvider ($ clientName , new Google (array_merge ([
178183 'clientId ' => $ clientId ,
179184 'clientSecret ' => $ clientSecret ,
180185 'redirectUri ' => $ options ['redirectUri ' ],
@@ -283,8 +288,8 @@ public function login(array $credentials): bool
283288 $ this ->errorsArray ['auth ' ] = Config::get ('messages.loginParamsError ' );
284289 return false ;
285290 }
286- } catch (\ Throwable $ th ) {
287- throw new \ Exception ($ th ->getMessage ());
291+ } catch (Throwable $ th ) {
292+ throw new Exception ($ th ->getMessage ());
288293 }
289294
290295 if ($ passwordKey !== false ) {
@@ -350,8 +355,8 @@ public function register(array $userData): bool
350355 $ this ->errorsArray = array_merge ($ this ->errorsArray , $ this ->db ->errors ());
351356 return false ;
352357 }
353- } catch (\ Throwable $ th ) {
354- throw new \ Exception ($ th ->getMessage ());
358+ } catch (Throwable $ th ) {
359+ throw new Exception ($ th ->getMessage ());
355360 }
356361
357362 $ user = $ this ->db ->select ($ table )->where ($ userData )->first ();
@@ -420,8 +425,8 @@ public function update(array $userData): bool
420425 $ this ->errorsArray = array_merge ($ this ->errorsArray , $ this ->db ->errors ());
421426 return false ;
422427 }
423- } catch (\ Throwable $ th ) {
424- throw new \ Exception ($ th ->getMessage ());
428+ } catch (Throwable $ th ) {
429+ throw new Exception ($ th ->getMessage ());
425430 }
426431
427432 if (Config::get ('session ' )) {
@@ -483,8 +488,8 @@ public function updatePassword(string $oldPassword, string $newPassword): bool
483488 $ this ->errorsArray = array_merge ($ this ->errorsArray , $ this ->db ->errors ());
484489 return false ;
485490 }
486- } catch (\ Throwable $ th ) {
487- throw new \ Exception ($ th ->getMessage ());
491+ } catch (Throwable $ th ) {
492+ throw new Exception ($ th ->getMessage ());
488493 }
489494
490495 $ this ->user ->{$ passwordKey } = $ newPassword ;
@@ -592,8 +597,8 @@ public function createUserFor($userData)
592597 $ this ->errorsArray = array_merge ($ this ->errorsArray , $ this ->db ->errors ());
593598 return false ;
594599 }
595- } catch (\ Throwable $ th ) {
596- throw new \ Exception ($ th ->getMessage ());
600+ } catch (Throwable $ th ) {
601+ throw new Exception ($ th ->getMessage ());
597602 }
598603
599604 $ user = $ this ->db ->select ($ table )->where ($ userData )->first ();
@@ -690,8 +695,8 @@ public function user()
690695 $ this ->errorsArray = $ this ->db ->errors ();
691696 return null ;
692697 }
693- } catch (\ Throwable $ th ) {
694- throw new \ Exception ($ th ->getMessage ());
698+ } catch (Throwable $ th ) {
699+ throw new Exception ($ th ->getMessage ());
695700 }
696701
697702 return $ this ->user = (new User (
@@ -736,8 +741,8 @@ public function tokens()
736741 */
737742 public function middleware (string $ middleware , callable $ callback )
738743 {
739- if (!class_exists (\ Leaf \ App::class)) {
740- throw new \ Exception ('This feature is only available for Leaf apps ' );
744+ if (!class_exists (App::class)) {
745+ throw new Exception ('This feature is only available for Leaf apps ' );
741746 }
742747
743748 if ($ middleware === 'auth.required ' ) {
@@ -833,7 +838,7 @@ public function parseToken()
833838 $ bearerToken ,
834839 new Key (Config::get ('token.secret ' ), 'HS256 ' )
835840 );
836- } catch (\ Throwable $ th ) {
841+ } catch (Throwable $ th ) {
837842 $ this ->errorsArray ['token ' ] = $ th ->getMessage ();
838843 return null ;
839844 }
@@ -875,7 +880,7 @@ public function verifyToken(string $token, ?string $purpose = null)
875880 }
876881
877882 return $ user ;
878- } catch (\ Throwable $ th ) {
883+ } catch (Throwable $ th ) {
879884 $ this ->errorsArray ['token ' ] = $ th ->getMessage ();
880885 return null ;
881886 }
@@ -894,13 +899,13 @@ public function db()
894899 protected function checkDbConnection (): void
895900 {
896901 if (!$ this ->db && function_exists ('db ' )) {
897- if (db ()->connection () instanceof \ PDO || db ()->autoConnect ()) {
902+ if (db ()->connection () instanceof PDO || db ()->autoConnect ()) {
898903 $ this ->db = db ();
899904 }
900905 }
901906
902907 if (!$ this ->db ) {
903- throw new \ Exception ('You need to connect to your database first ' );
908+ throw new Exception ('You need to connect to your database first ' );
904909 }
905910 }
906911
@@ -916,7 +921,7 @@ protected function getFromSession($value)
916921 protected function sessionCheck ()
917922 {
918923 if (!Config::get ('session ' )) {
919- throw new \ Exception ('Turn on sessions to use this feature. ' );
924+ throw new Exception ('Turn on sessions to use this feature. ' );
920925 }
921926 }
922927
0 commit comments