I’m using google drive in my project for login with google.
It’s working fine for me, but the problem is when user select email, in callback method user have to redirect to '/'
, but user will redirect to home
, this is callback method :
public function callback(Request $request) { $googleUser = Socialite::driver('google')->stateless()->user(); $user = User::where('email', $googleUser->email)->first(); if (!$user) { $user = User::create([ 'name' => $googleUser->name, 'email' => $googleUser->email, 'password' => bcrypt(Str::random(16)) ]); } auth()->loginUsingId($user->id); return $this->loggedIn($request, $user) ?: redirect(route('login')); }
It’s ok for next time user login with google, but for the first time redirect to home
.
And in loggedIn function for the first time returned false because two_factor_type
is off in default :
public function loggedIn(Request $request, $user) { if ($user->two_factor_type === 'on') { auth()->logout(); $request->session()->flash('auth', [ 'user_id' => $user->id, 'remember' => $request->has('remember') ]); if ($user->two_factor_type === 'on') { $code = ActiveCode::generateCode($user); //TODO send sms } return redirect(route('login.twoFactor')); } return false; }
Even in my LoginController or RegisterController i changed this :
protected $redirectTo = RouteServiceProvider::HOME;
To this :
protected $redirectTo = '/';
So why it will redirect to home
?
Advertisement
Answer
in app/Http/Controllers/Auth/LoginController
check if the controller protected with middleware, e.g:
public function __construct() { $this->middleware('guest')->except('logout'); //meaning if there is user authenticated not guest, //when he hit function other than logout() //will be redirected to default landing, in code below }
in app/Http/Middleware/RedirectIfAuthenticated
will check if current auth()->user() is authenticated
change the default code to :
public function handle($request, Closure $next, $guard = null) { if (Auth::guard($guard)->check()) { return redirect('/home'); // here change the default redirected } return $next($request); }