Per*_*ere 5 oauth cross-domain firebase google-cloud-functions
我使用此示例开发了 oAuth 登录。遇到的第一个问题是如果浏览器中禁用了第三方 cookie(现在默认情况下),则状态 cookie 验证。正如这个答案所建议的,我代理了这些功能。
因此,我使用托管重写来代理这些功能,因此您位于同一域中,并且第一个重定向功能设置的服务器 cookie 似乎与应用程序位于同一域中。所以这就是发生的事情
当我尝试从 token 函数读取 cookie 时
[Object: null prototype] {}
Run Code Online (Sandbox Code Playgroud)
这是托管重写
"hosting": {
...
"rewrites": [
{
"source": "/redirect",
"function": "redirect"
},
{
"source": "/token**",
"function": "token"
},
{
"source": "**",
"destination": "/index.html"
}
],
Run Code Online (Sandbox Code Playgroud)
这是重定向功能
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)
})
})
Run Code Online (Sandbox Code Playgroud)
这是令牌函数
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 })
}
})
Run Code Online (Sandbox Code Playgroud)
为什么cookie没有通过第二个云函数传递?如果禁用重写并启用第三方 cookie,则代码可以正常工作。
| 归档时间: |
|
| 查看次数: |
312 次 |
| 最近记录: |