Fix model creation for new wms-core version

This commit is contained in:
Alice Gaudon 2020-09-01 11:21:54 +02:00
parent 5c0b2e6040
commit f873a24791
5 changed files with 9 additions and 16 deletions

View File

@ -101,8 +101,6 @@ export default class App extends Application {
// Session // Session
this.use(redisComponent); this.use(redisComponent);
this.use(new SessionComponent(redisComponent)); this.use(new SessionComponent(redisComponent));
// Auth
this.use(new AuthComponent(new class extends AuthGuard<MagicLink | AuthToken> { this.use(new AuthComponent(new class extends AuthGuard<MagicLink | AuthToken> {
public async getProofForSession(session: Express.Session): Promise<any | null> { public async getProofForSession(session: Express.Session): Promise<any | null> {
return await MagicLink.bySessionID(session.id, [MagicLinkActionType.LOGIN, MagicLinkActionType.REGISTER]); return await MagicLink.bySessionID(session.id, [MagicLinkActionType.LOGIN, MagicLinkActionType.REGISTER]);

View File

@ -11,7 +11,7 @@ export default class AuthTokenController extends Controller {
} }
protected async postGenAuthToken(req: Request, res: Response): Promise<void> { protected async postGenAuthToken(req: Request, res: Response): Promise<void> {
const authToken = new AuthToken({ const authToken = AuthToken.create({
user_id: req.models.user!.id, user_id: req.models.user!.id,
ttl: req.body.ttl ? parseInt(req.body.ttl) : 365 * 24 * 3600, ttl: req.body.ttl ? parseInt(req.body.ttl) : 365 * 24 * 3600,
}); });

View File

@ -42,7 +42,7 @@ export default class URLRedirectController extends Controller {
if (req.body.type !== 'url') return next(); if (req.body.type !== 'url') return next();
slug = slug || req.params.slug || req.body.slug || await generateSlug(10); slug = slug || req.params.slug || req.body.slug || await generateSlug(10);
const urlRedirect = new URLRedirect({ const urlRedirect = URLRedirect.create({
user_id: req.models.user!.id, user_id: req.models.user!.id,
slug: slug, slug: slug,
target_url: req.body.target_url, target_url: req.body.target_url,

View File

@ -11,20 +11,19 @@ export default class AuthToken extends Model implements AuthProof<User> {
protected used_at?: Date = undefined; protected used_at?: Date = undefined;
protected readonly ttl?: number = undefined; protected readonly ttl?: number = undefined;
constructor(props: any) {
super(props);
if (!this.secret) {
this.secret = cryptoRandomDictionary(64, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_');
}
}
protected init() { protected init() {
this.setValidation('user_id').defined().exists(User, 'id'); this.setValidation('user_id').defined().exists(User, 'id');
this.setValidation('secret').defined().between(32, 64); this.setValidation('secret').defined().between(32, 64);
this.setValidation('ttl').defined().min(1).max(5 * 365 * 24 * 3600 /* 5 years */); this.setValidation('ttl').defined().min(1).max(5 * 365 * 24 * 3600 /* 5 years */);
} }
protected async autoFill(): Promise<void> {
await super.autoFill();
// @ts-ignore
if (!this.secret) this['secret'] = cryptoRandomDictionary(64, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_');
}
public use() { public use() {
this.used_at = new Date(); this.used_at = new Date();
} }

View File

@ -24,10 +24,6 @@ export default class URLRedirect extends Model {
public readonly target_url?: string = undefined; public readonly target_url?: string = undefined;
public created_at?: Date = undefined; public created_at?: Date = undefined;
constructor(data: any) {
super(data);
}
protected init(): void { protected init(): void {
this.setValidation('user_id').defined().exists(User, 'id'); this.setValidation('user_id').defined().exists(User, 'id');
this.setValidation('slug').defined().minLength(1).maxLength(259).unique(URLRedirect, 'slug').unique(FileModel, 'slug'); this.setValidation('slug').defined().minLength(1).maxLength(259).unique(URLRedirect, 'slug').unique(FileModel, 'slug');