mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: support oauth bind/unbind account (#971)
Co-authored-by: panda <panda_dtdyy@outlook.com>
This commit is contained in:
1 parent
1a8232899c
commit
536c697f4b
16 files changed
+393
-141
No files matched your search
@@ -1,23 +1,57 @@
|
||||
import db from '../service/db';
|
||||
import { Collection } from 'mongodb';
|
||||
import { Context, Service } from '../context';
|
||||
|
||||
const coll = db.collection('oauth');
|
||||
export interface OauthMap {
|
||||
platform: string;
|
||||
/** source openId */
|
||||
id: string;
|
||||
/** target uid */
|
||||
uid: number;
|
||||
}
|
||||
|
||||
class OauthModel {
|
||||
static async get(_id: string) {
|
||||
const doc = await coll.findOne({ _id });
|
||||
declare module '../context' {
|
||||
interface Context {
|
||||
oauth: OauthModel;
|
||||
}
|
||||
}
|
||||
|
||||
export default class OauthModel extends Service {
|
||||
static inject = ['db'];
|
||||
coll: Collection<OauthMap>;
|
||||
|
||||
constructor(ctx: Context) {
|
||||
super(ctx, 'oauth');
|
||||
this.coll = ctx.db.collection('oauth');
|
||||
}
|
||||
|
||||
async [Context.init]() {
|
||||
await this.ctx.db.ensureIndexes(this.coll,
|
||||
// { key: { platform: 1, id: 1 }, name: 'platform_id', unique: true },
|
||||
{ key: { uid: 1, platform: 1 }, name: 'uid_platform' },
|
||||
);
|
||||
console.log('oauth init');
|
||||
}
|
||||
|
||||
async get(platform: string, id: string) {
|
||||
const doc = await this.coll.findOne({ platform, id });
|
||||
if (doc) return doc.uid;
|
||||
return null;
|
||||
}
|
||||
|
||||
static async set(_id: string, uid: number) {
|
||||
const res = await coll.findOneAndUpdate(
|
||||
{ _id },
|
||||
async set(platform: string, id: string, uid: number) {
|
||||
const res = await this.coll.findOneAndUpdate(
|
||||
{ platform, id },
|
||||
{ $set: { uid } },
|
||||
{ upsert: true, returnDocument: 'after' },
|
||||
);
|
||||
return res?.uid;
|
||||
}
|
||||
}
|
||||
|
||||
export default OauthModel;
|
||||
global.Hydro.model.oauth = OauthModel;
|
||||
async unbind(platform: string, id: string, uid: number) {
|
||||
await this.coll.deleteOne({ platform, id, uid });
|
||||
}
|
||||
|
||||
async list(uid: number) {
|
||||
return this.coll.find({ uid }).toArray();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user