mirror of
https://github.com/p-stream/backend.git
synced 2026-04-21 12:52:16 +00:00
created a better useAuth util to remove the constant boilerplate, now to get a user its just const user = await useAuth().getCurrentSession() and it handles all the content
This commit is contained in:
parent
3e88b42770
commit
180fac4164
13 changed files with 46 additions and 307 deletions
|
|
@ -5,6 +5,9 @@ import { version } from "./server/utils/config";
|
||||||
export default defineNitroConfig({
|
export default defineNitroConfig({
|
||||||
srcDir: "server",
|
srcDir: "server",
|
||||||
compatibilityDate: "2025-03-05",
|
compatibilityDate: "2025-03-05",
|
||||||
|
experimental: {
|
||||||
|
asyncContext: true,
|
||||||
|
},
|
||||||
runtimeConfig: {
|
runtimeConfig: {
|
||||||
public: {
|
public: {
|
||||||
meta: {
|
meta: {
|
||||||
|
|
|
||||||
|
|
@ -8,32 +8,7 @@ const updateSessionSchema = z.object({
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
const sessionId = getRouterParam(event, 'sid');
|
const sessionId = getRouterParam(event, 'sid');
|
||||||
|
|
||||||
const authHeader = getRequestHeader(event, 'authorization');
|
const currentSession = await useAuth().getCurrentSession();
|
||||||
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 401,
|
|
||||||
message: 'Unauthorized'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const token = authHeader.split(' ')[1];
|
|
||||||
const auth = useAuth();
|
|
||||||
|
|
||||||
const payload = auth.verifySessionToken(token);
|
|
||||||
if (!payload) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 401,
|
|
||||||
message: 'Invalid token'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const currentSession = await auth.getSessionAndBump(payload.sid);
|
|
||||||
if (!currentSession) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 401,
|
|
||||||
message: 'Session not found or expired'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const targetedSession = await prisma.sessions.findUnique({
|
const targetedSession = await prisma.sessions.findUnique({
|
||||||
where: { id: sessionId }
|
where: { id: sessionId }
|
||||||
|
|
@ -97,7 +72,7 @@ export default defineEventHandler(async (event) => {
|
||||||
if (!sessionExists) {
|
if (!sessionExists) {
|
||||||
return { success: true };
|
return { success: true };
|
||||||
}
|
}
|
||||||
const session = await auth.getSessionAndBump(sid);
|
const session = await useAuth().getSessionAndBump(sid);
|
||||||
|
|
||||||
await prisma.sessions.delete({
|
await prisma.sessions.delete({
|
||||||
where: { id: sessionId }
|
where: { id: sessionId }
|
||||||
|
|
|
||||||
|
|
@ -1,32 +1,7 @@
|
||||||
import { useAuth } from '~/utils/auth';
|
import { useAuth } from '~/utils/auth';
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
const authHeader = getRequestHeader(event, 'authorization');
|
const session = await useAuth().getCurrentSession()
|
||||||
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 401,
|
|
||||||
message: 'Unauthorized'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const token = authHeader.split(' ')[1];
|
|
||||||
const auth = useAuth();
|
|
||||||
|
|
||||||
const payload = auth.verifySessionToken(token);
|
|
||||||
if (!payload) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 401,
|
|
||||||
message: 'Invalid token'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const session = await auth.getSessionAndBump(payload.sid);
|
|
||||||
if (!session) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 401,
|
|
||||||
message: 'Session not found or expired'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const user = await prisma.users.findUnique({
|
const user = await prisma.users.findUnique({
|
||||||
where: { id: session.user }
|
where: { id: session.user }
|
||||||
|
|
|
||||||
|
|
@ -17,32 +17,7 @@ export default defineEventHandler(async (event) => {
|
||||||
const userId = event.context.params?.id;
|
const userId = event.context.params?.id;
|
||||||
const method = event.method;
|
const method = event.method;
|
||||||
|
|
||||||
const authHeader = getRequestHeader(event, 'authorization');
|
const session = await useAuth().getCurrentSession();
|
||||||
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 401,
|
|
||||||
message: 'Unauthorized'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const token = authHeader.split(' ')[1];
|
|
||||||
const auth = useAuth();
|
|
||||||
|
|
||||||
const payload = auth.verifySessionToken(token);
|
|
||||||
if (!payload) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 401,
|
|
||||||
message: 'Invalid token'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const session = await auth.getSessionAndBump(payload.sid);
|
|
||||||
if (!session) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 401,
|
|
||||||
message: 'Session not found or expired'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (session.user !== userId) {
|
if (session.user !== userId) {
|
||||||
throw createError({
|
throw createError({
|
||||||
|
|
|
||||||
|
|
@ -2,32 +2,7 @@ export default defineEventHandler(async (event) => {
|
||||||
const userId = getRouterParam(event, 'id')
|
const userId = getRouterParam(event, 'id')
|
||||||
const tmdbId = getRouterParam(event, 'tmdbid')
|
const tmdbId = getRouterParam(event, 'tmdbid')
|
||||||
|
|
||||||
const authHeader = getRequestHeader(event, 'authorization');
|
const session = await useAuth().getCurrentSession();
|
||||||
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 401,
|
|
||||||
message: 'Unauthorized'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const token = authHeader.split(' ')[1];
|
|
||||||
const auth = useAuth();
|
|
||||||
|
|
||||||
const payload = auth.verifySessionToken(token);
|
|
||||||
if (!payload) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 401,
|
|
||||||
message: 'Invalid token'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const session = await auth.getSessionAndBump(payload.sid);
|
|
||||||
if (!session) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 401,
|
|
||||||
message: 'Session not found or expired'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (session.user !== userId) {
|
if (session.user !== userId) {
|
||||||
throw createError({
|
throw createError({
|
||||||
|
|
|
||||||
|
|
@ -34,26 +34,7 @@ export default defineEventHandler(async (event) => {
|
||||||
const userId = event.context.params?.id;
|
const userId = event.context.params?.id;
|
||||||
const method = event.method;
|
const method = event.method;
|
||||||
|
|
||||||
const authHeader = getRequestHeader(event, 'authorization');
|
const session = await useAuth().getCurrentSession();
|
||||||
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 401,
|
|
||||||
message: 'Unauthorized'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const token = authHeader.split(' ')[1];
|
|
||||||
const auth = useAuth();
|
|
||||||
|
|
||||||
const payload = auth.verifySessionToken(token);
|
|
||||||
if (!payload) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 401,
|
|
||||||
message: 'Invalid token'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const session = await auth.getSessionAndBump(payload.sid);
|
|
||||||
if (!session) {
|
if (!session) {
|
||||||
throw createError({
|
throw createError({
|
||||||
statusCode: 401,
|
statusCode: 401,
|
||||||
|
|
|
||||||
|
|
@ -35,32 +35,7 @@ export default defineEventHandler(async (event) => {
|
||||||
const tmdbId = event.context.params?.tmdb_id;
|
const tmdbId = event.context.params?.tmdb_id;
|
||||||
const method = event.method;
|
const method = event.method;
|
||||||
|
|
||||||
const authHeader = getRequestHeader(event, 'authorization');
|
const session = await useAuth().getCurrentSession();
|
||||||
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 401,
|
|
||||||
message: 'Unauthorized'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const token = authHeader.split(' ')[1];
|
|
||||||
const auth = useAuth();
|
|
||||||
|
|
||||||
const payload = auth.verifySessionToken(token);
|
|
||||||
if (!payload) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 401,
|
|
||||||
message: 'Invalid token'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const session = await auth.getSessionAndBump(payload.sid);
|
|
||||||
if (!session) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 401,
|
|
||||||
message: 'Session not found'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (session.user !== userId) {
|
if (session.user !== userId) {
|
||||||
throw createError({
|
throw createError({
|
||||||
|
|
|
||||||
|
|
@ -33,33 +33,8 @@ function defaultAndCoerceDateTime(dateTime: string | undefined) {
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
const userId = event.context.params?.id;
|
const userId = event.context.params?.id;
|
||||||
|
|
||||||
const authHeader = getRequestHeader(event, 'authorization');
|
const session = await useAuth().getCurrentSession();
|
||||||
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 401,
|
|
||||||
message: 'Unauthorized'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const token = authHeader.split(' ')[1];
|
|
||||||
const auth = useAuth();
|
|
||||||
|
|
||||||
const payload = auth.verifySessionToken(token);
|
|
||||||
if (!payload) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 401,
|
|
||||||
message: 'Invalid token'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const session = await auth.getSessionAndBump(payload.sid);
|
|
||||||
if (!session) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 401,
|
|
||||||
message: 'Session not found or expired'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (session.user !== userId) {
|
if (session.user !== userId) {
|
||||||
throw createError({
|
throw createError({
|
||||||
statusCode: 403,
|
statusCode: 403,
|
||||||
|
|
|
||||||
|
|
@ -10,32 +10,7 @@ const userRatingsSchema = z.object({
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
const userId = event.context.params?.id;
|
const userId = event.context.params?.id;
|
||||||
|
|
||||||
const authHeader = getRequestHeader(event, 'authorization');
|
const session = await useAuth().getCurrentSession();
|
||||||
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 401,
|
|
||||||
message: 'Unauthorized'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const token = authHeader.split(' ')[1];
|
|
||||||
const auth = useAuth();
|
|
||||||
|
|
||||||
const payload = auth.verifySessionToken(token);
|
|
||||||
if (!payload) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 401,
|
|
||||||
message: 'Invalid token'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const session = await auth.getSessionAndBump(payload.sid);
|
|
||||||
if (!session) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 401,
|
|
||||||
message: 'Session not found or expired'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (session.user !== userId) {
|
if (session.user !== userId) {
|
||||||
throw createError({
|
throw createError({
|
||||||
|
|
|
||||||
|
|
@ -3,32 +3,7 @@ import { useAuth } from '~/utils/auth';
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
const userId = getRouterParam(event, 'id');
|
const userId = getRouterParam(event, 'id');
|
||||||
|
|
||||||
const authHeader = getRequestHeader(event, 'authorization');
|
const session = await useAuth().getCurrentSession();
|
||||||
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 401,
|
|
||||||
message: 'Unauthorized'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const token = authHeader.split(' ')[1];
|
|
||||||
const auth = useAuth();
|
|
||||||
|
|
||||||
const payload = auth.verifySessionToken(token);
|
|
||||||
if (!payload) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 401,
|
|
||||||
message: 'Invalid token'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const session = await auth.getSessionAndBump(payload.sid);
|
|
||||||
if (!session) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 401,
|
|
||||||
message: 'Session not found or expired'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (session.user !== userId) {
|
if (session.user !== userId) {
|
||||||
throw createError({
|
throw createError({
|
||||||
|
|
|
||||||
|
|
@ -13,32 +13,7 @@ const userSettingsSchema = z.object({
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
const userId = event.context.params?.id;
|
const userId = event.context.params?.id;
|
||||||
|
|
||||||
const authHeader = getRequestHeader(event, 'authorization');
|
const session = await useAuth().getCurrentSession();
|
||||||
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 401,
|
|
||||||
message: 'Unauthorized'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const token = authHeader.split(' ')[1];
|
|
||||||
const auth = useAuth();
|
|
||||||
|
|
||||||
const payload = auth.verifySessionToken(token);
|
|
||||||
if (!payload) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 401,
|
|
||||||
message: 'Invalid token'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const session = await auth.getSessionAndBump(payload.sid);
|
|
||||||
if (!session) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 401,
|
|
||||||
message: 'Session not found or expired'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (session.user !== userId) {
|
if (session.user !== userId) {
|
||||||
throw createError({
|
throw createError({
|
||||||
|
|
|
||||||
|
|
@ -1,51 +0,0 @@
|
||||||
import { useAuth } from '~/utils/auth';
|
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
|
||||||
const authHeader = getRequestHeader(event, 'authorization');
|
|
||||||
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 401,
|
|
||||||
message: 'Unauthorized'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const token = authHeader.split(' ')[1];
|
|
||||||
const auth = useAuth();
|
|
||||||
|
|
||||||
const payload = auth.verifySessionToken(token);
|
|
||||||
if (!payload) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 401,
|
|
||||||
message: 'Invalid token'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const session = await auth.getSessionAndBump(payload.sid);
|
|
||||||
if (!session) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 401,
|
|
||||||
message: 'Session not found or expired'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const user = await prisma.users.findUnique({
|
|
||||||
where: { id: session.user }
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!user) {
|
|
||||||
throw createError({
|
|
||||||
statusCode: 404,
|
|
||||||
message: 'User not found'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
user: {
|
|
||||||
id: user.id,
|
|
||||||
publicKey: user.public_key,
|
|
||||||
namespace: user.namespace,
|
|
||||||
profile: user.profile,
|
|
||||||
permissions: user.permissions
|
|
||||||
}
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
@ -73,12 +73,43 @@ export function useAuth() {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getCurrentSession = async () => {
|
||||||
|
const event = useEvent();
|
||||||
|
const authHeader = getRequestHeader(event, 'authorization');
|
||||||
|
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
||||||
|
throw createError({
|
||||||
|
statusCode: 401,
|
||||||
|
message: 'Unauthorized'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const token = authHeader.split(' ')[1];
|
||||||
|
const payload = verifySessionToken(token);
|
||||||
|
if (!payload) {
|
||||||
|
throw createError({
|
||||||
|
statusCode: 401,
|
||||||
|
message: 'Invalid token'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const session = await getSessionAndBump(payload.sid);
|
||||||
|
if (!session) {
|
||||||
|
throw createError({
|
||||||
|
statusCode: 401,
|
||||||
|
message: 'Session not found or expired'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return session;
|
||||||
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
getSession,
|
getSession,
|
||||||
getSessionAndBump,
|
getSessionAndBump,
|
||||||
makeSession,
|
makeSession,
|
||||||
makeSessionToken,
|
makeSessionToken,
|
||||||
verifySessionToken
|
verifySessionToken,
|
||||||
|
getCurrentSession
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue