alepha
Version:
Easy-to-use modern TypeScript framework for building many kind of applications.
148 lines (147 loc) • 4.7 kB
JavaScript
import { $module, z } from "alepha";
import { $entity, db, sql } from "alepha/orm";
//#region ../../src/api/users/entities/users.ts
const DEFAULT_USER_REALM_NAME = "default";
const users = $entity({
name: "users",
schema: z.object({
id: db.primaryKey(z.uuid()),
version: db.version(),
createdAt: db.createdAt(),
updatedAt: db.updatedAt(),
realm: db.default(z.text(), DEFAULT_USER_REALM_NAME),
username: z.shortText({
minLength: 3,
maxLength: 30
}).optional(),
email: z.string().meta({ format: "email" }).optional(),
phoneNumber: z.e164().optional(),
roles: db.default(z.array(z.string()), []),
firstName: z.string().optional(),
lastName: z.string().optional(),
picture: z.string().optional(),
enabled: db.default(z.boolean(), true),
emailVerified: db.default(z.boolean(), false),
lastLoginAt: z.datetime().optional(),
organizationId: db.organization()
}),
indexes: [
{
expressions: (self) => [self.realm, sql`LOWER(${self.username})`],
unique: true,
name: "users_realm_username_lower_idx"
},
{
columns: ["realm", "email"],
unique: true
},
{
columns: ["realm", "phoneNumber"],
unique: true
}
]
});
//#endregion
//#region ../../src/api/users/entities/identities.ts
const identities = $entity({
name: "identities",
schema: z.object({
id: db.primaryKey(z.uuid()),
version: db.version(),
createdAt: db.createdAt(),
updatedAt: db.updatedAt(),
userId: db.ref(z.uuid(), () => users.cols.id),
password: z.text().optional(),
provider: z.text(),
providerUserId: z.text().optional(),
providerData: z.json().optional()
}),
indexes: [
"userId",
"provider",
{ columns: ["userId", "provider"] },
{
columns: ["provider", "providerUserId"],
unique: true
}
]
});
//#endregion
//#region ../../src/api/users/entities/sessions.ts
const sessions = $entity({
name: "sessions",
schema: z.object({
id: db.primaryKey(z.uuid()),
version: db.version(),
createdAt: db.createdAt(),
updatedAt: db.updatedAt(),
refreshToken: z.uuid(),
userId: db.ref(z.uuid(), () => users.cols.id),
/**
* OAuth client this session was minted for, when it was created via the
* OAuth 2.1 authorization flow — the `client_id` of an `oauth_clients`
* row. Null for first-party logins. Deliberately NOT a DB-level foreign
* key: `sessions` is a core entity and must not depend on the optional
* OAuth module's table; the join to `oauth_clients` is done at query time.
*/
clientId: z.text({ maxLength: 64 }).optional(),
expiresAt: z.datetime(),
/**
* Last time the session was used to refresh an access token.
* Used by realm `refreshToken.expirationIdle` to invalidate idle sessions.
* `null` on existing rows pre-migration — falls back to `createdAt`.
*/
lastUsedAt: z.datetime().optional(),
ip: z.text().optional(),
/**
* ISO 3166-1 alpha-2 country code derived from the request geo headers
* (`cf-ipcountry` on Cloudflare, CDN equivalents elsewhere) at login time.
* `null` on pre-migration rows and where geo isn't available.
*/
country: z.text({ maxLength: 2 }).optional(),
userAgent: z.object({
os: z.text(),
browser: z.text(),
device: z.enum([
"MOBILE",
"DESKTOP",
"TABLET"
])
}).optional()
}),
indexes: [
"userId",
"expiresAt",
{
column: "refreshToken",
unique: true
}
]
});
//#endregion
//#region ../../src/api/users/schemas/registerSchema.ts
const registerSchema = z.object({
username: z.string().min(3).max(20).regex(/^[a-zA-Z0-9_]+$/).describe("Username for the new account"),
email: z.email().describe("Email address for the new account"),
password: z.string().min(8).describe("Password for the new account"),
confirmPassword: z.string().min(8).describe("Confirmation of the password"),
firstName: z.string().max(100).describe("User's first name").optional(),
lastName: z.string().max(100).describe("User's last name").optional()
});
//#endregion
//#region ../../src/api/users/schemas/resetPasswordSchema.ts
const resetPasswordRequestSchema = z.object({ email: z.email().describe("Email address to send password reset link") });
const resetPasswordSchema = z.object({
token: z.string().describe("Password reset token from email"),
password: z.string().min(8).describe("New password"),
confirmPassword: z.string().min(8).describe("Confirmation of the new password")
});
//#endregion
//#region ../../src/api/users/index.browser.ts
const AlephaApiUsers = $module({
name: "alepha.api.users",
services: []
});
//#endregion
export { AlephaApiUsers, DEFAULT_USER_REALM_NAME, identities, registerSchema, resetPasswordRequestSchema, resetPasswordSchema, sessions, users };
//# sourceMappingURL=index.browser.js.map