jeudi 8 décembre 2022

Replacing Google OAuth API in Laravel

I have an app that I was tasked with to renew. However, the app runs a Google OAuth API to authenticate the users. However, this instance of the API no longer works as the company has changed name and thus the old mail domain no longer exists.

E.g: name@companyname.com

Is there a way for me to change the instance of the api so it will allow any gmail to get in.

here's my current controller for the oauth

public function checkUserByToken($social_token)
    {
        $client  = new \Google_Client(['client_id' => env('GOOGLE_CLIENT_ID', '')]);
        $payload = $client->verifyIdToken($social_token);
        if ($payload) {
            $validator = Validator::make($payload, [
                'email' => 'required|email|regex:/(.*)oldcompany.com$/i',
            ]);
            if ($validator->fails()) {
                return false;
            }
            $user = User::where('email', $payload['email'])->first();
            if (!$user) {
                $data = [
                    'name'      => $payload['family_name'],
                    'full_name' => $payload['name'],
                    'email'     => $payload['email'],
                    'password'  => bcrypt(str_random(8)),
                ];
                $user = $this->createUser($data);
            }
            $user->forceFill([
                'email' => $payload['email'],
                'email_verified_at' => Carbon::now(),
            ])->save();
            $tokenResult = $user->createToken('Personal Access Client');
            $token = $tokenResult->token;
            $token->expires_at = Carbon::now()->addMonth();
            $token->save();
            $data = [
                    'access_token' => $tokenResult->accessToken,
                    'token_type'   => 'Bearer',
                    'expires_at'   => Carbon::parse($tokenResult->token->expires_at)->toDateTimeString(),
                    'full_name'    => $payload['name'],
                    'avatar'       => $payload['picture'],
                    'role'         => $user->role,
                    'section'      => isset($user->section)?$user->section->name:"",
                    'id'           => $user->id
            ];
            return $data;
        } else {
            return false;
        }
    }

I have tried replacing the google OAuth API in .env and change

 $validator = Validator::make($payload, [
                'email' => 'required|email|regex:/(.*)oldcompany.com$/i',
            ]);

to

 $validator = Validator::make($payload, [
                'email' => 'required|email|regex:/(.*)newcompany.com$/i',
            ]);

no avail as I think the google API outside of sending back auth token also send back something else but I'm not sure what it is.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire