Javascript Firebase Google登录令牌过期问题
我想让我的用户通过他们的Gmail帐户在我的应用程序中发送电子邮件。 因此,在我的前端,我正在收集使用Javascript Firebase Google登录令牌过期问题,javascript,firebase,firebase-authentication,gmail-api,google-signin,Javascript,Firebase,Firebase Authentication,Gmail Api,Google Signin,我想让我的用户通过他们的Gmail帐户在我的应用程序中发送电子邮件。 因此,在我的前端,我正在收集使用 const provider = new firebase.auth.GoogleAuthProvider() provider.addScope('https://www.googleapis.com/auth/gmail.send') provider.setCustomParameters({ access_type: 'offline', prompt: 'consen
const provider = new firebase.auth.GoogleAuthProvider()
provider.addScope('https://www.googleapis.com/auth/gmail.send')
provider.setCustomParameters({
access_type: 'offline',
prompt: 'consent',
})
firebase.auth()
.signInWithPopup(provider)
.then((result) => {
var credential = result.credential;
var token = credential.accessToken;
})
在我的后端,由于谷歌API,我使用这个令牌代表他们发送电子邮件。一切正常,但代币只能维持一个小时
你对正确的处理方法有什么建议吗?我需要延长令牌的持续时间吗?每次我想发送电子邮件时是否必须创建一个新令牌?或者我不必使用firebase收集令牌吗?默认情况下,firebase auth返回一个短期身份验证令牌和一个刷新令牌,您可以使用它无限期地扩展该会话。为了延长该会话,您需要 以下是其工作原理的简要总结:
.signInWithPopup()登录
.verifyIdToken()
来验证该令牌,然后.createSessionCookie()
使用您想要的任何过期时间
set cookie
HTTP参数进行响应
login(req: any, res: Response) {
// Cookie has a 30 day expiration
const AUTH_COOKIE_LENGTH = 30;
// If no ID Token was passed, return an error
if (!req.body.idToken) {
return res.status(400).end();
}
// The ID Token passed as a POST parameter
const idToken = req.body.idToken.toString().trim();
// Verify the ID Token
admin.auth().verifyIdToken(idToken)
.then(async (user) => {
// Cookie expires
const expiresIn = 60 * 60 * 24 * AUTH_COOKIE_LENGTH * 1000;
// Generate the session cookie
admin.auth().createSessionCookie(idToken, {expiresIn})
.then((sessionCookie) => {
// Add the set-cookie parameter to the response
res.cookie("__session", sessionCookie, {
domain: '.example.com',
secure: true,
sameSite: 'strict',
expires: expiresIn
});
res.json({
success: true
}).end();
}, (error: any) => {
res.status(503).json({success: false}).end();
});
}).catch((error: any) => {
res.status(401).json({success: false, error: "INVALID_TOKEN"}).end();
});
}