mirror of
https://github.com/PretendoNetwork/website.git
synced 2026-09-07 16:36:55 -05:00
feat: fix authentication
This commit is contained in:
@@ -1,20 +1,9 @@
|
||||
import z from 'zod';
|
||||
import { ServerError } from 'nice-grpc';
|
||||
|
||||
export type ApiAuthLogin = {
|
||||
accessToken: string
|
||||
refreshToken: string;
|
||||
}
|
||||
|
||||
const LoginSchema = z.object({
|
||||
username: z.string(),
|
||||
password: z.string(),
|
||||
})
|
||||
export type ApiAuthLoginRequest = z.infer<typeof LoginSchema>
|
||||
import { ServerError } from "nice-grpc";
|
||||
import { ApiAuthLogin, LoginSchema } from "#shared/api-types"
|
||||
|
||||
export default defineEventHandler(async (event): Promise<ApiAuthLogin> => {
|
||||
const body = await readZodBody(event, LoginSchema);
|
||||
const grpc = useGrpc(event);
|
||||
const grpc = useApiGrpc(event);
|
||||
|
||||
try {
|
||||
const res = await grpc.login({
|
||||
@@ -25,21 +14,21 @@ export default defineEventHandler(async (event): Promise<ApiAuthLogin> => {
|
||||
|
||||
return {
|
||||
accessToken: res.accessToken,
|
||||
refreshToken: res.refreshToken,
|
||||
}
|
||||
refreshToken: res.refreshToken
|
||||
};
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof ServerError) {
|
||||
if (error.details === 'INVALID_ARGUMENT: User not found') {
|
||||
throw createError({
|
||||
status: 400,
|
||||
statusText: 'User not found',
|
||||
})
|
||||
statusText: 'User not found'
|
||||
});
|
||||
}
|
||||
if (error.details === 'INVALID_ARGUMENT: Password is incorrect') {
|
||||
throw createError({
|
||||
status: 400,
|
||||
statusText: 'Password was incorrect',
|
||||
})
|
||||
statusText: 'Password was incorrect'
|
||||
});
|
||||
}
|
||||
}
|
||||
throw error;
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
export interface GetApiAuthMe {
|
||||
pid: number;
|
||||
username: string;
|
||||
}
|
||||
import type { GetApiAuthMe } from "#shared/api-types"
|
||||
|
||||
export default defineEventHandler(async (event): Promise<GetApiAuthMe> => {
|
||||
const auth = enforceLoggedIn(event);
|
||||
|
||||
const grpc = useGrpc(event);
|
||||
const grpc = useAccountGrpc(event);
|
||||
const data = await grpc.getUserData({ pid: auth.pid });
|
||||
return {
|
||||
pid: data.pid,
|
||||
username: data.username,
|
||||
}
|
||||
mii: data.mii ? {
|
||||
imageUrl: `https://r2-cdn.pretendo.cc/mii/${data.pid}/normal_face.png`,
|
||||
name: data.mii.name,
|
||||
} : null,
|
||||
};
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user