mirror of
https://github.com/PretendoNetwork/website.git
synced 2026-10-12 18:18:30 -05:00
38 lines
943 B
TypeScript
38 lines
943 B
TypeScript
import { ServerError } from "nice-grpc";
|
|
import { ApiAuthLogin, LoginSchema } from "#shared/api-types"
|
|
import { useLegacyApiGrpc } from "~~/server/utils/useGrpc";
|
|
|
|
export default defineEventHandler(async (event): Promise<ApiAuthLogin> => {
|
|
const body = await readZodBody(event, LoginSchema);
|
|
const grpc = useApiGrpc(event);
|
|
|
|
try {
|
|
const res = await grpc.login({
|
|
username: body.username,
|
|
password: body.password,
|
|
grantType: 'password'
|
|
});
|
|
|
|
return {
|
|
accessToken: res.accessToken,
|
|
refreshToken: res.refreshToken
|
|
};
|
|
} catch (error: unknown) {
|
|
if (error instanceof ServerError) {
|
|
if (error.details === 'INVALID_ARGUMENT: User not found') {
|
|
throw createError({
|
|
status: 400,
|
|
message: 'User not found'
|
|
});
|
|
}
|
|
if (error.details === 'INVALID_ARGUMENT: Password is incorrect') {
|
|
throw createError({
|
|
status: 400,
|
|
message: 'Password was incorrect'
|
|
});
|
|
}
|
|
}
|
|
throw error;
|
|
}
|
|
});
|