mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: oauth: allow multiple ids (#1142)
This commit is contained in:
1 parent
232c066d22
commit
4f8c9e7b36
2 files changed
+9
-8
No files matched your search
@@ -467,26 +467,27 @@ class OauthCallbackHandler extends Handler {
|
|||||||
if (!provider) throw new UserFacingError('Oauth type');
|
if (!provider) throw new UserFacingError('Oauth type');
|
||||||
await this.limitRate('oauth_callback', 60, 5);
|
await this.limitRate('oauth_callback', 60, 5);
|
||||||
const r = await provider.callback.call(this, args);
|
const r = await provider.callback.call(this, args);
|
||||||
|
const ids = Array.isArray(r._id) ? r._id : [r._id];
|
||||||
|
const existing = await Promise.all(ids.map((id) => this.ctx.oauth.get(args.type, id)));
|
||||||
if (this.session.oauthBind === args.type) {
|
if (this.session.oauthBind === args.type) {
|
||||||
delete this.session.oauthBind;
|
delete this.session.oauthBind;
|
||||||
const existing = await this.ctx.oauth.get(args.type, r._id);
|
if (existing.some((id) => id && id !== this.user._id)) {
|
||||||
if (existing && existing !== this.user._id) {
|
|
||||||
throw new BadRequestError('Already binded to another account');
|
throw new BadRequestError('Already binded to another account');
|
||||||
}
|
}
|
||||||
this.response.redirect = '/home/security';
|
this.response.redirect = '/home/security';
|
||||||
if (existing !== this.user._id) await this.ctx.oauth.set(args.type, r._id, this.user._id);
|
await Promise.all(ids.map((i) => this.ctx.oauth.set(args.type, i, this.user._id)));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const effective = existing.find((i) => i);
|
||||||
|
|
||||||
const uid = await this.ctx.oauth.get(args.type, r._id);
|
if (effective) {
|
||||||
if (uid) {
|
await successfulAuth.call(this, await user.getById('system', effective));
|
||||||
await successfulAuth.call(this, await user.getById('system', uid));
|
|
||||||
this.response.redirect = '/';
|
this.response.redirect = '/';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const udoc = await user.getByEmail('system', r.email);
|
const udoc = await user.getByEmail('system', r.email);
|
||||||
if (udoc) {
|
if (udoc) {
|
||||||
await this.ctx.oauth.set(args.type, r._id, udoc._id);
|
await Promise.all(ids.map((i) => this.ctx.oauth.set(args.type, i, udoc._id)));
|
||||||
await successfulAuth.call(this, udoc);
|
await successfulAuth.call(this, udoc);
|
||||||
this.response.redirect = '/';
|
this.response.redirect = '/';
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ declare module '../context' {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface OAuthUserResponse {
|
export interface OAuthUserResponse {
|
||||||
_id: string;
|
_id: string | string[];
|
||||||
email: string;
|
email: string;
|
||||||
avatar?: string;
|
avatar?: string;
|
||||||
bio?: string;
|
bio?: string;
|
||||||
|
|||||||
Reference in new issue
Block a user