E
E
Eternal972021-06-03 10:34:19
Laravel
Eternal97, 2021-06-03 10:34:19

How to override REDIRECT_URI for Laravel Socialite depending on the type of user being authenticated?

Good day. I ran into a problem when authenticating through social networks for several entities. There are two entities: Student and Teacher. To begin with, I will describe a working version for one entity.
My authentication routes:

//Socialite auth routes...
Route::middleware('guest')->group(function () {
    Route::get('auth/{provider}', '[email protected]')->name('auth.provider');
    Route::get('auth/{provider}/callback', '[email protected]');
});

My controller:
class SocialController extends Controller
{
    public function index($provider)
    {
        return Socialite::driver($provider)->redirect();
    }

    public function callback(SocialService $socialService, $provider)
    {
        $user = Socialite::driver($provider)->user();
        if ($u = $socialService->saveSocialData($user)) {
            Auth::guard('student')->login($u);
            return redirect()->route('profile.index');
        }

        return back();
    }
}

My config:
VKONTAKTE_CLIENT_ID=7864077
VKONTAKTE_CLIENT_SECRET=Z2i92xXe47mGJY3D5DQE
VKONTAKTE_REDIRECT_URI=http://127.0.0.1:8000/auth/vkontakte/callback

So everything works fine, but for the entity Student.
I also want to pass the name of the entity to the route parameter (to select the appropriate model, guard and redirect in the controller), like this to the $client variable:
//Socialite auth routes...
Route::middleware('guest')->group(function () {
    Route::get('auth/{client}/{provider}', '[email protected]')->name('auth.provider');
    Route::get('auth/{client}/{provider}/callback', '[email protected]');
});

But, I'm facing a REDIRECT_URI route configuration issue:
VKONTAKTE_REDIRECT_URI=http://127.0.0.1:8000/auth/student/vkontakte/callback
VKONTAKTE_REDIRECT_URI=http://127.0.0.1:8000/auth/teacher/vkontakte/callback

If I write two routes in the config, then the variable will simply be overridden and always return the last route for any entity.
How can REDIRECT_URI be dynamically changed depending on the passed parameters?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eternal97, 2021-06-07
@Eternal97

Solved the problem like this:
Authorization controller

class SocialController extends Controller
{
    public function index(SocialService $socialService, $client, $provider)
    {
        $socialService->setConfig($client, $provider);

        return Socialite::driver($provider)->redirect();
    }

    public function callback(SocialService $socialService, $client, $provider)
    {
        $socialService->setConfig($client, $provider);
        $user = Socialite::driver($provider)->user();
        if ($u = $socialService->saveSocialData($client, $user)) {
            Auth::guard($client)->login($u);
            return redirect()->route('profile.index');
        }

        return back();
    }
}

Service Layer:
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Hash;

class SocialService
{
    public function saveSocialData($client, $user)
    {
        if ($client == 'student') {
            $model = new Student();
        }
        if ($client == 'teacher') {
            $model = new Teacher();
        }

        $email = $user->getEmail();

        $u = $model->where('email', $email)->first();

        if ($u == null) {
            $data = [
                'first_name' => 'First',
                'middle_name' => 'Middle',
                'last_name' => 'Last',
                'birth' => today(),
                'email' => $email,
                'password' => Hash::make('secret123'),
            ];
            return $model->create($data);
        }

        return $u;
    }

    public function setConfig($client, $provider)
    {
        if ($provider == 'vkontakte') {
            $clientId = env('VKONTAKTE_CLIENT_ID');
            $clientSecret = env('VKONTAKTE_CLIENT_SECRET');
            $redirectUrl = 'http://127.0.0.1:8000/auth/'.$client.'/vkontakte/callback';
        }
        if ($provider == 'google') {
            $clientId = env('GOOGLE_CLIENT_ID');
            $clientSecret = env('GOOGLE_CLIENT_SECRET');
            $redirectUrl = 'http://127.0.0.1:8000/auth/'.$client.'/google/callback';
        }
        Config::set(['services.'.$provider => [
            'client_id' => $clientId,
            'client_secret' => $clientSecret,
            'redirect' => $redirectUrl
        ]]);
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question