providers/google
GoogleProfile
扩展
- Record<- string,- any>
属性
aud
aud: string;azp
azp: string;email: string;email_verified
email_verified: boolean;exp
exp: number;family_name?
optional family_name: string;given_name
given_name: string;hd?
optional hd: string;iat
iat: number;iss
iss: string;jti?
optional jti: string;locale?
optional locale: string;name
name: string;nbf?
optional nbf: number;picture
picture: string;sub
sub: string;default()
default<P>(options): OAuthConfig<P>在您的页面中添加 Google 登录。
设置
回调 URL
https://example.com/api/auth/callback/google配置
import { Auth } from "@auth/core"
import Google from "@auth/core/providers/google"
 
const request = new Request(origin)
const response = await Auth(request, {
  providers: [
    Google({ clientId: GOOGLE_CLIENT_ID, clientSecret: GOOGLE_CLIENT_SECRET }),
  ],
})资源
注意
默认情况下,Auth.js 假设 Google 提供者基于 Open ID Connect 规范。
在创建凭据时使用的“授权重定向 URI”必须包含您的完整域名,并在回调路径中结束。例如;
- 对于生产:https://{YOUR_DOMAIN}/api/auth/callback/google
- 对于开发:https://:3000/api/auth/callback/google
⚠️
Google 仅在用户首次登录时向应用程序提供刷新令牌。
要强制 Google 重新颁发刷新令牌,用户需要从其帐户中删除应用程序,然后重新登录:https://myaccount.google.com/permissions
或者,您也可以在 params 对象中传递选项 authorization,这将强制在登录时始终提供刷新令牌,但是这将要求所有用户在每次登录时确认他们是否希望授予您的应用程序访问权限。
如果您需要访问 Google 帐户的刷新令牌或访问令牌,并且您没有使用数据库来持久保存用户帐户,那么这可能是您需要执行的操作。
const options = {
  providers: [
    Google({
      clientId: process.env.GOOGLE_ID,
      clientSecret: process.env.GOOGLE_SECRET,
      authorization: {
        params: {
          prompt: "consent",
          access_type: "offline",
          response_type: "code"
        }
      }
    })
  ],
}💡
Google 还将在 OAuth 配置文件中返回一个 email_verified 布尔属性。
您可以使用此属性来限制对特定域中具有已验证帐户的人员的访问。
const options = {
  ...
  callbacks: {
    async signIn({ account, profile }) {
      if (account.provider === "google") {
        return profile.email_verified && profile.email.endsWith("@example.com")
      }
      return true // Do different verification for other providers that don't have `email_verified`
    },
  }
  ...
}💡
Google 提供者附带一个 默认配置。要覆盖您的用例的默认值,请查看 自定义内置 OAuth 提供者。
类型参数
| 类型参数 | 
|---|
| PextendsGoogleProfile | 
参数
| 参数 | 类型 | 
|---|---|
| options | OAuthUserConfig<P> | 
返回值
OAuthConfig<P>