Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ngh's avatar
Level 1

Auto Login After Registration

Hi,

(I'm using Laravel 5.3 and the basic Laravel Auth system...)

I'm trying to get my site to automatically login after the user registers. From Googling it seems like this should be the default behaviour, this but it doesn't seem to be working for me. The user is definitely being created so the problem isn't there. I thought I'd need to edit the RegisterController create() method but I'm not sure exactly what I should be putting in there. Also, if it's not doing the default behaviour I'd rather fix that than put something in to bodge it!

Any help would be appreciated!

Many thanks, Nick

0 likes
14 replies
iiCe89's avatar

have you checked the LoginController ? http/controllers/auth/Logincontroller

use AuthenticatesUsers;

/**
 * Where to redirect users after login.
 *
 * @var string
 */
protected $redirectTo = 'yournamehere';
1 like
ngh's avatar
Level 1

Hi,

Thanks for the quick reply!

I've got it to redirect to the page I want it to go to... but it shows the login form rather than what they would see if they were logged in. What I want is after the user has registered that they don't need to enter their username and password again if that makes sense!

1 like
iiCe89's avatar

ok so it never shows them the form , when returning to site after logging in the first time?

ngh's avatar
Level 1

Sorry, I don't understand!

What the user goes through now when they register is:

Registration Page -> Login Page -> Dashboard

What I want is:

Registration Page -> Dashboard

The site physically redirects the user to the dashboard upon registering correctly, but because the user is not authenticated it's still showing the login page rather than the dashboard.

ngh's avatar
Level 1

Thanks - I had come across that before but couldn't get it to work!

I edited my RegisterController create() method to:

    protected function create(array $data)
    {
        
        $user = User::create([
            'name'      => $data['name'],
            'email'     => $data['email'],
        'role'      => 0,
            'password'  => bcrypt($data['password']),
        ]);

        if (Auth::attempt(['email' => $data['email'], 'password' => $data['password']])) {
            return $user;
        }
    }

But was getting the error:

Type error: Argument 1 passed to Illuminate\Auth\SessionGuard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, null given, called in /vendor/laravel/framework/src/Illuminate/Foundation/Auth/RegistersUsers.php 
ngh's avatar
Level 1

...so it looks like the Auth::attempt is failing so it's returning null rather than the $user.

dd(Auth::attempt(['email' => $data['email'], 'password' => $data['password']])) returns false. Any idea why that might be when it's just created the user with those credentials?!

ngh's avatar
Level 1
Auth::loginUsingId($user->id);

fails too! This is driving me mad!

dd([$user->id, Auth::loginUsingId($user->id)]);

returns:

array:2 [▼
  0 => 48
  1 => false
]
Jaytee's avatar

This is how the auto-login happens in 5.3 from the RegistersUsers trait in the RegisterController:

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

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

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

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

Are you sure you have this? Copy that into your register controller and override it.

You can also add the registered method to your registercontroller too.

protected function registered(Request $request, $user)
    {
        return Auth::loginUsingId($user->id);
    }
ngh's avatar
Level 1

Thanks, I've tried copying and pasting the above in... It complained about not being able to find Registered so I added

use Illuminate\Auth\Events\Registered;

but it still didn't work....

Then it hit me that I had got a scope on the user model... deleted that and hey presto it worked! Sorry for wasting your time - I'm an idiot!

iiCe89's avatar

Trial & error :) your by no means an idiot bud ????

Jaytee's avatar

You're not an idiot haha. Sometimes with brand new projects, things don't work out of the box no matter what you try.

Next time, if you didn't modify anything and it's a brand new project and you can't figure out why it's not working, just make a new project :)

1 like
ngh's avatar
Level 1

Haha thanks to the both of you :) Will do

1 like
sherwinmdev's avatar

you should use an if statement to ensure the user was created successfully before logging them in. did you get it to work?

Please or to participate in this conversation.