feat: add snippet and test for supa db

This commit is contained in:
2024-10-21 01:31:58 +08:00
parent 089f629096
commit 0eae9458c6
8 changed files with 177 additions and 40 deletions

View File

@@ -26,15 +26,29 @@ export class User extends Model {
declare orgId: string;
declare email: string;
declare avatar: string;
declare supaId: string;
tokenUser: any;
setTokenUser(tokenUser: any) {
this.tokenUser = tokenUser;
}
/**
* uid 是用于 orgId 的用户id
* @param uid
* @returns
*/
async createToken(uid?: string) {
const { id, username, type } = this;
const { id, username, type, supaId } = this;
const expireTime = 60 * 60 * 24 * 7; // 7 days
const now = new Date().getTime();
const token = await createToken({ id, username, uid, type }, config.tokenSecret);
let supa = {};
if (supaId) {
supa = {
aud: 'authenticated',
role: 'authenticated',
sub: supaId,
};
}
const token = await createToken({ id, username, uid, type, ...supa }, config.tokenSecret);
return { token, expireTime: now + expireTime };
}
static async verifyToken(token: string) {
@@ -168,6 +182,9 @@ User.init(
orgId: {
type: DataTypes.UUID,
},
supaId: {
type: DataTypes.UUID,
},
needChangePassword: {
type: DataTypes.BOOLEAN,
defaultValue: false,