skip to Main Content

I’m using Laravel Fortify in my site. It has 2 different authentication screens for Admin and User. For example:

  • Admin login route: /admin/login

  • Admin forgot password route: /admin/forgot-password

  • User login route: /login

  • User forgot password route: /forgot-password

and so on…

I’m using Spatie laravel-permission to give 1 account an Admin role and another account an User role.

Everything works perfectly, like user is only able to login from User login page, Admin will be redirected to Admin Dashboard after login from Admin login page, etc. Even the reset password actually works. But the main problem is that, for both Admin and User, the reset password link is the same.

What I want is they have different reset password link. For example for Admin, I want the reset password link to be 127.0.0.1:8000/admin/reset-password/token and for User to be 127.0.0.1:8000/reset-password/token.

I found this weird considering I already made 2 different routes for them that I can check using php artisan route:list as shown below:
Admin Routes
User Routes

This is my codes related to the problem:

appProvidersFortifyServiceProvider.php:

        Fortify::requestPasswordResetLinkView(function () {
            if (Route::current()->getPrefix() === '/admin') {
                return view('auth.admin.forgot-password');
            }

            return view('auth.forgot-password');
        });

        Fortify::resetPasswordView(function (Request $request) {
            return view('auth.admin.reset-password', ['request' => $request]);
            // I didn't check the prefix like requestPasswordLinkView because it will throw error
            // the error is caused by not having admin prefix, just as my problem I'm asking now
        });

resourcesviewsauthadminlogin.blade.php:

                            <form action="{{ route('admin.login') }}" method="POST" class="mb-3">
                                @csrf
                                <div class="mb-3">
                                    <label for="email" class="form-label">Email Address</label>
                                    <input type="email" class="form-control @error('email') is-invalid @enderror" id="email" name="email" placeholder="Enter your email address" autofocus>
                                    @error('email') <div class="invalid-feedback">{{ $message }}</div> @enderror
                                </div>
                                <div class="mb-3 form-password-toggle">
                                    <div class="d-flex justify-content-between">
                                        <label for="password" class="form-label">Password</label>
                                        <a href="{{ route('admin.password.request') }}">
                                            <small>Forgot Password?</small>
                                        </a>
                                    </div>
                                    <div class="input-group input-group-merge">
                                        <input type="password" class="form-control @error('password') is-invalid @enderror" id="password" name="password" placeholder="············" aria-describedby="password">
                                        <span class="input-group-text cursor-pointer"><i class="ti ti-eye-off"></i></span>
                                    </div>
                                    @error('password') <div class="invalid-feedback d-block">{{ $message }}</div> @enderror
                                </div>
                                <div class="mb-3">
                                    <div class="form-check">
                                        <input type="checkbox" class="form-check-input" id="remember" name="remember">
                                        <label for="remember" class="form-check-label">Remember Me</label>
                                    </div>
                                </div>
                                <div class="mb-3">
                                    <button type="submit" class="btn btn-primary d-grid w-100">Log In</button>
                                </div>
                            </form>

resourcesviewsauthadminforgot-password.blade.php:

                            <form action="{{ route('admin.password.email') }}" method="POST" class="mb-3">
                                @csrf
                                <div class="mb-3">
                                    <label for="email" class="form-label">Email Address</label>
                                    <input type="email" class="form-control @error('email') is-invalid @enderror" id="email" name="email" placeholder="Enter your email address" autofocus>
                                    @error('email') <div class="invalid-feedback">{{ $message }}</div> @enderror
                                </div>
                                <button type="submit" class="btn btn-primary d-grid w-100">Send Reset Link</button>
                            </form>

resourcesviewsauthadminreset-password.blade.php:

                            <form action="{{ route('admin.password.update') }}" method="POST" class="mb-3">
                                @csrf
                                <input type="hidden" name="token" value="{{ $request->route('token') }}">
                                <div class="mb-3">
                                    <label for="email" class="form-label">Email Address</label>
                                    <input type="email" class="form-control @error('email') is-invalid @enderror" id="email" name="email" value="{{ $request->email ?? old('email') }}" placeholder="Enter your email address" autofocus>
                                    @error('email') <div class="invalid-feedback">{{ $message }}</div> @enderror
                                </div>
                                <div class="mb-3 form-password-toggle">
                                    <label for="password" class="form-label">New Password</label>
                                    <div class="input-group input-group-merge">
                                        <input type="password" class="form-control @error('password') is-invalid @enderror" id="password" name="password" placeholder="············" aria-describedby="password">
                                        <span class="input-group-text cursor-pointer"><i class="ti ti-eye-off"></i></span>
                                    </div>
                                    @error('password') <div class="invalid-feedback d-block">{{ $message }}</div> @enderror
                                </div>
                                <div class="mb-3 form-password-toggle">
                                    <label for="password" class="form-label">Confirm Password</label>
                                    <div class="input-group input-group-merge">
                                        <input type="password" class="form-control" id="password-confirm" name="password_confirmation" placeholder="············" aria-describedby="password">
                                        <span class="input-group-text cursor-pointer"><i class="ti ti-eye-off"></i></span>
                                    </div>
                                </div>
                                <div class="mb-3">
                                    <button type="submit" class="btn btn-primary d-grid w-100">Reset Password</button>
                                </div>
                            </form>

This is the email I got from Mailtrap if I tried to reset the password for Admin:
Admin Reset Password Link

Which is weird. I thought the links gonna have admin prefix considering my routes using admin prefix for Admin, but for some reason this doesn’t. Can anyone please help me pointing out what I did wrong?

2

Answers


  1. What you could do is override the notification function.
    AFAIK, Fortify use the CanResetPassword trait under the hood.
    So, in your User model, you could implement your own logic, e.g.

    public function sendPasswordResetNotification($token)
    {
        return $this->is_admin 
               ? $this->notify(new AdminResetPasswordMailNotification($this, $token))
               : $this->notify(new UserResetPasswordMailNotification($this, $token));
    }
    

    You can now create your own Notifications and build your links as you wish

    PS: Don’t forget to php artisan optimize:clear before testing

    Login or Signup to reply.
  2. config directory fortify.php file has default guard is ‘web’ you have to change the to "admin" you can do that in

    appProvidersFortifyServiceProvider.php: register method

    public function register()
    {
        if(request()->is('admin/*')) {
            config()->set('fortify.guard','admin');
        } 
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search