mirror of
https://github.com/movie-web/backend.git
synced 2025-09-13 16:43:26 +00:00
Fix @me endpoint
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "backend",
|
"name": "backend",
|
||||||
"version": "1.1.0",
|
"version": "1.1.1",
|
||||||
"private": true,
|
"private": true,
|
||||||
"homepage": "https://github.com/movie-web/backend",
|
"homepage": "https://github.com/movie-web/backend",
|
||||||
"engines": {
|
"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 { User, formatUser } from '@/db/models/User';
|
||||||
import { StatusError } from '@/services/error';
|
import { StatusError } from '@/services/error';
|
||||||
import { handle } from '@/services/handler';
|
import { handle } from '@/services/handler';
|
||||||
@@ -6,6 +6,24 @@ import { makeRouter } from '@/services/router';
|
|||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
export const userGetRouter = makeRouter((app) => {
|
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(
|
app.get(
|
||||||
'/users/:uid',
|
'/users/:uid',
|
||||||
{
|
{
|
||||||
@@ -17,25 +35,15 @@ export const userGetRouter = makeRouter((app) => {
|
|||||||
},
|
},
|
||||||
handle(async ({ auth, params, em }) => {
|
handle(async ({ auth, params, em }) => {
|
||||||
await auth.assert();
|
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);
|
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);
|
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 {
|
return {
|
||||||
user: formatUser(user),
|
user: formatUser(user),
|
||||||
session: session ? formatSession(session) : undefined,
|
|
||||||
};
|
};
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
Reference in New Issue
Block a user