Update Registration to new auth method

This commit is contained in:
William Oldham
2023-11-03 17:43:34 +00:00
parent 88bf4eb31b
commit c4f6e7a87f
8 changed files with 160 additions and 21 deletions

View File

@@ -0,0 +1,43 @@
import { Entity, PrimaryKey, Property } from '@mikro-orm/core';
import { randomUUID } from 'crypto';
// 30 seconds
const CHALLENGE_EXPIRY_MS = 3000000 * 1000;
@Entity({ tableName: 'challenge_codes' })
export class ChallengeCode {
@PrimaryKey({ name: 'code', type: 'uuid' })
code: string = randomUUID();
@Property({ name: 'stage', type: 'text' })
stage!: 'registration' | 'login';
@Property({ name: 'auth_type' })
authType!: 'mnemonic';
@Property({ type: 'date' })
createdAt: Date = new Date();
@Property({ type: 'date' })
expiresAt: Date = new Date(Date.now() + CHALLENGE_EXPIRY_MS);
}
export interface ChallengeCodeDTO {
code: string;
stage: string;
authType: string;
createdAt: string;
expiresAt: string;
}
export function formatChallengeCode(
challenge: ChallengeCode,
): ChallengeCodeDTO {
return {
code: challenge.code,
stage: challenge.stage,
authType: challenge.authType,
createdAt: challenge.createdAt.toISOString(),
expiresAt: challenge.expiresAt.toISOString(),
};
}

View File

@@ -6,7 +6,7 @@ export class Session {
@PrimaryKey({ name: 'id', type: 'uuid' })
id: string = randomUUID();
@Property({ name: 'user', type: 'uuid' })
@Property({ name: 'user', type: 'text' })
user!: string;
@Property({ type: 'date' })

View File

@@ -1,5 +1,5 @@
import { Entity, PrimaryKey, Property, types } from '@mikro-orm/core';
import { randomUUID } from 'crypto';
import { Entity, Index, PrimaryKey, Property, types } from '@mikro-orm/core';
import { nanoid } from 'nanoid';
export type UserProfile = {
colorA: string;
@@ -9,8 +9,12 @@ export type UserProfile = {
@Entity({ tableName: 'users' })
export class User {
@PrimaryKey({ name: 'id', type: 'uuid' })
id: string = randomUUID();
@PrimaryKey({ name: 'id', type: 'text' })
id: string = nanoid(12);
@Property({ name: 'public_key', type: 'text' })
@Index()
publicKey!: string;
@Property({ name: 'namespace' })
namespace!: string;
@@ -18,9 +22,6 @@ export class User {
@Property({ type: 'date' })
createdAt: Date = new Date();
@Property({ type: 'text' })
name!: string;
@Property({ name: 'permissions', type: types.array })
roles: string[] = [];
@@ -34,7 +35,7 @@ export class User {
export interface UserDTO {
id: string;
namespace: string;
name: string;
publicKey: string;
roles: string[];
createdAt: string;
profile: {
@@ -48,7 +49,7 @@ export function formatUser(user: User): UserDTO {
return {
id: user.id,
namespace: user.namespace,
name: user.name,
publicKey: user.publicKey,
roles: user.roles,
createdAt: user.createdAt.toISOString(),
profile: {