Laravel default Auth system is great, but sometimes there’s a need that user would register but NOT log in automatically. The problem is that Laravel does auto-login by default after registration. Worry no more, there is a simple solution for that!
Let’s start with fresh Laravel 5.2 installation and ‘php artisan make:auth’ command called. Of course this will work in existing projects too!
Next, let’s create a nice success page: (for the purpose of our tutorial I’m keeping it really simple)
@extends('layouts.app') @section('content') <div class="container"> <div class="row"> <div class="col-md-10 col-md-offset-1"> <div class="alert alert-success"> Success! You have registered, please check your email for more instructions! </div> </div> </div> </div> @endsection
* File was saved under /resources/views/success.blade.php
Now we add a route, in which user will be met with our success message:
Route::get('/auth/success', [ 'as' => 'auth.success', 'uses' => 'Auth\AuthController@success' ]);
And finally – open app/Http/Controllers/Auth/AuthController.php and make these changes:
1. Add new method:
public function success() { return view('success'); }
2. Override ‘register’ method:
/** * Handle a registration request for the application. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function register(Request $request) { $validator = $this->validator($request->all()); if ($validator->fails()) { $this->throwValidationException( $request, $validator ); } $this->create($request->all()); return redirect(route('auth.success')); // Change this route to your needs }
That is it! By overriding AuthController::register() method and changing
Auth::guard($this->getGuard())->login($this->create($request->all()));
to
$this->create($request->all());
in the code – you’ve disabled auto-login function. Now your users will see a nice success page instead of being logged in directly!
You disabled not only auto-login, but registration too hahaha.
You forgot the “$this->create($request->all());” method call.
Thanks for the comment and pointing out the mistake, it’s all fixed now!
[…] Register without login on LaravelDaily […]
Thanks a ton. Saved my day 🙂
Thanks for the pointers, very useful. I found in my version of Laravel (5.3.25) I needed to make a few changes to the RegisterController.
use Illuminate\Http\Request;
use Illuminate\Auth\Events\Registered;
/**
* Handle a registration request for the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
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());
}
Might be useful for someone 🙂
I think a better solution is to override the `registered` method (which is meant to be overriden) and to write the following:
“`
public function registered(Request $request, )
{
$this->guard()->logout();
}
“`
replace
public function registered(Request $request, )
{
$this->guard()->logout();
}
public function registered(Request $request, )
{
$this->guard();
}
What about Laravel 5.4 ?
there is no Authcontroller here ..How we gonna solve the issue?
Temporarily I have to modify the register method of the RegistersUsers trait, and looking for a suggestion also.
hi how could i show a session massage to user after register in login page and i know that i use ->with after redirect but where should i use this