PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, March 11, 2022

[FIXED] How to disable auto login on register in Laravel 5?

 March 11, 2022     laravel-5, php     No comments   

Issue

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.


Solution

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());
}


Answered By - Marco Pallante
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing