簡體   English   中英

社交名流注冊(Laravel 5.4)

[英]Socialite registration (Laravel 5.4)

我正在制作一個Laravel 5.4項目,我希望用戶在注冊並創建用戶之前選擇一所學校(他們將被分配到該學校)。 我的問題是,谷歌登錄名是應用程序的主要登錄方法,用戶創建是在LoginController中處理的。 我正在使用Socialite軟件包進行Google登錄。

如何將用戶和數據從Google回調傳遞到注冊頁面?

這是我的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
    ]);
}

您可以使用刷新會話 嘗試這個:

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);
    }
}

並在您的注冊視圖中

<input type='text' name='email' value='{{session("user")->email}}'>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM