Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
649 views
in Technique[技术] by (71.8m points)

php - How to disable auto login on register in Laravel 5?

I am new to Laravel but fell in love with the framework and decided to use it for my project.

I have a field active and by default I've set it to 0. In the Attempt() method, I've set $credentials['active'] = 1. When I logout and login again, this works fine.

But when I register a user, it automatically logs the user in without checking active field.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I assume you are using the AuthenticatesAndRegistersUsers trait in your controller.

The registration is carried by the postRegister() method in that trait, which calls the login() method after creating a new user.

You can override this method in your controller and call the login() method only when the active field is true. So, your postRegister() method will be something like:

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

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

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

    if ($request->get('active')) {
        $this->auth->login($user);
    }

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...