Add error handling
This commit is contained in:
@@ -1,15 +1,22 @@
|
||||
import { Context } from "hono";
|
||||
import { hashToken } from "../auth";
|
||||
import { db } from "../db";
|
||||
import { UnsendApiError } from "./api-error";
|
||||
|
||||
export const getTeamFromToken = async (c: Context) => {
|
||||
const authHeader = c.req.header("Authorization");
|
||||
if (!authHeader) {
|
||||
throw new Error("No Authorization header provided");
|
||||
throw new UnsendApiError({
|
||||
code: "UNAUTHORIZED",
|
||||
message: "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");
|
||||
throw new UnsendApiError({
|
||||
code: "UNAUTHORIZED",
|
||||
message: "No Authorization header provided",
|
||||
});
|
||||
}
|
||||
|
||||
const hashedToken = hashToken(token);
|
||||
@@ -25,7 +32,10 @@ export const getTeamFromToken = async (c: Context) => {
|
||||
});
|
||||
|
||||
if (!team) {
|
||||
throw new Error("No team found for this token");
|
||||
throw new UnsendApiError({
|
||||
code: "FORBIDDEN",
|
||||
message: "Invalid API token",
|
||||
});
|
||||
}
|
||||
|
||||
return team;
|
||||
|
Reference in New Issue
Block a user