我有一个Lambda函数,该函数将Auth0授权的id_token传递给验证JWT的函数,然后从firebase返回新铸造的令牌。
这是Lambda函数:
const express = require('express');
const serverless = require('serverless-http');
const cors = require('cors');
const jwt = require('express-jwt');
const jwks = require('jwks-rsa');
const firebaseAdmin = require('firebase-admin');
const app = express();
app.use(cors());
const jwtCheck = jwt({
secret: jwks.expressJwtSecret({
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 5,
jwksUri: 'https://skillthrive.auth0.com/.well-known/jwks.json'
}),
audience: '3U-nSf4D_ofItgNsObXGdFY92aFqBYih',
issuer: 'https://skillthrive.auth0.com/',
algorithm: 'RS256'
});
const serviceAccount = require('../firebase/firebase-keys.json');
firebaseAdmin.initializeApp({
credential: firebaseAdmin.credential.cert(serviceAccount),
databaseURL: `https://${serviceAccount.project_id}.firebaseio.com`
});
// GET object containing Firebase custom token
app.get('/firebase', jwtCheck, async (req, res) => {
const {sub: uid} = req.user;
try {
const firebaseToken = await firebaseAdmin.auth().createCustomToken(uid);
res.json({firebaseToken});
} catch (err) {
res.status(500).send({
message: 'Something went wrong acquiring a Firebase token.',
error: err
});
}
});
module.exports.handler = serverless(app);
在客户端,我通过以下方法提出请求:
doSignInWithCustomToken() {
if (typeof window !== 'undefined') {
fetch('/.netlify/functions/firebase', {
headers: {
'Authorization': `Bearer ${window.localStorage.getItem('id_token')}`,
},
}).then(response => console.log(response))
/*.then(response => response.json())
.then(data => this.auth.signInWithCustomToken(data.firebaseToken))*/
.catch(error => console.log(error));
}
}
在本地主机上,我收到200条响应,然后可以将响应传递给signInWithCustomToken()(firebase提供的函数)。
但是,一旦我推入Netlify并进行测试,我的Netlify功能日志中就会出现502错误和error decoding lambda response: json: cannot unmarshal number into Go value of type string
。
有人知道什么地方可能出问题了吗?