Auth0 SPA + API。如何获取 API 上的电子邮件、电话和其他信息?

问题描述 投票:0回答:1

在我的 React 应用程序中,我使用这样的 Auth0 Provider:

<Auth0Provider
  domain="xxx.us.auth0.com"
  clientId="xxxxxx"
  authorizationParams={{
    redirect_uri: window.location.origin,
    audience: 'api.xxxx',
    scope: 'openid profile email',
  }}
>
  <App />
</Auth0Provider>

在我的nodeJS后端,我已经像这样设置了verifyAuth中间件-

import { auth } from 'express-oauth2-jwt-bearer';

const verifyAuth = auth({
  audience: 'api.xxxx',
  issuerBaseURL: 'https://xxxx.us.auth0.com/',
  tokenSigningAlg: 'RS256',
});

export default verifyAuth;

如何在我的nodeJS后端获取用户的电子邮件和电话号码?

我想设置一个

findOrCreate
功能,当我在运行中间件后检查
req.auth
时,我看到了这个:

{
  payload: {
    iss: 'https://xxxx.us.auth0.com/',
    sub: 'auth0|xxxxxx',
    aud: [ 'xxxx.xxxx', 'https://xxxx.us.auth0.com/userinfo' ],
    iat: 1695839707,
    exp: 1695926107,
    azp: 'xxxxxxxxx',
    scope: 'openid profile email'
  },
  header: { alg: 'RS256', typ: 'JWT', kid: 'xxxxxx' },
  token: 'eyJhbGciO..............xxxxx'
}
node.js jwt auth0
1个回答
0
投票

默认情况下,Auth0 中的 JWT 将仅包含 JWT 规范中的基本字段。如果您需要更多信息,可以将其添加到登录时运行的 Auth0 操作中。

Actions -> Library
下添加一个操作来扩展您的 JWT。 在
Actions -> Flows -> Login
下添加操作。

操作可能看起来像这样:

exports.onExecutePostLogin = async (event, api) => {
    api.accessToken.setCustomClaim("email", event.user.email)
    api.accessToken.setCustomClaim("emailIsVerified", event.user.email_verified)
    console.log(`User ${event.user.user_id} extended jwt with ${event.user.email}`)
  };

请注意,控制台日志存储为日志条目,但我个人发现它有点难以找到。您可以在

Monitoring -> Logs -> "Success Login" -> Details -> Action Details

下找到它
© www.soinside.com 2019 - 2024. All rights reserved.