Search code examples
authenticationlaravel-5registration

auth check returns true after registration itself in laravel 5.2


I am creating a user registration and login page in laravel 5.2. Both are in same page and separated by navbar tabs. For registeration I used form action and for login, I used ajax in button click. Both works fine. But the problem is, after the registration itself, the login url changes to logout. I need it only after a user login to the site. Below is my registration function.

protected function create(array $data)
{

    $user = User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);

    $meber_role = Role::select('id')->where('slug', 'member')->get();

    $RoleUser = new RoleUser;
    $data['role_id'] = $meber_role[0]->id;
    $RoleUser->fill($data); /* user Role */
    $user->RoleUser()->save($RoleUser);

    return $user;
}

I used the below code in view to check whether a user is loggedin or not. If user loggedin, the login in header changes to logout.

@if(!Auth::check())
    <div class="sign-in">
       <a href="{{url('login')}}" title="login" class="clsComLogin"><i class="icon-user icons"></i><span>Login</span></a>
    </div>
@else
     <div>
        <a href="{{ url('auth/logout') }}"><i class="fa fa-sign-out"></i>&nbsp;Logout</a>
     </div>
@endif

Now the logout url appears in header after registration itself. How to change this situation. Thanks in advance.


Solution

  • User is logged in after registration by default. To change this situation add or overwrite the postRegister() function. This only registers but donot login.

    public function postRegister(Request $request)
    {   
        $validator = $this->validator($request->all());
    
        if ($validator->fails()) {
            $this->throwValidationException(
                $request, $validator
            );  
        }   
    
        $this->create($request->all());
    
        return redirect($this->redirectPath());
    }