From 3b3d9fd4877d1c869c0743b1beb2c922abb36f38 Mon Sep 17 00:00:00 2001 From: Lakshan Madushanka Date: Mon, 22 Apr 2024 13:52:20 +0530 Subject: [PATCH 1/2] Make redirection to intended URL after authenticate and fix hard coded url name. --- src/UserProviderRedirector.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/UserProviderRedirector.php b/src/UserProviderRedirector.php index cbd527b..c89c249 100644 --- a/src/UserProviderRedirector.php +++ b/src/UserProviderRedirector.php @@ -12,6 +12,12 @@ class UserProviderRedirector implements ProviderRedirector { + /** + * @var string + * URL to redirect after successfully authenticated + */ + public static string $success_url = 'dashboard'; + /** * Redirect when unable to authenticate the user. */ @@ -62,7 +68,7 @@ public function userAuthenticated(Authenticatable $user, SocialiteUser $socialit Session::regenerate(); - return redirect('dashboard'); + return redirect()->intended(static::$success_url); } /** From ee4dacfb79a2f886a9108d5cd0d85d2ab766a24f Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Thu, 25 Apr 2024 15:59:08 -0400 Subject: [PATCH 2/2] Add static methods to control all default URLs --- src/UserProviderRedirector.php | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/UserProviderRedirector.php b/src/UserProviderRedirector.php index c89c249..bbb1669 100644 --- a/src/UserProviderRedirector.php +++ b/src/UserProviderRedirector.php @@ -13,10 +13,24 @@ class UserProviderRedirector implements ProviderRedirector { /** - * @var string - * URL to redirect after successfully authenticated + * The URL to redirect to when unable to authenticate the user. */ - public static string $success_url = 'dashboard'; + public static string $unableToAuthenticateUserUrl = 'login'; + + /** + * The URL to redirect to when the user already exists. + */ + public static string $userAlreadyExistsUrl = 'login'; + + /** + * The URL to redirect to when unable to create the user. + */ + public static string $unableToCreateUserUrl = 'login'; + + /** + * The URL to redirect to when the user has been successfully authenticated. + */ + public static string $userAuthenticatedUrl = 'dashboard'; /** * Redirect when unable to authenticate the user. @@ -27,7 +41,7 @@ public function unableToAuthenticateUser(Exception $e, string $driver): Redirect $message = sprintf('There was a problem creating your %s account. Please try again.', ucfirst($driver)); - return $this->redirector()->to('login')->with('flash', [ + return $this->redirector()->to(static::$unableToAuthenticateUserUrl)->with('flash', [ 'bannerStyle' => 'danger', 'banner' => $message, ]); @@ -40,7 +54,7 @@ public function userAlreadyExists(SocialiteUser $user, string $driver): Redirect { $message = sprintf("An account with the email address '%s' already exists.", $user->email); - return $this->redirector()->to('login')->with('flash', [ + return $this->redirector()->to(static::$userAlreadyExistsUrl)->with('flash', [ 'bannerStyle' => 'danger', 'banner' => $message, ]); @@ -53,7 +67,7 @@ public function unableToCreateUser(Exception $e, SocialiteUser $user, string $dr { report($e); - return $this->redirector()->to('login')->with('flash', [ + return $this->redirector()->to(static::$unableToCreateUserUrl)->with('flash', [ 'bannerStyle' => 'danger', 'banner' => 'There was a problem creating your account. Please try again.', ]); @@ -68,7 +82,7 @@ public function userAuthenticated(Authenticatable $user, SocialiteUser $socialit Session::regenerate(); - return redirect()->intended(static::$success_url); + return redirect()->intended(static::$userAuthenticatedUrl); } /**