Home > Net >  How user is logged in after registeration automatically in laravel?
How user is logged in after registeration automatically in laravel?

Time:04-30

I am working on Auth and used Php artisan ui auth. Everything is setup but I can't understand the process. When user is registering he is taken to trait RegistersUsers Inside this trait they are taken to

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

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

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

        if ($response = $this->registered($request, $user)) {
            return $response;
        }

        return $request->wantsJson()
                    ? new JsonResponse([], 201)
                    : redirect($this->redirectPath());
    }

and what I have understood is that they are logged in via this line in register function above

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

So I went to check its definition which lead me to StatefulGuard.php file and inside it there was

public function login(Authenticatable $user, $remember = false);

which I assume did the login part and logged the user into application but thing that isn't clear to me is that where is definition or where is the above login function defined as I am unable to find it and at first I thought it is using login function of trait AuthenticatesUsers but it isn't as I placed dd inside login function in trait AuthenticatesUsers but it didn't worked.

So is there a separate login function that registration uses ? and where is the function defined that I mentioned that is in StatefulGuard.php?

CodePudding user response:

Use This

public function register(Request $request)
{
    // if registration is closed, deny access
    if (!config('backpack.base.registration_open')) 
    {
        abort(403, trans('backpack::base.registration_closed'));
    }

    $this->validator($request->all())->validate();

    $this->guard()->login($this->create($request->all()));

    return redirect($this->redirectPath());
}

CodePudding user response:

you can logged in user after registeration

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

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

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

        Auth::loginUsingId($user->id);

        if ($response = $this->registered($request, $user)) {
            return $response;
        }

        return $request->wantsJson()
                    ? new JsonResponse([], 201)
                    : redirect($this->redirectPath());
    }

or use laravel ui package

  • Related