From f1c848eb618a17cba5bbf52c534ffa3a663af7e8 Mon Sep 17 00:00:00 2001 From: Debangshu Roy Date: Sat, 13 Jun 2020 01:14:47 +0530 Subject: [PATCH 1/2] Update RedirectIfNotAuthenticated.stub This change is to make the MultiAuth system compatible with Laravel default Authentication process and to behave identical as "web guard" or "User Model". Actually there is a little problem if we use public function handle($request, Closure $next, $guard = 'student') { if (!Auth::guard($guard)->check()) { return redirect('student\login'); } return $next($request); } as it is not go through the following method in //Illuminate\Foundation\Exceptions\Handler.php; /** * Convert an authentication exception into a response. * * @param \Illuminate\Http\Request $request * @param \Illuminate\Auth\AuthenticationException $exception * @return \Symfony\Component\HttpFoundation\Response */ protected function unauthenticated($request, AuthenticationException $exception) { return $request->expectsJson() ? response()->json(['message' => $exception->getMessage()], 401) : redirect()->guest($exception->redirectTo() ?? route('login')); } in this method redirect()->guest() play a role to set Intended Url $this->setIntendedUrl($intended); . So, for your newly created guard i.e. "student" if you not go through $this->setIntendedUrl($intended); in "unauthenticated" situation then after authentication rather than redirect to "student dashboard" it will redirect to previously set Indented Url. To understand this you can check it. Do the following.... Two guards - User(web/default) and student **in logout condition first visit domain.test/home [ user home page, don't login] then visit domain.test/student [student dashboard and do login in student] after student successful login it will redirect to domain.test/login instead of domain.test/student. --- .../RedirectIfNotAuthenticated.stub | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/stubs/Middleware/RedirectIfNotAuthenticated.stub b/stubs/Middleware/RedirectIfNotAuthenticated.stub index 70eec97..6b4b05e 100755 --- a/stubs/Middleware/RedirectIfNotAuthenticated.stub +++ b/stubs/Middleware/RedirectIfNotAuthenticated.stub @@ -2,27 +2,22 @@ namespace {{namespace}}\Http\Middleware; -use Closure; -use Illuminate\Support\Facades\Auth; +use Illuminate\Auth\Middleware\Authenticate as Middleware; -class RedirectIfNot{{singularClass}} +class RedirectIfNot{{singularClass}} extends Middleware { /** - * Handle an incoming request. + * Get the path the user should be redirected to when they are not authenticated. * * @param \Illuminate\Http\Request $request - * @param \Closure $next - * @param string|null $guard - * @return mixed + * @return string|null */ - public function handle($request, Closure $next, $guard = '{{singularSnake}}') + protected function redirectTo($request) { - if (!Auth::guard($guard)->check()) { - return redirect('{{singularSlug}}/login'); + if (! $request->expectsJson()) { + return '/{{singularSlug}}/login'; } - - return $next($request); } } From 9d3a8820af7c0eb8df10066fb3174aa07c3daafa Mon Sep 17 00:00:00 2001 From: Debangshu Roy Date: Sat, 13 Jun 2020 02:12:43 +0530 Subject: [PATCH 2/2] Update RedirectIfNotAuthenticated.stub --- stubs/Middleware/RedirectIfNotAuthenticated.stub | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/Middleware/RedirectIfNotAuthenticated.stub b/stubs/Middleware/RedirectIfNotAuthenticated.stub index 6b4b05e..ac965a8 100755 --- a/stubs/Middleware/RedirectIfNotAuthenticated.stub +++ b/stubs/Middleware/RedirectIfNotAuthenticated.stub @@ -16,7 +16,7 @@ class RedirectIfNot{{singularClass}} extends Middleware protected function redirectTo($request) { if (! $request->expectsJson()) { - return '/{{singularSlug}}/login'; + return route('{{singularSlug}}.login'); } }