我试图以最安全的方式处理使用Firebase创建customToken的最佳方法 .
这就是我想出的:
-
用户使用电子邮件和密码登录客户端 .
firebase.auth().signInWithEmailAndPassword(email, password)
-
将用户的当前UID存储在本地存储中 .
localStorage.setItem('uid', response.uid);
-
获取当前用户的JWT令牌 .
firebase.auth().currentUser.getToken(true)
并将令牌存储在localStorage中localStorage.setItem('token', res)
-
对服务器进行调用后调用并将令牌添加到Authorization标头并在正文中发送UID .
const headers = new Headers(); headers.append('Content-Type', 'application/json'); headers.append('Authorization', localStorage.getItem('token')); this.http.post('/api/login', localStorage.getItem('uid'), { headers: headers })
-
在服务器端验证令牌
const authorization = req.headers.authorization; admin.auth().verifyIdToken(authorization)
. 如果有效设置UIDthis.uid = decodedToken.uid;
-
现在生成自定义令牌 . 添加additionalClaims
const additionalClaims = { premiumAccount: true };
并调用createCustomToken函数 .admin.auth().createCustomToken(this.uid, additionalClaims)
-
将自定义令牌发送回客户端
res.status(200).send({ token: customToken })
-
在客户端使用customToken登录 .
firebase.auth().signInWithCustomToken(data.token)
这个摘要是一个好的做法还是有更好的方法来处理这个?