@@ -38,6 +38,11 @@ class Auth
3838 */
3939 protected $ errorsArray = [];
4040
41+ /**
42+ * Configured oauth clients
43+ */
44+ protected $ oauthClients = [];
45+
4146 public function __construct ()
4247 {
4348 $ this ->middleware ('auth.required ' , function () {
@@ -87,6 +92,21 @@ public function __construct()
8792 $ this ->middleware ('auth.unverified ' , function () {
8893 response ()->redirect ('/dashboard ' );
8994 });
95+
96+ if (
97+ class_exists ('League\OAuth2\Client\Provider\Google ' ) &&
98+ _env ('GOOGLE_AUTH_CLIENT_ID ' ) &&
99+ _env ('GOOGLE_AUTH_CLIENT_SECRET ' )
100+ ) {
101+ $ this ->withGoogle (
102+ _env ('GOOGLE_AUTH_CLIENT_ID ' ),
103+ _env ('GOOGLE_AUTH_CLIENT_SECRET ' ),
104+ [
105+ 'name ' => 'google ' ,
106+ 'redirectUri ' => _env ('GOOGLE_AUTH_REDIRECT_URI ' , _env ('APP_URL ' ) . '/auth/register/google ' ),
107+ ]
108+ );
109+ }
90110 }
91111
92112 /**
@@ -130,6 +150,47 @@ public function dbConnection(\PDO $connection)
130150 return $ this ;
131151 }
132152
153+ /**
154+ * Register a Google OAuth client
155+ * ---
156+ * Register a Google OAuth client to use with Leaf Auth, should be a league/oauth2-client compatible client.
157+ * @param string $clientId
158+ * @param string $clientSecret
159+ * @param array $options
160+ * @return static
161+ */
162+ public function withGoogle (
163+ string $ clientId ,
164+ string $ clientSecret ,
165+ array $ options = []
166+ ) {
167+ $ clientName = $ options ['name ' ] ?? 'google ' ;
168+
169+ unset($ options ['name ' ]);
170+
171+ if (!isset ($ options ['redirectUri ' ])) {
172+ $ options ['redirectUri ' ] = _env ('APP_URL ' ) . '/auth/google/callback ' ;
173+ }
174+
175+ $ this ->oauthClients [$ clientName ] = new \League \OAuth2 \Client \Provider \Google (array_merge ([
176+ 'clientId ' => $ clientId ,
177+ 'clientSecret ' => $ clientSecret ,
178+ 'redirectUri ' => $ options ['redirectUri ' ],
179+ ], $ options ));
180+
181+ return $ this ;
182+ }
183+
184+ /**
185+ * Return an oauth client
186+ * @param string $clientName The name of the client to return
187+ * @return \League\OAuth2\Client\Provider\AbstractProvider|null
188+ */
189+ public function client (string $ clientName )
190+ {
191+ return $ this ->oauthClients [$ clientName ] ?? null ;
192+ }
193+
133194 /**
134195 * Get/Set Leaf Auth config
135196 *
@@ -429,7 +490,11 @@ public function fromOAuth(array $userData): bool
429490 $ this ->checkDbConnection ();
430491 $ this ->config ('password.key ' , false );
431492
432- $ user = $ this ->db ->select (Config::get ('db.table ' ))->where ($ userData ['user ' ])->first ();
493+ $ user = $ this ->db ->select (Config::get ('db.table ' ))
494+ ->where ([
495+ 'email ' => $ userData ['user ' ]['email ' ] ?? null ,
496+ ])
497+ ->first ();
433498
434499 Config::setUserCache ('oauth-token ' , $ userData ['token ' ]);
435500
0 commit comments