我希望用户在首次访问该网站时自动匿名登录,然后在任何时候都应该能够使用外部提供商(例如Google或LinkedIn)登录,并继续使用以前的数据。存储在匿名帐户下。
let currentUser = auth.currentUser;
if (!currentUser) {
currentUser = (await auth.signInAnonymously()).user;
}
我已经实现了OAuth 2.0流程,以使用户能够使用包括LinkedIn在内的各种提供商来登录我的系统。我的后端会生成一个JWT访问令牌,该令牌将提供给Web客户端。
const accessToken = {
sub: my-service-account@my-firebase-project.iam.gserviceaccount.com,
iss: my-service-account@my-firebase-project.iam.gserviceaccount.com,
aud: "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit",
uid: 1234
// other claims...
}
const newUser = await auth.signInWithCustomToken(accessToken);
到目前为止一切都很好-但是如何将两者联系起来?根据{{3}},我应该可以执行以下操作:
if (currentUser && currentUser.isAnonymous) {
const provider = new firebase.auth.OAuthProvider('myprovider');
const credential = provider.credential(token, token);
const linkedUser = await currentUser.linkWithCredential(credential);
}
但是,firebase将请求发送到account linking documentation
{
idToken: {
"provider_id": "anonymous",
"iss": "https://securetoken.google.com/my-firebase-project",
"aud": "my-firebase-project",
"user_id": anonymousUserId,
"sub": anonymousUserId,
"firebase": {
"identities": {},
"sign_in_provider": "anonymous"
}
}
postBody: {
id_token: token,
access_token: token
providerId: "myprovider"
}
requestUri: "http://localhost"
returnIdpCredential: true
returnSecureToken: true
}
我收到400错误响应:
INVALID_CREDENTIAL_OR_PROVIDER_ID : Invalid IdP response/credential: http://localhost?id_token=${token}&access_token=${token}&provider=myprovider
我的前端是 https ,运行在端口3000 上,后端是/api/oauth/
,也是https
requestUri
?