mirror of
https://github.com/PretendoNetwork/website.git
synced 2026-09-25 18:25:40 -05:00
feat: add support for refresh tokens in internal stores
This commit is contained in:
27
server/api/auth/refresh.post.ts
Normal file
27
server/api/auth/refresh.post.ts
Normal 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;
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user