我使用开发了一个oAuth登录这个例子 https://github.com/firebase/functions-samples/tree/master/linkedin-auth。遇到的第一个问题是如果浏览器中禁用第三方 cookie(现在默认情况下),则状态 cookie 验证。正如建议的这个答案 https://github.com/firebase/functions-samples/tree/master/linkedin-auth,我代理了这些功能。
因此,我使用托管重写来代理这些功能,因此您位于同一域中,并且第一个重定向功能设置的服务器 cookie 似乎与应用程序位于同一域中。所以这就是发生的事情
- 用户被重定向到云功能,该功能设置 cookie 并将用户重定向到第三方身份验证提供商
- 用户登录
- 用户再次重定向到应用程序,应用程序获取授权码并将用户重定向到令牌函数
- token函数尝试读取状态cookie,但根本没有cookie
当我尝试从 token 函数读取 cookie 时
[Object: null prototype] {}
这是托管重写
"hosting": {
...
"rewrites": [
{
"source": "/redirect",
"function": "redirect"
},
{
"source": "/token**",
"function": "token"
},
{
"source": "**",
"destination": "/index.html"
}
],
这是重定向功能
exports.redirect = functions.https.onRequest((req, res) => {
cookieParser()(req, res, () => {
const redirect_uri = `https://${process.env.GCLOUD_PROJECT}.firebaseapp.com/auth.html`
const state = req.cookies.state || crypto.randomBytes(20).toString('hex')
const authorizationUri = fedidClient().authorizationCode.authorizeURL({
redirect_uri: redirect_uri,
scope: OAUTH_SCOPES,
state: state,
})
res.cookie('state', state.toString(), {
maxAge: 3600000,
secure: true,
httpOnly: true,
})
res.redirect(authorizationUri)
})
})
这是令牌函数
exports.token = functions.https.onRequest((req, res) => {
const redirect_uri = `https://${process.env.GCLOUD_PROJECT}.firebaseapp.com/auth.html`
try {
return cookieParser()(req, res, async () => {
if (!req.cookies.state) {
throw new Error(
'State cookie not set or expired. Maybe you took too long to authorize. Please try again.'
)
}
const tokenConfig = {
code: req.query.code,
redirect_uri: redirect_uri,
scope: OAUTH_SCOPES,
}
const result = await fedidClient().authorizationCode.getToken(tokenConfig)
const accessToken = fedidClient().accessToken.create(result)
let user = {}
await getUserInfo(accessToken)
.then((result) => result.json())
.then((json) => (user = json))
// Create a Firebase account and get the Custom Auth Token.
const firebaseToken = await createFirebaseAccount(
user.uid,
user.displayName,
user.mail,
accessToken.token.access_token
)
res.jsonp({
token: firebaseToken,
})
})
} catch (error) {
return res.status(500).jsonp({ error: error.toString })
}
})
为什么cookie没有通过第二个云函数传递?如果禁用重写并启用第三方 cookie,则代码可以正常工作。