ellajhonm's avatar

Auth Guard 'Client' not logging in

I have a client model that used to be for displaying clients only, but now I'm adding a new feature that allows clients to register and log into my website. I decided to follow the steps such as make the client model extend authenticable, adding a new guard and provider to config/auth.php as shown here: https://hackthestuff.com/article/laravel-multiple-authentication-system-using-different-models

Now, I want the client to automatically log in once they have been successfully registered, and redirect home

// log in the user after account creation
                auth()->guard('client')->attempt([
                    'email' => request()->input('email'),
                    'password' => Hash::make(request()->input('password')),
                ]);
                                return redirect('/');

however when i perform a dd(auth()->guard('client')->check());, it shows false, so it's not logging in? The client gets inserted into the table. I've tried reading the docs but I can't get my head wrapped around it. What am I missing? What's the proper way to log in custom 'user' models like this?

Complete function in controller:

 public function register(){
        $validatedAttributes = request()->validate([
            'name' => ['required','max:100','string'],
            'contact' => ['required', 'digits:11','unique:clients,contact'],
            'email' => ['required', 'email', 'max:255', 'unique:clients,email'],
            'password' => ['required','string',Password::min(8),'confirmed'],
            'address' => ['required','min:5','max:250','string'],
            'image' => ['nullable','mimes:jpg,png,jpeg,gif,svg']
        ]);

        // if there's an image
        if (isset($validatedAttributes['image'])){
            $path = request()->file('image')->store('public/storage/images','s3');
         //    ignore url error, intellisense issue
            $validatedAttributes['image'] = basename($path);

            // hash password first
            $validatedAttributes['password'] = Hash::make(request()->input('password'));
            $newClient = Client::create($validatedAttributes);
        }
        else{
            // if no image
            $newClient = Client::create([
            'name' => request()->input('name'),
            'contact_num' => request()->input('contact_num'),
            'email' => request()->input('email'),
            'password' => Hash::make(request()->input('password')),
            'address' => request()->input('address')
            ]);
        }

                // log in the user after account creation
                auth()->guard('client')->attempt([
                    'email' => request()->input('email'),
                    'password' => Hash::make(request()->input('password')),
                ]);
                                return redirect('/');


    }
0 likes
2 replies
mabdullahsari's avatar

You're probably not passing the guard to the auth middleware. Can you show the Kernel where this middleware is applied?

1 like

Please or to participate in this conversation.