core: oauth: enhancement

This commit is contained in:
undefined committed 2022-02-09 18:44:41 +08:00
1 parent 04ec44dab9
commit b1f90ab5a1
10 files changed
+55 -26

No files matched your search

+17 -5
View File
@@ -6,7 +6,7 @@ import {
SystemError, UserAlreadyExistError, UserFacingError,
UserNotFoundError, ValidationError, VerifyPasswordError,
} from '../error';
import { User } from '../interface';
import { OAuthUserResponse, Udoc, User } from '../interface';
import avatar from '../lib/avatar';
import { sendMail } from '../lib/mail';
import { isEmail, isPassword, isUname } from '../lib/validator';
@@ -360,14 +360,23 @@ class OauthCallbackHandler extends Handler {
noCheckPermView = true;
async get(args: any) {
let r;
if (global.Hydro.lib[`oauth_${args.type}`]) r = await global.Hydro.lib[`oauth_${args.type}`].callback.call(this, args);
else throw new UserFacingError('Oauth type');
if (!global.Hydro.lib[`oauth_${args.type}`]) throw new UserFacingError('Oauth type');
const r = await global.Hydro.lib[`oauth_${args.type}`].callback.call(this, args) as OAuthUserResponse;
const uid = await oauth.get(r._id);
if (uid) {
await user.setById(uid, { loginat: new Date(), loginip: this.request.ip });
this.session.uid = uid;
this.session.scope = PERM.PERM_ALL.toString();
} else {
if (r.email) {
const udoc = await user.getByEmail('system', r.email);
if (udoc) {
await user.setById(udoc._id, { loginat: new Date(), loginip: this.request.ip });
this.session.uid = udoc._id;
this.session.scpoe = PERM.PERM_ALL.toString();
return;
}
}
this.checkPriv(PRIV.PRIV_REGISTER_USER);
let username = '';
r.uname = r.uname || [];
@@ -384,11 +393,14 @@ class OauthCallbackHandler extends Handler {
r.email, username, String.random(32),
undefined, this.request.ip,
);
const $set: any = {
const $set: Partial<Udoc> = {
oauth: args.type,
loginat: new Date(),
loginip: this.request.ip,
};
if (r.bio) $set.bio = r.bio;
if (r.viewLang) $set.viewLang = r.viewLang;
if (r.avatar) $set.avatar = r.avatar;
await Promise.all([
user.setById(_id, $set),
oauth.set(r.email, _id),
+9
View File
@@ -55,6 +55,15 @@ export interface Setting {
flag: number,
}
export interface OAuthUserResponse {
_id: string;
avatar?: string;
email?: string;
bio?: string;
uname?: string[];
viewLang?: string;
}
export interface Udoc extends Dictionary<any> {
_id: number;
mail: string;