I'm making a Laravel 5.4 project where I want users to select a school (which they will be assigned to) before they register and the user is created. My problem is that a google login is the main login method of the application and the user creation is handled in the LoginController. I'm using the Socialite package for the google login.
How do I pass the user and the data from the google callback to the register page?
This is my LoginController:
public function redirectToProvider()
{
return Socialite::driver('google')->redirect();
}
public function handleProviderCallback()
{
try
{
$user = Socialite::driver('google')->user();
}
catch (Exception $e)
{
return redirect('auth/google');
}
$authUser = $this->findOrCreateUser($user);
Auth::login($authUser, true);
return redirect('/home');
}
public function findOrCreateUser($googleUser)
{
$authUser = User::where('google_id', $googleUser->id)->first();
if ($authUser) {
return $authUser;
}
return User::create([
'name' => $googleUser->name,
'email' => $googleUser->email,
'google_id' => $googleUser->id,
'avatar' => $googleUser->avatar
]);
}
You can use flashed session. Try this:
public function handleProviderCallback()
{
try
{
$user = Socialite::driver('google')->user();
}
catch (Exception $e)
{
return redirect('auth/google');
}
$authUser = User::where('google_id', $user->id)->first();
if($authUser) //if user is found
{
Auth::login($authUser, true);
return redirect('/home');
}
else //if user is not found, redirect to register page
{
return redirect('/register')->with('user', $user);
}
}
and in your register view
<input type='text' name='email' value='{{session("user")->email}}'>