May Sale! All accounts are 40% off this week.

mozew's avatar
Level 6

How to login after enter verification code in laravel?

We have a page with a verification code that redirects to this page after the user logs in. I want to login after clicking the user confirmation button.

code

public function send(Request $request)
{
    $user = User::whereCode($request->code)->whereMobile($request->mobile)->first();
    if($user){
        $user->verification_code = 1;
        $user->save();
        
        $this->validator($request->all())->validate();

        event(new Registered($user = $this->create($request->all())));

        $this->guard()->login($user);

        return $this->registered($request, $user)
                ?: redirect($this->redirectPath());
        
    } else {
        return redirect()->back();
    }
}
0 likes
1 reply
Nakov's avatar
Nakov
Best Answer
Level 73

Have you tried using the facade?

Auth::login($user);

And just as a side note, you should validate the request before you try to do anything else, so I would move the validation line as a first line in the method.

1 like

Please or to participate in this conversation.