Added token authentication, organization module. Moved server bootstrapping code to server.ts file

This commit is contained in:
2024-12-19 21:49:54 +05:30
parent 970a972b11
commit 4b49c43a0c
16 changed files with 652 additions and 22 deletions

View File

@@ -0,0 +1,52 @@
import { buildJsonSchemas } from "fastify-zod";
import mongoose from "mongoose";
import { z } from "zod";
import { Claim } from "../utils/claims";
export const tokenModel = mongoose.model(
"token",
new mongoose.Schema({
tenantId: String,
pid: {
type: String,
unique: true,
},
name: String,
claims: [],
userId: mongoose.Types.ObjectId,
hash: { type: String, required: true },
createdAt: Date,
})
);
const tokenCore = {
name: z.string().max(30),
claims: z.array(z.string()).nonempty(),
};
const createTokenInput = z.object({
...tokenCore,
});
const createTokenResponse = z.object({
pid: z.string(),
token: z.string(),
...tokenCore,
});
const getTokenResponse = z.object({
pid: z.string(),
name: z.string(),
createdAt: z.string(),
});
export type CreateTokenInput = z.infer<typeof createTokenInput>;
export const { schemas: tokenSchemas, $ref: $token } = buildJsonSchemas(
{
createTokenInput,
createTokenResponse,
getTokenResponse,
},
{ $id: "token" }
);