mirror of
https://github.com/p-stream/backend.git
synced 2026-03-11 17:55:35 +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({
|
||||
srcDir: "server",
|
||||
compatibilityDate: "2025-03-05",
|
||||
experimental: {
|
||||
asyncContext: true,
|
||||
},
|
||||
runtimeConfig: {
|
||||
public: {
|
||||
meta: {
|
||||
|
|
|
|||
|
|
@ -8,32 +8,7 @@ const updateSessionSchema = z.object({
|
|||
export default defineEventHandler(async (event) => {
|
||||
const sessionId = getRouterParam(event, 'sid');
|
||||
|
||||
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 currentSession = await auth.getSessionAndBump(payload.sid);
|
||||
if (!currentSession) {
|
||||
throw createError({
|
||||
statusCode: 401,
|
||||
message: 'Session not found or expired'
|
||||
});
|
||||
}
|
||||
const currentSession = await useAuth().getCurrentSession();
|
||||
|
||||
const targetedSession = await prisma.sessions.findUnique({
|
||||
where: { id: sessionId }
|
||||
|
|
@ -97,7 +72,7 @@ export default defineEventHandler(async (event) => {
|
|||
if (!sessionExists) {
|
||||
return { success: true };
|
||||
}
|
||||
const session = await auth.getSessionAndBump(sid);
|
||||
const session = await useAuth().getSessionAndBump(sid);
|
||||
|
||||
await prisma.sessions.delete({
|
||||
where: { id: sessionId }
|
||||
|
|
|
|||
|
|
@ -1,32 +1,7 @@
|
|||
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 session = await useAuth().getCurrentSession()
|
||||
|
||||
const user = await prisma.users.findUnique({
|
||||
where: { id: session.user }
|
||||
|
|
|
|||
|
|
@ -17,32 +17,7 @@ export default defineEventHandler(async (event) => {
|
|||
const userId = event.context.params?.id;
|
||||
const method = event.method;
|
||||
|
||||
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 session = await useAuth().getCurrentSession();
|
||||
|
||||
if (session.user !== userId) {
|
||||
throw createError({
|
||||
|
|
|
|||
|
|
@ -2,32 +2,7 @@ export default defineEventHandler(async (event) => {
|
|||
const userId = getRouterParam(event, 'id')
|
||||
const tmdbId = getRouterParam(event, 'tmdbid')
|
||||
|
||||
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 session = await useAuth().getCurrentSession();
|
||||
|
||||
if (session.user !== userId) {
|
||||
throw createError({
|
||||
|
|
|
|||
|
|
@ -34,26 +34,7 @@ export default defineEventHandler(async (event) => {
|
|||
const userId = event.context.params?.id;
|
||||
const method = event.method;
|
||||
|
||||
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);
|
||||
const session = await useAuth().getCurrentSession();
|
||||
if (!session) {
|
||||
throw createError({
|
||||
statusCode: 401,
|
||||
|
|
|
|||
|
|
@ -35,32 +35,7 @@ export default defineEventHandler(async (event) => {
|
|||
const tmdbId = event.context.params?.tmdb_id;
|
||||
const method = event.method;
|
||||
|
||||
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'
|
||||
});
|
||||
}
|
||||
const session = await useAuth().getCurrentSession();
|
||||
|
||||
if (session.user !== userId) {
|
||||
throw createError({
|
||||
|
|
|
|||
|
|
@ -33,33 +33,8 @@ function defaultAndCoerceDateTime(dateTime: string | undefined) {
|
|||
export default defineEventHandler(async (event) => {
|
||||
const userId = event.context.params?.id;
|
||||
|
||||
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 session = await useAuth().getCurrentSession();
|
||||
|
||||
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) {
|
||||
throw createError({
|
||||
statusCode: 403,
|
||||
|
|
|
|||
|
|
@ -10,32 +10,7 @@ const userRatingsSchema = z.object({
|
|||
export default defineEventHandler(async (event) => {
|
||||
const userId = event.context.params?.id;
|
||||
|
||||
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 session = await useAuth().getCurrentSession();
|
||||
|
||||
if (session.user !== userId) {
|
||||
throw createError({
|
||||
|
|
|
|||
|
|
@ -3,32 +3,7 @@ import { useAuth } from '~/utils/auth';
|
|||
export default defineEventHandler(async (event) => {
|
||||
const userId = getRouterParam(event, 'id');
|
||||
|
||||
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 session = await useAuth().getCurrentSession();
|
||||
|
||||
if (session.user !== userId) {
|
||||
throw createError({
|
||||
|
|
|
|||
|
|
@ -13,32 +13,7 @@ const userSettingsSchema = z.object({
|
|||
export default defineEventHandler(async (event) => {
|
||||
const userId = event.context.params?.id;
|
||||
|
||||
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 session = await useAuth().getCurrentSession();
|
||||
|
||||
if (session.user !== userId) {
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
||||
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 {
|
||||
getSession,
|
||||
getSessionAndBump,
|
||||
makeSession,
|
||||
makeSessionToken,
|
||||
verifySessionToken
|
||||
verifySessionToken,
|
||||
getCurrentSession
|
||||
};
|
||||
}
|
||||
Loading…
Reference in a new issue