Added token authentication, organization module. Moved server bootstrapping code to server.ts file
This commit is contained in:
27
src/tokens/token.service.ts
Normal file
27
src/tokens/token.service.ts
Normal 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 });
|
||||
}
|
||||
Reference in New Issue
Block a user