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,27 @@
import bcrypt from "bcrypt";
import { generateId, generateToken } from "../utils/id";
import { CreateTokenInput, tokenModel } from "./token.schema";
export async function createToken(input: CreateTokenInput) {
const tokenId = generateId();
const newToken = await generateToken();
const tokenHash = await bcrypt.hash(newToken, 10);
const tokenInDb = await tokenModel.create({
pid: tokenId,
hash: tokenHash,
createdAt: new Date(),
...input,
});
return {
pid: tokenInDb.pid,
name: tokenInDb.name,
claims: tokenInDb.claims,
token: `${tokenInDb.pid}.${newToken}`,
};
}
export async function getToken(tokenId: string) {
return await tokenModel.findOne({ pid: tokenId });
}