Skip to content

Commit 4cb10d1

Browse files
author
ahmadhuss
committed
revert: Revert back old logic of RedirectIfAuthenticated route middleware
* We didn't remove the `RedirectIfAuthenticated` middleware instead move the logic into `RedirectIfAuthenticatedOld` middleware.
1 parent 3f7ce48 commit 4cb10d1

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace App\Http\Middleware;
4+
5+
use App\Providers\RouteServiceProvider;
6+
use Closure;
7+
use Illuminate\Http\Request;
8+
use Illuminate\Support\Facades\Auth;
9+
10+
class RedirectIfAuthenticatedOld
11+
{
12+
/**
13+
* Handle an incoming request.
14+
*
15+
* @param \Illuminate\Http\Request $request
16+
* @param \Closure $next
17+
* @param string|null ...$guards
18+
* @return mixed
19+
*/
20+
public function handle(Request $request, Closure $next, ...$guards)
21+
{
22+
// If you authenticated with the `normal user` then the `SessionGuard` object 'name' will be `null`
23+
// and Auth::guard($guard)->check() will return TRUE
24+
// and invokes the line `redirect(RouteServiceProvider::HOME)`.
25+
$guards = empty($guards) ? [null] : $guards;
26+
27+
// Remember Auth::guard('admin') will returns a `SessionGuard` object
28+
// which will use `check()` method to return boolean Is the Authenticated user is authenticated
29+
// with the `admin` guard session?
30+
foreach ($guards as $guard) {
31+
32+
// At the time of "localhost/admin/login" route the $guard will be `admin` then
33+
// is_null('admin') will return false and we make this expression true. So redirection
34+
// to the dashboard will automatically happen.
35+
36+
// But if guard session is admin & guard is null returns true then we make
37+
// this false and whole expression will not RUN
38+
39+
// Thr expression will only run if there is explicitly guard value is present
40+
if (!is_null($guard) && Auth::guard('admin')->check()) {
41+
return redirect()->route('dashboard');
42+
}
43+
44+
45+
else if (Auth::guard($guard)->check()) {
46+
return redirect(RouteServiceProvider::HOME);
47+
}
48+
}
49+
50+
51+
// If there is not any session guard present in the request. please open the route.
52+
// if (Auth::guard($guard)->check()) returns true then it means the user is
53+
// authenticated with the some session guard, then $next($request) will not work.
54+
return $next($request);
55+
}
56+
}

0 commit comments

Comments
 (0)