从Laravel在Nuxt 3授权过程中使用sidebase/nuxt-auth和GoogleProvider获取令牌

huangapple go评论73阅读模式
英文:

Get token from Laravel in Nuxt 3 authorization process using sidebase/nuxt-auth and GoogleProvider

问题

我在 nuxt 3.2.3 中使用 @sidebase/nuxt-auth 0.5.0-rc.0 进行授权。我这样定义了 Google 提供程序:

GoogleProvider.default({
  clientId: 'secret.apps.googleusercontent.com',
  clientSecret: 'secret',
  token: {
    url: "https://api.example.com/api/auth/socialite/google"
  }
})

我添加了 token 选项,就像在这里定义的一样:https://next-auth.js.org/configuration/providers/oauth#token-option

并且添加了 jwt 回调:

async jwt({ token, account }) {
  if (account) {
    token.jwt = account.id_token
  }
  return token
}

一切都正常,包括 Google 授权等等。但在授权过程中,我期望进行 API 请求以从我的 Laravel 应用程序中获取 access_tokenaccount 参数中有一些 access_tokenid_token,但这些不是来自我的应用程序。

几年前,我在 Nuxt 2 中使用了一些不同的设置,像这样,并且它有效:

google: {
  clientId: 'secret.apps.googleusercontent.com',
  clientSecret: 'secret',
  responseType: 'code',
  codeChallengeMethod: '',
  endpoints: {
    token: 'https://api.example.com/api/auth/socialite/google'
  },
  token: {
    property: 'access_token',
  }
}

我是否正确使用了 token 选项?

英文:

I'm using nuxt 3.2.3 with @sidebase/nuxt-auth 0.5.0-rc.0 for authorization.

I defined Google Provider this way:

...
GoogleProvider.default({
  clientId: 'secret.apps.googleusercontent.com',
  clientSecret: 'secret',
  token: {
    url: "https://api.example.com/api/auth/socialite/google"
  }
})
...

So I added token option as it is defined here: https://next-auth.js.org/configuration/providers/oauth#token-option

And also jwt callback:

...
async jwt({ token, account }) {
  if (account) {
    token.jwt = account.id_token
  }
  return token
}
...

Everything works fine like Google authorization and so on, but in the process of authorization I would expect to make an API request to retrieve an access_token from my Laravel application. There are some access_token and id_token in account parameter, but those are not from my application.

I did the same with Nuxt 2 few years ago with some different settings and it worked:

...
google: {
  clientId: 'secret.apps.googleusercontent.com',
  clientSecret: 'secret',
  responseType: 'code',
  codeChallengeMethod: '',
  endpoints: {
    token: 'https://api.example.com/api/auth/socialite/google'

  },
  token: {
    property: 'access_token',
  }
}
...

Am I using the token option the right way?

答案1

得分: 1

After reading this article I found out the solution for my effort to connect Nuxt and nuxt-auth with separate API server.

我的后端应用程序的令牌可以在使用 signIn 回调时获取。不需要在Google提供程序中使用 token 选项。

英文:

After reading this article I found out the solution for my effort to connect Nuxt and nuxt-auth with separate API server.

The token from my backend application is possible to obtain using signIn Callback. No need to use token option in Google Provider.

huangapple
  • 本文由 发表于 2023年4月19日 16:25:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052264.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定