簡體   English   中英

Laravel 身份驗證成功后社交名流登錄未重定向

[英]Laravel socialite login after successful auth not redirecting

我有一個使用谷歌登錄的 laravel 項目。 我正在使用來自 laravel 的社交名媛 package。 在他們在谷歌身份驗證屏幕中確認后,我可以將用戶的信息保存在我的數據庫中,但之后它將始終重定向到登錄。 似乎Auth::login($user)不起作用。 我錯過了什么?

這是我的登錄名 controller

public function redirectToProvider()
{
    return Socialite::driver('google')->redirect();
}


public function handleProviderCallback()
{
    try {
        $user = Socialite::driver('google')->user();
    } catch (\Exception $e) {
        return redirect('/login');
    }

    $existingUser = User::where('email', $user->email)->first();
    if($existingUser){
        //login the user
        \Auth::login($existingUser,true);
        return redirect('/home');
    } else {
        // create a new user
        $newUser                  = new User;
        $newUser->name            = $user->name;
        $newUser->email           = $user->email;
        $newUser->google_id       = $user->id;
        $newUser->avatar          = $user->avatar;
        $newUser->avatar_original = $user->avatar_original;
        $newUser->save();

        \Auth::login($newUser,true);
    }
    return redirect('/home');
}

路線

Route::get('/redirect', 'Auth\LoginController@redirectToProvider');
Route::get('/callback', 'Auth\LoginController@handleProviderCallback');

來自谷歌控制台的授權重定向 URI

http://localhost:8000/callback
http://localhost:8000/home

參考 laravel 使用社交名流與谷歌登錄: 鏈接在這里

當我檢查網絡時。 /home路徑寫為響應 302。 在此處輸入圖像描述

我剛剛通過在我的 session.php 中創建domain null 來解決它。 我不知道這是否是正確的答案,但我現在可以在使用谷歌登錄后重定向到我的主頁。 也許如果我將它部署到生產中,我會將domain更改為我服務器的實際 url。

/*
    |--------------------------------------------------------------------------
    | Session Cookie Domain
    |--------------------------------------------------------------------------
    |
    | Here you may change the domain of the cookie used to identify a session
    | in your application. This will determine which domains the cookie is
    | available to in your application. A sensible default has been set.
    |
    */

    'domain' => null,

    /*
    |--------------------------------------------------------------------------
    | HTTPS Only Cookies
    |--------------------------------------------------------------------------
    |
    | By setting this option to true, session cookies will only be sent back
    | to the server if the browser has a HTTPS connection. This will keep
    | the cookie from being sent to you if it can not be done securely.
    |
    */

    'secure' => false,

暫無
暫無

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

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