Asked  9 Months ago    Answers:  5   Viewed   116 times

I'm using Laravel 5.4 and Socialite so my users can log in with Facebook.

My website works with subdomain newyork.example.com, paris.example.com example.com

The Facebook URL redirect for the callback login has to be absolute so I set http://example.com

login/facebook

public function redirectToProvider()
    {

        $_SESSION['originalURL'] ="http://paris.example.com";

        return Socialite::driver('facebook')
            ->scopes(['rsvp_event', 'public_profile'])
            ->redirect();
    }

login/facebook/callback

 public function handleProviderCallback(SocialAccountService $service)
    {
        $user = $service->createOrGetUser(Socialite::driver('facebook')->user());
        // $user->token;

        Auth::login($user, true);

        $originalURL = $_SESSION['originalURL'];

        return redirect()->to($originalURL);
    }

Problems

When I am in the route login/facebook I can view the original URL paris.example.com with HTTP_POST

When I am in the route login/facebook/callback the HTTP_POST is example.com since the URL of the redirection is example.com. I tries to save the URL in a session var but $_SESSION is empty.

Questions

How I can do to get the original url after the facebook login callback redirection. ? So if I started the login process with paris.example.com I'm redirected to example.com and then I get the save url to be redirected

sessions.php

'cookie' => 'laravel_session',

    /*
    |--------------------------------------------------------------------------
    | Session Cookie Path
    |--------------------------------------------------------------------------
    |
    | The session cookie path determines the path for which the cookie will
    | be regarded as available. Typically, this will be the root path of
    | your application but you are free to change this when necessary.
    |
    */

    'path' => '/',

    /*
    |--------------------------------------------------------------------------
    | 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' => env('SESSION_DOMAIN', '.localhost.com'),

    /*
    |--------------------------------------------------------------------------
    | 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' => env('SESSION_SECURE_COOKIE', false),

    /*
    |--------------------------------------------------------------------------
    | HTTP Access Only
    |--------------------------------------------------------------------------
    |
    | Setting this value to true will prevent JavaScript from accessing the
    | value of the cookie and the cookie will only be accessible through
    | the HTTP protocol. You are free to modify this option if needed.
    |
    */

    'http_only' => true,

 Answers

83

The key was not to use $_SESSION but session(['city' => 'paris']); and session('city') to retrieve the value.

Wednesday, March 31, 2021
 
dimitarvp
answered 9 Months ago
62

After a long search, the solution is to remove the profile from scopes in vendorlaravelsocialitesrcTwoGoogleProvider.php

protected $scopes = [
    'openid',
    'email',
];
Friday, May 28, 2021
 
Sufi
answered 7 Months ago
35

You can use a named route to solve this issue:

Lets make a named route like this:

For Get

Route::get('user/additional-fields',array(
    'uses' => 'UserController@getAdditionalFields',
    'as'   => 'user.getAdditionalFields'
));

For post

Route::post('user/additional-fields',array(
    'uses' => 'UserController@postAdditionalFields',
    'as'   => 'user.postAdditionalFields'
));

So we can now ensure Laravel uses the right route by doing this

return redirect()->guest(route('user.getAdditionalFields'));

Also note that its not possible to redirect a POST because Laravel expects form to be submitted. SO you can't do this:

return redirect()->guest(route('user.postAdditionalFields'));

except you use something like cURL or GuzzleHttp simulate a post request

Saturday, May 29, 2021
 
PHLAK
answered 7 Months ago
46

Simply fetch the data using file_get_contents function and process the retrieved data.

use File; 

create a helper method to get the avatar

function getSocialAvatar($file, $path){
    $fileContents = file_get_contents($file);
    return File::put(public_path() . $path . $user->getId() . ".jpg", $fileContents);
}

replace $user->getAvatar()

with the helper method

getSocialAvatar($user->getAvatar(), 'path')

Saturday, May 29, 2021
 
wavyGravy
answered 7 Months ago
83

Update:

The Laravelcollective/html has been updated to Laravel 5.4 but there is no any changes in the documentation till now. If you want to see the pull request it is right here:

https://github.com/LaravelCollective/html/pull/276

https://github.com/LaravelCollective/html/pull/284

Just use:

composer require "laravelcollective/html":"^5.4"

Or,

composer require laravelcollective/html

If you don't want to specify the version and get the same latest version of LaravelCollective.

Older Issue:

Laravelcollective/html v5.3.0 requires Laravel 5.3 and not yet supports Laravel 5.4.

The laravelcollective/html v5.4 is under open issue:

https://github.com/LaravelCollective/html/pull/276

It could be merged any time.

Just have an eye on it.

Edit:

Alternatively you could use maddhatter's git repository as he had done some changes that is not yet provided in git@github.com:st3f/html.git repository.

Just add it into your composer.json

"repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/maddhatter/html.git"
        }
],
"require": {
     "laravelcollective/html": "5.4.x-dev",
}

and run

composer update

Or, if you don't need those changes you could follow Cerlin Boss answer

https://stackoverflow.com/a/41845331/3887342

Sunday, September 26, 2021
 
BaajiRao
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share