chore: refactor code

This commit is contained in:
Catalin Pit
2024-02-27 15:22:02 +02:00
parent af30443f5a
commit f0a511e238
5 changed files with 37 additions and 18 deletions

View File

@ -1,4 +1,4 @@
import { getUserByApiToken } from '../../public-api/get-user-by-token';
import { getApiTokenByToken } from '../../public-api/get-api-token-by-token';
type ValidateApiTokenOptions = {
authorization: string | undefined;
@ -9,7 +9,11 @@ export const validateApiToken = async ({ authorization }: ValidateApiTokenOption
// Support for both "Authorization: Bearer api_xxx" and "Authorization: api_xxx"
const [token] = (authorization || '').split('Bearer ').filter((s) => s.length > 0);
return await getUserByApiToken({ token });
if (!token) {
throw new Error('Missing API token');
}
return await getApiTokenByToken({ token });
} catch (err) {
throw new Error(`Failed to validate API token`);
}