feat: encapsulate identity with better-auth
This commit is contained in:
46
modules/identity/models/principal.ts
Normal file
46
modules/identity/models/principal.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { makeDocumentParser } from "@platform/database/utilities.ts";
|
||||
import z from "zod";
|
||||
|
||||
export enum PrincipalTypeId {
|
||||
User = 1,
|
||||
Group = 2,
|
||||
Other = 99,
|
||||
}
|
||||
|
||||
export const PRINCIPAL_TYPE_NAMES = {
|
||||
[PrincipalTypeId.User]: "User",
|
||||
[PrincipalTypeId.Group]: "Group",
|
||||
[PrincipalTypeId.Other]: "Other",
|
||||
};
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------------
|
||||
| Schema
|
||||
|--------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
export const PrincipalSchema = z.object({
|
||||
id: z.string(),
|
||||
type: z.strictObject({
|
||||
id: z.enum(PrincipalTypeId),
|
||||
name: z.string(),
|
||||
}),
|
||||
roles: z.array(z.string()),
|
||||
attr: z.record(z.string(), z.any()),
|
||||
});
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------------
|
||||
| Parsers
|
||||
|--------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
export const parsePrincipal = makeDocumentParser(PrincipalSchema);
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------------
|
||||
| Types
|
||||
|--------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
export type Principal = z.infer<typeof PrincipalSchema>;
|
||||
26
modules/identity/models/session.ts
Normal file
26
modules/identity/models/session.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import z from "zod";
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------------
|
||||
| Schema
|
||||
|--------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
export const SessionSchema = z.object({
|
||||
id: z.string(),
|
||||
userId: z.string(),
|
||||
token: z.string(),
|
||||
ipAddress: z.string().nullable().optional(),
|
||||
userAgent: z.string().nullable().optional(),
|
||||
createdAt: z.coerce.date(),
|
||||
updatedAt: z.coerce.date(),
|
||||
expiresAt: z.coerce.date(),
|
||||
});
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------------
|
||||
| Types
|
||||
|--------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
export type Session = z.infer<typeof SessionSchema>;
|
||||
Reference in New Issue
Block a user