mirror of https://github.com/MaxLeiter/Drift
				
				
				
			dep management
							parent
							
								
									ecd4521403
								
							
						
					
					
						commit
						733a93dd87
					
				@ -1,7 +0,0 @@
 | 
			
		||||
"use client";
 | 
			
		||||
 | 
			
		||||
import { MDXRemote, MDXRemoteProps } from "next-mdx-remote";
 | 
			
		||||
 | 
			
		||||
export default function MDXRemoteWrapper(props: MDXRemoteProps) {
 | 
			
		||||
  return <MDXRemote {...props} />;
 | 
			
		||||
}
 | 
			
		||||
@ -1,41 +0,0 @@
 | 
			
		||||
import type { NextApiHandler, NextApiRequest, NextApiResponse } from "next"
 | 
			
		||||
import * as z from "zod"
 | 
			
		||||
import type { ZodSchema, ZodType } from "zod"
 | 
			
		||||
 | 
			
		||||
type NextApiRequestWithParsedBody<T> = NextApiRequest & {
 | 
			
		||||
	parsedBody?: T
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export type NextApiHandlerWithParsedBody<T> = (
 | 
			
		||||
	req: NextApiRequestWithParsedBody<T>,
 | 
			
		||||
	res: NextApiResponse
 | 
			
		||||
) => ReturnType<NextApiHandler>
 | 
			
		||||
 | 
			
		||||
export function withValidation<T extends ZodSchema>(
 | 
			
		||||
	schema: T,
 | 
			
		||||
	handler: NextApiHandler
 | 
			
		||||
): (
 | 
			
		||||
	req: NextApiRequest,
 | 
			
		||||
	res: NextApiResponse
 | 
			
		||||
) => Promise<void | NextApiResponse<any> | NextApiHandlerWithParsedBody<T>> {
 | 
			
		||||
	return async function (req: NextApiRequest, res: NextApiResponse) {
 | 
			
		||||
		try {
 | 
			
		||||
			const body = req.body
 | 
			
		||||
 | 
			
		||||
			await schema.parseAsync(body)
 | 
			
		||||
 | 
			
		||||
			;(req as NextApiRequestWithParsedBody<T>).parsedBody = body
 | 
			
		||||
 | 
			
		||||
			return handler(req, res) as Promise<NextApiHandlerWithParsedBody<T>>
 | 
			
		||||
		} catch (error) {
 | 
			
		||||
			if (process.env.NODE_ENV === "development") {
 | 
			
		||||
				console.error(error)
 | 
			
		||||
			}
 | 
			
		||||
			if (error instanceof z.ZodError) {
 | 
			
		||||
				return res.status(422).json(error.issues)
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			return res.status(422).end()
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@ -1,65 +0,0 @@
 | 
			
		||||
// next api route jwt middleware; check if the user has a valid jwt token
 | 
			
		||||
 | 
			
		||||
import config from "@lib/config"
 | 
			
		||||
import { User } from "@prisma/client"
 | 
			
		||||
import { prisma } from "@lib/server/prisma"
 | 
			
		||||
import * as jwt from "jsonwebtoken"
 | 
			
		||||
import next, { NextApiHandler, NextApiRequest, NextApiResponse } from "next"
 | 
			
		||||
 | 
			
		||||
type ReqWithUser = NextApiRequest & {
 | 
			
		||||
	user?: User
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type WrappedHandler = (req: ReqWithUser, res: NextApiResponse) => Promise<void>
 | 
			
		||||
 | 
			
		||||
// usage: useJwt(otherHandler)
 | 
			
		||||
 | 
			
		||||
// we want the usage to be the user writing their API route and exporting it with useJwt(handler)
 | 
			
		||||
 | 
			
		||||
// uses prisma
 | 
			
		||||
export async function withJwt(
 | 
			
		||||
	origHandler: NextApiHandler
 | 
			
		||||
): Promise<WrappedHandler | void> {
 | 
			
		||||
	return async (req: ReqWithUser, res: NextApiResponse) => {
 | 
			
		||||
		const authHeader = req ? req.headers["authorization"] : undefined
 | 
			
		||||
		const token = authHeader && authHeader.split(" ")[1]
 | 
			
		||||
 | 
			
		||||
		if (token == null) return res.status(401).send("Unauthorized")
 | 
			
		||||
 | 
			
		||||
		const authToken = await prisma.authTokens.findUnique({
 | 
			
		||||
			// @ts-ignore
 | 
			
		||||
			where: { id: token }
 | 
			
		||||
		})
 | 
			
		||||
		if (authToken == null) {
 | 
			
		||||
			return res.status(401).send("Unauthorized")
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if (authToken.deletedAt) {
 | 
			
		||||
			return res.status(401).json({
 | 
			
		||||
				message: "Token is no longer valid"
 | 
			
		||||
			})
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		jwt.verify(token, config.jwt_secret, async (err: any, user: any) => {
 | 
			
		||||
			if (err) return res.status(403).send("Forbidden")
 | 
			
		||||
			const userObj = await prisma.user.findUnique({
 | 
			
		||||
				where: { id: user.id },
 | 
			
		||||
				select: {
 | 
			
		||||
					id: true,
 | 
			
		||||
					email: true,
 | 
			
		||||
					// displayName: true,
 | 
			
		||||
					// bio: true,
 | 
			
		||||
					// createdAt: true,
 | 
			
		||||
					// updatedAt: true,
 | 
			
		||||
					// deletedAt: true
 | 
			
		||||
				}
 | 
			
		||||
			})
 | 
			
		||||
			if (!userObj) {
 | 
			
		||||
				return res.status(403).send("Forbidden")
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			;(req as ReqWithUser).user = user
 | 
			
		||||
			return origHandler(req, res)
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@ -1,25 +0,0 @@
 | 
			
		||||
import { USER_COOKIE_NAME, TOKEN_COOKIE_NAME } from "@lib/constants"
 | 
			
		||||
import { User } from "@lib/server/prisma"
 | 
			
		||||
import { setCookie } from "cookies-next"
 | 
			
		||||
import { NextApiRequest, NextApiResponse } from "next"
 | 
			
		||||
import { generateAndExpireAccessToken } from "./generate-access-token"
 | 
			
		||||
 | 
			
		||||
export const signin = async (
 | 
			
		||||
	userId: User["id"],
 | 
			
		||||
	req: NextApiRequest,
 | 
			
		||||
	res: NextApiResponse
 | 
			
		||||
) => {
 | 
			
		||||
	const token = await generateAndExpireAccessToken(userId)
 | 
			
		||||
	setCookie(USER_COOKIE_NAME, userId, {
 | 
			
		||||
		maxAge: 30 * 24 * 60 * 60, // 30 days,
 | 
			
		||||
		req,
 | 
			
		||||
		res
 | 
			
		||||
	})
 | 
			
		||||
	setCookie(TOKEN_COOKIE_NAME, token, {
 | 
			
		||||
		maxAge: 30 * 24 * 60 * 60, // 30 days
 | 
			
		||||
		req,
 | 
			
		||||
		res
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
	return token
 | 
			
		||||
}
 | 
			
		||||
@ -1,18 +0,0 @@
 | 
			
		||||
import { z } from "zod"
 | 
			
		||||
 | 
			
		||||
export const CreatePostSchema = z.object({
 | 
			
		||||
	title: z.string(),
 | 
			
		||||
	description: z.string(),
 | 
			
		||||
	files: z.array(z.object({
 | 
			
		||||
		title: z.string(),
 | 
			
		||||
		content: z.string(),
 | 
			
		||||
	})),
 | 
			
		||||
	visibility: z.string(),
 | 
			
		||||
	password: z.string().optional(),
 | 
			
		||||
	expiresAt: z.number().optional().nullish(),
 | 
			
		||||
	parentId: z.string().optional()
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
export const DeletePostSchema = z.object({
 | 
			
		||||
	id: z.string()
 | 
			
		||||
})
 | 
			
		||||
											
												
													File diff suppressed because it is too large
													Load Diff
												
											
										
									
								@ -1,6 +0,0 @@
 | 
			
		||||
{
 | 
			
		||||
	"dependencies": {
 | 
			
		||||
		"next": "^13.0.3",
 | 
			
		||||
		"eslint-config-next": "^13.0.3"
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
											
												
													File diff suppressed because it is too large
													Load Diff
												
											
										
									
								@ -1,4 +0,0 @@
 | 
			
		||||
import * as dotenv from "dotenv"
 | 
			
		||||
dotenv.config()
 | 
			
		||||
 | 
			
		||||
import "./src/server"
 | 
			
		||||
@ -1,7 +0,0 @@
 | 
			
		||||
{
 | 
			
		||||
  "$schema": "https://openapi.vercel.sh/vercel.json",
 | 
			
		||||
  "github": {
 | 
			
		||||
    "silent": true,
 | 
			
		||||
    "autoJobCancelation": true
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
					Loading…
					
					
				
		Reference in New Issue