kkhicher1
1 month ago
183
5
Laravel

Multi Role Social Login or Register Laravel

Posted 1 month ago by kkhicher1

i want to register a user on login if user isn't exist but i have 3 role. and i want to assign role on the social login but redirect url make me confuse. have a look to my code. i use socialite package. and also jwt package

.env

FACEBOOK_CLIENT_ID=
FACEBOOK_CLIENT_SECRET=
FACEBOOK_REDIRECT='url'


GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
GOOGLE_REDIRECT='url'

TWITTER_CLIENT_ID=
TWITTER_CLIENT_SECRET=
TWITTER_REDIRECT='url'

service.php in config

'facebook' => [
        'client_id' => env('FACEBOOK_CLIENT_ID', ''),
        'client_secret' => env('FACEBOOK_CLIENT_SECRET', ''),
        'redirect' =>  env('FACEBOOK_REDIRECT', ''),
    ],

    'google' => [
        'client_id' => env('GOOGLE_CLIENT_ID', ''),
        'client_secret' => env('GOOGLE_CLIENT_SECRET', ''),
        'redirect' =>  env('GOOGLE_REDIRECT', ''),
    ],
    'twitter' => [
        'client_id' => env('TWITTER_CLIENT_ID', ''),
        'client_secret' => env('TWITTER_CLIENT_SECRET', ''),
        'redirect' =>  env('TWITTER_REDIRECT', ''),
    ],

Routes

//seller social Login

Route::get('/redirect/{service}/seller', 'Auth\[email protected]');
Route::get('/callback/{service}/seller', 'Auth\[email protected]');

//buyer social Login

Route::get('/redirect/{service}/buyer', 'Auth\[email protected]');
Route::get('/callback/{service}/buyer', 'Auth\[email protected]');

SocialAuthController


<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Laravel\Socialite\Facades\Socialite;

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

    public function callback($service)
    {
        $userSocial = Socialite::with($service)->stateless()->user();

        $user = User::where(['email' => $userSocial->getEmail()])->first();
        if ($user) {
            if (!$token = auth('api')->login($user)) {
                return response()->json(['error' => 'Unauthorized'], 401);
            }
            return $this->respondWithToken($token);
        } else {
            $user = User::create([
                'name'          => $userSocial->getName(),
                'email'         => $userSocial->getEmail(),
            ]);
            $user->userProvider()->create(
                [
                    'image'         => $userSocial->getAvatar(),
                    'provider_id'   => $userSocial->getId(),
                    'provider'      => $service,
                ]
            );
            if ($user){
                if(\request()->segment(4) == "seller"){
                    $user->assignRole('seller');
                }elseif (\request()->segment(4) == "buyer"){
                    $user->assignRole('buyer');
                }
                if (!$token = auth('api')->login($user)) {
                    return response()->json(['error' => 'Unauthorized'], 401);
                }
                return $this->respondWithToken($token);
            }
        }
    }

    /**
     * Get the token array structure.
     *
     * @param string $token
     *
     * @return \Illuminate\Http\JsonResponse
     */
    protected function respondWithToken(string $token)
    {
        $access_url = auth('api')->user()->roles[0]->name ?: null;
        return response()->json([
            'access_token' => $token,
            'token_type' => 'bearer',
            'expires_in' => auth('api')->factory()->getTTL() * 60,
            'access_url' => '/' . $access_url
        ]);
    }
}


Please sign in or create an account to participate in this conversation.