feat: add support for refresh tokens in internal stores

This commit is contained in:
mrjvs
2026-08-16 22:17:49 +02:00
parent b3b44751ea
commit 47d703f6fd
7 changed files with 53 additions and 15 deletions

View File

@@ -0,0 +1,27 @@
import { ClientError } from 'nice-grpc';
import { RefreshSchema } from '#shared/api-types';
import type { ApiAuthLogin } from '#shared/api-types';
export default defineEventHandler(async (event): Promise<ApiAuthLogin> => {
const body = await readZodBody(event, RefreshSchema);
const grpc = useApiGrpc(event);
try {
const res = await grpc.login({
refreshToken: body.token,
grantType: 'refresh_token'
});
return {
accessToken: res.accessToken,
refreshToken: res.refreshToken
};
} catch (error: unknown) {
if (error instanceof ClientError) {
if (error.details === 'INVALID_ARGUMENT: Invalid or missing refresh token') {
throw createApiError('UNAUTHENTICATED');
}
}
throw error;
}
});

View File

@@ -20,10 +20,7 @@ export function getAuthContext(event: H3Event): AuthContext | null {
export function enforceLoggedIn(event: H3Event): AuthContext {
const context = getAuthContext(event);
if (!context) {
throw createError({
status: 401,
message: 'This action requires authentication'
});
throw createApiError('UNAUTHENTICATED');
}
return context;
}