Add API authentication
This commit is contained in:
33
apps/web/src/server/public-api/auth.ts
Normal file
33
apps/web/src/server/public-api/auth.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { Context } from "hono";
|
||||
import { bearerAuth } from "hono/bearer-auth";
|
||||
import { hashToken } from "../auth";
|
||||
import { db } from "../db";
|
||||
|
||||
export const getTeamFromToken = async (c: Context) => {
|
||||
const authHeader = c.req.header("Authorization");
|
||||
if (!authHeader) {
|
||||
throw new Error("No Authorization header provided");
|
||||
}
|
||||
const token = authHeader.split(" ")[1]; // Assuming the Authorization header is in the format "Bearer <token>"
|
||||
if (!token) {
|
||||
throw new Error("No bearer token provided");
|
||||
}
|
||||
|
||||
const hashedToken = hashToken(token);
|
||||
|
||||
const team = await db.team.findFirst({
|
||||
where: {
|
||||
apiKeys: {
|
||||
some: {
|
||||
tokenHash: hashedToken,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!team) {
|
||||
throw new Error("No team found for this token");
|
||||
}
|
||||
|
||||
return team;
|
||||
};
|
Reference in New Issue
Block a user