29 lines
762 B
TypeScript
29 lines
762 B
TypeScript
import bcrypt from 'bcryptjs';
|
|
import { generateId, generateToken } from "../utils/id";
|
|
import { CreateTokenInput, tokenModel } from "./token.schema";
|
|
|
|
export async function createToken(input: CreateTokenInput, tenantId: string) {
|
|
const tokenId = generateId();
|
|
const newToken = await generateToken();
|
|
const tokenHash = await bcrypt.hash(newToken, 5);
|
|
|
|
const tokenInDb = await tokenModel.create({
|
|
tenantId: tenantId,
|
|
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 });
|
|
}
|