commit
c18ff489c3
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "backend",
|
||||
"version": "1.1.0",
|
||||
"version": "1.1.1",
|
||||
"private": true,
|
||||
"homepage": "https://github.com/movie-web/backend",
|
||||
"engines": {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { Session, formatSession } from '@/db/models/Session';
|
||||
import { formatSession } from '@/db/models/Session';
|
||||
import { User, formatUser } from '@/db/models/User';
|
||||
import { StatusError } from '@/services/error';
|
||||
import { handle } from '@/services/handler';
|
||||
|
@ -6,6 +6,24 @@ import { makeRouter } from '@/services/router';
|
|||
import { z } from 'zod';
|
||||
|
||||
export const userGetRouter = makeRouter((app) => {
|
||||
app.get(
|
||||
'/users/@me',
|
||||
handle(async ({ auth, em }) => {
|
||||
await auth.assert();
|
||||
|
||||
const user = await em.findOne(User, { id: auth.user.id });
|
||||
if (!user) throw new StatusError('User does not exist', 404);
|
||||
|
||||
const session = await auth.getSession();
|
||||
if (!session) throw new StatusError('Session does not exist', 400);
|
||||
|
||||
return {
|
||||
user: formatUser(user),
|
||||
session: formatSession(session),
|
||||
};
|
||||
}),
|
||||
);
|
||||
|
||||
app.get(
|
||||
'/users/:uid',
|
||||
{
|
||||
|
@ -17,25 +35,15 @@ export const userGetRouter = makeRouter((app) => {
|
|||
},
|
||||
handle(async ({ auth, params, em }) => {
|
||||
await auth.assert();
|
||||
let uid = params.uid;
|
||||
if (uid === '@me') uid = auth.user.id;
|
||||
|
||||
if (auth.user.id !== uid)
|
||||
if (auth.user.id !== params.uid)
|
||||
throw new StatusError('Cannot access users other than yourself', 403);
|
||||
|
||||
const user = await em.findOne(User, { id: uid });
|
||||
const user = await em.findOne(User, { id: params.uid });
|
||||
if (!user) throw new StatusError('User does not exist', 404);
|
||||
|
||||
let session: Session | undefined = undefined;
|
||||
|
||||
if (uid === '@me') {
|
||||
session = (await auth.getSession()) ?? undefined;
|
||||
if (!session) throw new StatusError('Session does not exist', 400);
|
||||
}
|
||||
|
||||
return {
|
||||
user: formatUser(user),
|
||||
session: session ? formatSession(session) : undefined,
|
||||
};
|
||||
}),
|
||||
);
|
||||
|
|
Loading…
Reference in New Issue