DEV Community

Cover image for Laravel API Authentication via Sanctum & Socialite and test on Postman

Laravel API Authentication via Sanctum & Socialite and test on Postman

Philip-Droubi on August 18, 2022

Laravel Sanctum with Socialite API (Updated on 23/9/2022 : Flutter app updated) Hello everyone, this is my first post on DEV.to. In this post, I ...
Collapse
 
rober profile image
Lorem

Great article, I really need this.
Also, thanks for sharing the Flutter app.

Collapse
 
rober profile image
Lorem • Edited

@philipdroubi , Do I need to create a Google app to get client_id and client_secret ?
And does this work with Facebook, Github and other providers??

Collapse
 
philipdroubi profile image
Philip-Droubi

@rober You're welcome.
1- As a backend you don't need to create any google app, frontend should do.
2- Yes it works, But you need to edit your config/services.php file to be like this :

'google' => [
        'client_id' => env('GOOGLE_CLIENT_ID'),
        'client_secret' => env('GOOGLE_CLIENT_SECRET'),
        'redirect' => 'GOOGLE_REDIRECT_URI',
    ],
'facebook' => [
        'client_id' => env('FACEBOOK_CLIENT_ID'),
        'client_secret' => env('FACEBOOK_CLIENT_SECRET'),
        'redirect' => 'FACEBOOK_REDIRECT_URI',
    ],
Enter fullscreen mode Exit fullscreen mode

And the validateProvider function in SocialiteController to be like this :

protected function validateProvider($provider)
    {
        if (!in_array($provider, ['google','facebook'])) {
            return response()->json(["message" => 'You can only login via google or Facebook accounts'], 400);
        }
    }
Enter fullscreen mode Exit fullscreen mode
Collapse
 
gankcc profile image
Suphasit Thongniam

You saved my day.
Cheers mate!

Collapse
 
philipdroubi profile image
Philip-Droubi

My pleasure! 😃

Collapse
 
229okpe profile image
229okpe

where can i find the access_provider_token ?
The redirect is not obligatory?

Collapse
 
philipdroubi profile image
Philip-Droubi • Edited

@229okpe

  • You can get the access_provider_token for testing purpose through this flutter app or through this website.
    And remember that as an API the frontend application must send the access_provider_token through the request.

  • No need for any redirect.

Collapse
 
ahmedali190000 profile image
Ahmed Ali

is there no providers table to know which this user comes from google ,GitHub or Facebook ??
and how mobile developer sends me access_token_provider in callback ??

Collapse
 
philipdroubi profile image
Philip-Droubi • Edited

@ahmedali190000
1- Yes, of course, if you want to store from which provider each user came from, you must have a providers_table.

2- Mobile developers can send you the access_token_provider in the request parameters or body, but if you mean the token itself it's better to send it using the request body as it may be too long to be in the request parameters.

Collapse
 
keyvervelasquez profile image
Keyver Velásquez Guerra

Bro!, Thank you!

Collapse
 
philipdroubi profile image
Philip-Droubi

@keyvervelasquez You're welcome.