谷歌登录vuejs spa和laravel api

问题描述 投票:8回答:2

我有一个单页应用程序与vue.js使用Laravel 5.5作为其api。我想添加谷歌登录,但我很困惑,从哪里开始。我之前使用laravel socialite添加谷歌登录,但这是纯粹的laravel应用程序而不是laravel api。

任何人都可以指出我能阅读的一些资源。是否有类似社交名媛的包,在这种情况下是否有办法使用社交名媛。

laravel vue.js vuejs2 google-signin laravel-5.5
2个回答
2
投票

看看hellojs

Hellojs让它变得简单。允许您打开弹出窗口以便与社交提供程序一起登录并返回访问令牌。然后在后端使用访问令牌来获取用户。

所以你的js看起来像这样

 hello
  .login(network)
  .then(
    () => {
      const authRes = hello(network).getAuthResponse();
      axios
        .get('api/link/to/sociak/callback',{
            params:{
              access_token : authRes.access_token, 
              provider: network
            }
         })
        .then((response) => {console.log(response.data.token)})})
        .catch((error) => {console.log(error.response.data)})
    },
    (e) => {
      console.log(e)
    }
  )

并在您的控制器中处理它像..

public function handleProviderCallback(Request $request)
{
    $s_user = Socialite::with($request->provider)->stateless()->userFromToken($request->access_token);
    //your logic here...
}

希望它能让你更清楚。我只是在努力解决这个问题,你好js让我的生活变得简单


0
投票

与上一个答案相反,我使用qazxsw poi添加google身份验证窗口并获取令牌(带有'email,profile'范围)。

然后将其发送到laravel后端(安装了vue-hellojs和CORS),该后端检查是否有用户使用该电子邮件并使用laravel google socialite provider作为登录来响应vue SPA。

© www.soinside.com 2019 - 2024. All rights reserved.