Laravel session is lost or not created on redirect

Viewed 379

We are trying to setup the Facebook social connect on our Laravel application, but it seems like we have an issue on session creation.

Here is the code for the Controller :

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Laravel\Socialite\Facades\Socialite;
use App\Services\SocialAuthService;

class SocialAuthController extends Controller
{
    public function redirect()
    {
        return Socialite::driver('facebook')->redirect();
    }

    public function callback(SocialAuthService $service)
    {
        $user = $service->createOrGetUser(Socialite::driver('facebook')->stateless()->user());
        auth()->login($user);

        return redirect()->intended('/');
    }
}

And the code for the service :

<?php

namespace App\Services;

use Laravel\Socialite\Contracts\User as ProviderUser;
use Myproject\Users\User;
use Myproject\Users\SocialLogin;


class SocialAuthService
{
    public function createOrGetUser(ProviderUser $providerUser)
    {
        $account = SocialLogin::where('provider', '=', 'facebook')
                              ->where('provider_user_id', '=', $providerUser->getId())
                              ->first();

        if ($account) {
            return $account->user;
        }
        $user = User::where('email', '=', $providerUser->email)->first();

        if (!$user) {
            $fullname = explode(' ', $providerUser->getName());
            $user = User::create([
                'email' => $providerUser->getEmail(),
                'firstname' => $fullname[0],
                'lastname'  => $fullname[1],
                'password' => md5(rand(1, 9999)),
            ]);
        }
        $account = new SocialLogin([
            'provider_user_id' => $providerUser->getId(),
            'provider' => 'facebook'
        ]);
        $account->user()->associate($user);
        $account->save();

        return $user;
    }
}

And finally the Model :

<?php

namespace Myproject\Users;

use Illuminate\Database\Eloquent\Model;
use Myproject\Users\User;

class SocialLogin extends Model
{

    protected $table = 'social_logins';
    protected $fillable = ['user_id', 'provider_user_id', 'provider'];

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

When we're trying to connect via Facebook, the information is correctly insert in Database, and the callback URL set on Facebook Developers correspond to what we have in our .env, so the redirection is correctly done but at the end we don't have any session created for the user.

I think the issue comes from cross-domain, here are the interesting parts of our .env file :

APP_URL=https://www.website.com
APP_DOMAIN=website.com
SESSION_DOMAIN=.website.com

CACHE_DRIVER=redis
SESSION_DRIVER=redis
SESSION_LIFETIME=120

FACEBOOK_REDIRECT=https://www.website.com/callback/facebook
GOOGLE_REDIRECT=https://www.website.com/auth/google/callback

And our routing on web.php :

Route::domain('{subdomain}.{domain}')->middleware('locale')->group(function () {
    Route::get('/callback/facebook', 'Auth\SocialAuthController@callback');
    Route::get('/redirect/facebook', 'Auth\SocialAuthController@redirect');
});

I really think the issue is located on routing or SESSION_DOMAIN, but we tried to :

  • delete the session domain
  • routing outside the middleware locale, in a middleware auth

It still doesn't affect the login.

0 Answers
Related