【发布时间】:2019-01-21 23:44:34
【问题描述】:
如何将社交名流回调中获得的访问令牌返回给客户端? 我需要将此数据返回给 vue 以便用户登录。 token data
这是我在 vue 中获取重定向 url 的代码
googleLogin(){
const self = this;
let socialLogin = {
name: 'google'
};
self.$http.post(`api/googleLogin`, socialLogin)
.then(response => {
console.log('GOOGLE LOGIN RESPONSE ', response.body);
window.location = response.body;
})
}
对于后端
public function googleLogin(Request $request){
$socialType = $request->name;
return response()->json(
Socialite::driver($socialType)
->with(['social_type' => $socialType])
->stateless()
->redirect()
->getTargetUrl()
);
}
public function googleLoginCallback(){
$http = new \GuzzleHttp\Client;
$user = Socialite::with('google')->stateless()->user();
$userCredentials = [
'token' => $user->token,
'refreshToken' => $user->refreshToken,
'expiresIn' => $user->expiresIn,
];
$response = $http->post(env('LOGIN_ENDPOINT'), [
'form_params' => [
'grant_type' => 'social',
'client_id' => env('CLIENT_ID'),
'client_secret' => env('CLIENT_SECRET'),
'network' => 'google',
'access_token' => $userCredentials['token'],
]
]);
return json_decode((string) $response->getBody(), true);
}
【问题讨论】:
标签: vue.js single-page-application laravel-5.6 laravel-socialite