简体   繁体   English

Firebase,Auth0,React。 自定义令牌格式不正确。 请检查文件

[英]Firebase, Auth0, React. The custom token format is incorrect. Please check the documentation

I'm trying to use Auth0 JWT Tokens with Firebase, with no much luck. 我正在尝试将Auth0 JWT令牌与Firebase结合使用,但运气不好。

When using the token with Firebase: 在Firebase中使用令牌时:

const token = localStorage.getItem('id_token'); //from auth0
firebase.auth().signInWithCustomToken(token).catch((error) => {
  var errorCode = error.code;
  var errorMessage = error.message;

  console.log(error);
  console.log(token);
});

All I get is: 我得到的是:

“The custom token format is incorrect. Please check the documentation.”

As far as I saw in Firebase's documentation Auth0 and Firebase tokens are different: https://firebase.google.com/docs/auth/admin/create-custom-tokens 据我在Firebase的文档中所看到的,Auth0和Firebase令牌是不同的: https ://firebase.google.com/docs/auth/admin/create-custom-tokens

Apparently, Firebase expects an uid which is not present in the one generated by Auth0 which uid equivalent is in sub . 显然, Firebase期望一个uid不在Auth0生成的uid中,而uid等效于sub

I tried to create a rule to modify the Auth0's token to include a copy of sub named uid to see if this could be a solution, but it's not working, nothing is added to the body of the token. 我试图创建一条规则来修改Auth0的令牌,以包括一个名为uid的子副本,以查看这是否可以解决,但它不起作用,没有将任何内容添加到令牌的主体中。

function (user, context, callback) {
context.idToken.uid = user.user_id;
callback(null, user, context);
}

Any idea / suggestion? 有什么想法/建议吗?

PS: PS:

1.I checked the token in jwt.io and its valid. 1.我检查了jwt.io中的令牌及其有效。 2.I tried reducing the expiring time to less than 5min, as I saw some people considering this a possible solution, but its not. 2.我尝试将过期时间减少到5分钟以内,因为我看到有人认为这是一种可能的解决方案,但事实并非如此。

You can't use an Auth0 token directly with Firebase. 您不能直接在Firebase中使用Auth0令牌。 You need to create a server-side API that uses the firebase-admin SDK to create a Firebase Custom Token using the Auth0 data. 您需要创建使用firebase-admin SDK的服务器端API,以使用Auth0数据创建Firebase自定义令牌。

There's a full tutorial over on the OAuth site. OAuth网站上有完整的教程 Check out the API Routes section on how to use firebaseAdmin.auth().createCustomToken given the OAuth token: 在给定OAuth令牌的情况下,查看关于如何使用firebaseAdmin.auth().createCustomToken的“ API路由”部分:

// Auth0 athentication middleware
  const jwtCheck = jwt({
    secret: jwks.expressJwtSecret({
      cache: true,
      rateLimit: true,
      jwksRequestsPerMinute: 5,
      jwksUri: `https://${config.AUTH0_DOMAIN}/.well-known/jwks.json`
    }),
    audience: config.AUTH0_API_AUDIENCE,
    issuer: `https://${config.AUTH0_DOMAIN}/`,
    algorithm: 'RS256'
  });

  // Initialize Firebase Admin with service account
  const serviceAccount = require(config.FIREBASE_KEY);
  firebaseAdmin.initializeApp({
    credential: firebaseAdmin.credential.cert(serviceAccount),
    databaseURL: config.FIREBASE_DB
  });

  // GET object containing Firebase custom token
  app.get('/auth/firebase', jwtCheck, (req, res) => {
    // Create UID from authenticated Auth0 user
    const uid = req.user.sub;
    // Mint token using Firebase Admin SDK
    firebaseAdmin.auth().createCustomToken(uid)
      .then(customToken => 
        // Response must be an object or Firebase errors
        res.json({firebaseToken: customToken})
      )
      .catch(err => 
        res.status(500).send({
          message: 'Something went wrong acquiring a Firebase token.',
          error: err
        })
      );
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM