Detect Laravel fortify login and do something

652 Views Asked by At

I'm using Fortify in my Laravel 8 application that's used as an API backend to a Nuxt front-end. When a user logs in, and their log in is successful; I'd like to perform some custom functionality to update things in my database after a user logs in, how can I achieve this?

Originally posted here and suggested to use Stackoverflow

1

There are 1 best solutions below

0
On

You could write a custom user authentication inside the FortifyServiceProvider file's boot function.

So for an example, you can do this:

    Fortify::authenticateUsing(function (Request $request) {

        $user = User::where('email', $request->email)->first();

        // If the user exists with the provided credentials
        if ($user && Hash::check($request->password, $user->password)) 
        {
            // update your database stuff here

            return $user;
        }

        return null;
    });

So this would execute and run your custom functionality BEFORE the user gets logged in, but since you're checking beforehand if everything is in order with the provided login credentials, you can do what you need.

Hope this helps.