Skip to content
Advertisement

Login user right after registration – laravel

I am trying to login a user right after registration with the code below.
I am not seeing anything wrong in my code by I get the following error

Method [guard] does not exist.

I have as well imported use

IlluminateContractsAuthGuard;

RegisterUser

public function register(Request $request)
{
    $validator = $this->validator($request->all());

    if ($validator->fails()) {
        $this->throwValidationException(
            $request, $validator
        );
    }

    // $this->create($request->all());
    $user = $this->create($request->all());

    UserVerification::generate($user);
    UserVerification::send($user, 'My Custom E-mail Subject');

    $this->guard()->login($user)->with('status', 'Registration successfully done.');

    return $this->registered($request, $user)
        ?: redirect($this->redirectPath());

}

Advertisement

Answer

There is no method named guard in your controller. Use the login method in Auth.
Use Auth::login($user); to manually log in a user

public function register(Request $request)
{
    $validator = $this->validator($request->all());

    if ($validator->fails()) {
        $this->throwValidationException(
            $request, $validator
        );
    }

    // $this->create($request->all());
    $user = $this->create($request->all());

    UserVerification::generate($user);
    UserVerification::send($user, 'My Custom E-mail Subject');

    Auth::login($user);

    return $this->registered($request, $user)
        ?: redirect($this->redirectPath());

}

If you want to use a custom guard you can specify the guard Auth::guard('guard-name')->login($user)

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement