如何从Laravel Socialite Google用户获取城市,性别,年龄,州信息?

时间:2019-02-23 06:05:16

标签: php laravel laravel-5 google-api-php-client laravel-socialite

我已经制作了一个控制器,该控制器目前为我提供名称,电子邮件ID,图片,但我还想要在 socialite laravel中注册用户的城市,性别,年龄,州信息。

我正在使用laravel 5.6。建议我详细介绍一下。

这是我的控制人

<?php

namespace App\Http\Controllers;
use App\User;
use Auth;
use Socialite;

class SocialAuthGoogleController extends Controller 
    {
    public function redirect() {
        return Socialite::driver('google')->redirect();
    }

    public function callback() {
        $user = Socialite::driver('google')->stateless()->user();
        dd($user);
        try {

            $googleUser = $user;
            $existUser = User::where('email', $googleUser->email)->first();

            if ($existUser) {
                Auth::loginUsingId($existUser->id);
            } else {
                $user = new User;
                $user->name = $googleUser->name;
                $user->email = $googleUser->email;
                $user->provider = 'google';
                $user->role = 'user';
                $user->provider_id = $googleUser->id;
                $user->password = md5(rand(1, 10000));
                $user->save();
                Auth::loginUsingId($user->id);
            }
            return redirect()->to('/google/login');
        } catch (Exception $e) {
            return $e;
        }
    }
}

0 个答案:

没有答案