I've got the Remember Me option on my Login form which works perfectly.
I'd quite like to offer this option on the Registration form so that the user wouldn't have to log back in after the default session length time.
I've looked inside RegisterController.php which has a validator, create and a redirectTo but I can't see how it actually auth's the user after creating the row, which is presumably how I could bolt on the remember me logic.
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'country' => ['required', 'string'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'country' => $data['country'],
'password' => Hash::make($data['password']),
'last_login_at' => Carbon::now()->toDateTimeString(),
]);
}
protected function redirectTo()
{
Session::flash('message', 'Thank you for registering ' . Auth::user()->name . '! Welcome to site.com!');
Session::flash('alert-class', 'alert-success');
return '/my-account';
}
}
CodePudding user response:
You can overwrite the RegistersUsers trait (located in your RegisterController) which provides this functionality.
CodePudding user response:
Indeed, adding this to the RegisterController is the answer:
public function register(Request $request)
{
$this->validator($request->all())->validate();
event(new Registered($user = $this->create($request->all())));
$this->guard()->login($user, $request->remember);
return $this->registered($request, $user)
?: redirect($this->redirectPath());
}