mercredi 26 février 2020

How to split given Google API getName value into first_name and last_name using Laravel and Google API login system?

I have created a user table and trying to implement Google API Login system.

My users table has fields for first_name and last_name, but Google API provides only a getName request, how could I split the given value of getName to two pecies and fill my migration table with given values?

LoginController:

    public function handleProviderCallback()
{
    try {
        $user = Socialite::driver('google')->user();
    } catch (\Exception $e) {
        return redirect()->to('/');
    }

    $existingUser = User::where('email', $user->getEmail())->first();

    if ($existingUser) {
        auth()->login($existingUser, true);
    } else {

        $newUser                    = new User;
        $newUser->provider_name     = 'google';
        $newUser->provider_id       = $user->getId();
        $newUser->first_name        = $user->getName();
        $newUser->last_name         = $user->getName();
        $newUser->email             = $user->getEmail();
        $newUser->email_verified_at = now();
        $newUser->save();

        auth()->login($newUser, true);
    }

    return redirect($this->redirectPath());
}
}

I know there is a way to make it happen with custom Php script and the answer is here Split text string into $first and $last name in php

But can't figure out how to implement it with my LoginController function handleProviderCallback

P.S I would not want to change my users_table to have only one column for name



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2wajiwd
via IFTTT

Aucun commentaire:

Enregistrer un commentaire