From cf7d89eb2037dcce076ede333ff4f78b9298f72b Mon Sep 17 00:00:00 2001 From: Max Leiter Date: Wed, 9 Nov 2022 19:46:12 -0800 Subject: [PATCH] cookies fixes, hook improvement, more porting --- client/app/(posts)/new/from/[id]/page.tsx | 88 ++++++------------- client/app/(posts)/new/layout.tsx | 4 + client/app/(posts)/new/page.tsx | 6 +- client/app/mine/page.tsx | 34 +++++++ client/components/admin/index.tsx | 5 +- client/components/auth/index.tsx | 3 +- .../badges/visibility-control/index.tsx | 5 +- client/components/new-post/index.tsx | 11 +-- client/components/post-list/index.tsx | 9 +- client/components/post-page/index.tsx | 4 +- client/components/preview/index.tsx | 3 +- .../components/settings/sections/password.tsx | 4 +- .../components/settings/sections/profile.tsx | 4 +- client/lib/hooks/use-signed-in.ts | 1 + client/lib/hooks/use-user-data.ts | 13 +-- .../auth/AuthClientContextProvider.tsx | 60 ------------- client/lib/providers/auth/AuthProvider.tsx | 27 ------ .../auth/AuthServerContextProvider.tsx | 25 ------ .../{api => server}/generate-access-token.ts | 2 +- .../get-html-from-drift-file.ts | 0 .../hooks/use-redirect-if-not-authed.ts | 12 +++ client/lib/server/is-signed-in.ts | 6 ++ client/lib/{api => server}/jwt.ts | 0 client/lib/{api => server}/parse-url-query.ts | 0 client/lib/{api => server}/signin.ts | 0 client/pages/api/auth/signin.ts | 2 +- client/pages/api/auth/signup.ts | 4 +- client/pages/api/file/get-html.ts | 4 +- client/pages/api/posts/users-posts.ts | 11 +++ client/pages/api/welcome.ts | 1 - client/pages/mine.tsx | 67 -------------- 31 files changed, 138 insertions(+), 277 deletions(-) create mode 100644 client/app/(posts)/new/layout.tsx create mode 100644 client/app/mine/page.tsx delete mode 100644 client/lib/providers/auth/AuthClientContextProvider.tsx delete mode 100644 client/lib/providers/auth/AuthProvider.tsx delete mode 100644 client/lib/providers/auth/AuthServerContextProvider.tsx rename client/lib/{api => server}/generate-access-token.ts (96%) rename client/lib/{api => server}/get-html-from-drift-file.ts (100%) create mode 100644 client/lib/server/hooks/use-redirect-if-not-authed.ts create mode 100644 client/lib/server/is-signed-in.ts rename client/lib/{api => server}/jwt.ts (100%) rename client/lib/{api => server}/parse-url-query.ts (100%) rename client/lib/{api => server}/signin.ts (100%) create mode 100644 client/pages/api/posts/users-posts.ts delete mode 100644 client/pages/mine.tsx diff --git a/client/app/(posts)/new/from/[id]/page.tsx b/client/app/(posts)/new/from/[id]/page.tsx index b20974c8..e520fe66 100644 --- a/client/app/(posts)/new/from/[id]/page.tsx +++ b/client/app/(posts)/new/from/[id]/page.tsx @@ -1,67 +1,33 @@ -import styles from "@styles/Home.module.css" import NewPost from "@components/new-post" -import PageSeo from "@components/page-seo" -import { Page } from "@geist-ui/core/dist" -import Head from "next/head" -import { GetServerSideProps } from "next" -import { Post } from "@lib/types" -import cookie from "cookie" +import { useRouter } from "next/navigation" +import { cookies } from "next/headers" +import { TOKEN_COOKIE_NAME } from "@lib/constants" +import { getPostWithFiles } from "app/prisma" +import { useRedirectIfNotAuthed } from "@lib/server/hooks/use-redirect-if-not-authed" +const NewFromExisting = async ({ + params +}: { + params: { + id: string + } +}) => { + const { id } = params + const router = useRouter() + const cookieList = cookies() + useRedirectIfNotAuthed() + const driftToken = cookieList.get(TOKEN_COOKIE_NAME) + if (!driftToken) { + return router.push("/signin") + } -const NewFromExisting = async () => { - return ( - // - // {/* TODO: solve this. */} - // {/* eslint-disable-next-line @next/next/no-css-tags */} - // - // - - ) + if (!id) { + return router.push("/new") + } + + const post = await getPostWithFiles(id) + + return } -// export const getServerSideProps: GetServerSideProps = async ({ -// req, -// params -// }) => { -// const id = params?.id -// const redirect = { -// redirect: { -// destination: "/new", -// permanent: false -// } -// } - -// if (!id) { -// return redirect -// } - -// const driftToken = cookie.parse(req.headers.cookie || "")[`drift-token`] - -// const post = await fetch(`${process.env.API_URL}/posts/${id}`, { -// method: "GET", -// headers: { -// "Content-Type": "application/json", -// Authorization: `Bearer ${driftToken}`, -// "x-secret-key": process.env.SECRET_KEY || "" -// } -// }) - -// if (!post.ok) { -// return redirect -// } - -// const data = await post.json() - -// if (!data) { -// return redirect -// } - -// return { -// props: { -// post: data, -// parentId: id -// } -// } -// } - export default NewFromExisting diff --git a/client/app/(posts)/new/layout.tsx b/client/app/(posts)/new/layout.tsx new file mode 100644 index 00000000..479eb04b --- /dev/null +++ b/client/app/(posts)/new/layout.tsx @@ -0,0 +1,4 @@ +export default function NewLayout({ children }: { children: React.ReactNode }) { + // useRedirectIfNotAuthed() + return <>{children}; +} diff --git a/client/app/(posts)/new/page.tsx b/client/app/(posts)/new/page.tsx index ed68e74f..aa0621a3 100644 --- a/client/app/(posts)/new/page.tsx +++ b/client/app/(posts)/new/page.tsx @@ -1,10 +1,6 @@ import NewPost from "@components/new-post" import '@styles/react-datepicker.css' -const New = () => { - return ( - - ) -} +const New = () => export default New diff --git a/client/app/mine/page.tsx b/client/app/mine/page.tsx new file mode 100644 index 00000000..2835c15e --- /dev/null +++ b/client/app/mine/page.tsx @@ -0,0 +1,34 @@ +import styles from "@styles/Home.module.css" + +import MyPosts from "@components/my-posts" +import type { GetServerSideProps } from "next" +import { Post } from "@lib/types" +import { Page } from "@geist-ui/core/dist" +import { getCookie } from "cookies-next" +import { TOKEN_COOKIE_NAME } from "@lib/constants" +import { useRouter } from "next/navigation" +import { cookies } from "next/headers" +export default function Mine() { + const router = useRouter() + const driftToken = cookies().get(TOKEN_COOKIE_NAME) + if (!driftToken) { + return router.push("/signin") + } + + // const posts = await fetch(process.env.API_URL + `/posts/mine`, { + // method: "GET", + // headers: { + // "Content-Type": "application/json", + // Authorization: `Bearer ${driftToken}`, + // "x-secret-key": process.env.SECRET_KEY || "" + // } + // }) + + if (!posts.ok) { + return router.push("/signin") + } + + const { posts, error, hasMore } = await posts.json() + + return +} diff --git a/client/components/admin/index.tsx b/client/components/admin/index.tsx index 1d104eb6..34c7b5b7 100644 --- a/client/components/admin/index.tsx +++ b/client/components/admin/index.tsx @@ -1,4 +1,5 @@ -import Cookies from "js-cookie" +import { TOKEN_COOKIE_NAME } from "@lib/constants" +import { getCookie } from "cookies-next" import styles from "./admin.module.css" import PostTable from "./post-table" import UserTable from "./user-table" @@ -14,7 +15,7 @@ export const adminFetcher = async ( method: options?.method || "GET", headers: { "Content-Type": "application/json", - Authorization: `Bearer ${Cookies.get("drift-token")}` + Authorization: `Bearer ${getCookie(TOKEN_COOKIE_NAME)}` }, body: options?.body && JSON.stringify(options.body) }) diff --git a/client/components/auth/index.tsx b/client/components/auth/index.tsx index 822456a8..f8c7075e 100644 --- a/client/components/auth/index.tsx +++ b/client/components/auth/index.tsx @@ -10,6 +10,7 @@ import Input from "@components/input" import Button from "@components/button" import Note from "@components/note" import { USER_COOKIE_NAME } from "@lib/constants" +import { setCookie } from "cookies-next" const NO_EMPTY_SPACE_REGEX = /^\S*$/ const ERROR_MESSAGE = @@ -33,7 +34,7 @@ const Auth = ({ const handleJson = (json: any) => { signin(json.token) - Cookies.set(USER_COOKIE_NAME, json.userId) + setCookie(USER_COOKIE_NAME, json.userId) router.push("/new") } diff --git a/client/components/badges/visibility-control/index.tsx b/client/components/badges/visibility-control/index.tsx index 72234e9d..5a6bd665 100644 --- a/client/components/badges/visibility-control/index.tsx +++ b/client/components/badges/visibility-control/index.tsx @@ -1,7 +1,8 @@ import PasswordModal from "@components/new-post/password-modal" import { Button, ButtonGroup, Loading, useToasts } from "@geist-ui/core/dist" +import { TOKEN_COOKIE_NAME } from "@lib/constants" import type { PostVisibility } from "@lib/types" -import Cookies from "js-cookie" +import { getCookie } from "cookies-next" import { useCallback, useState } from "react" type Props = { @@ -21,7 +22,7 @@ const VisibilityControl = ({ postId, visibility, setVisibility }: Props) => { method: "PUT", headers: { "Content-Type": "application/json", - Authorization: `Bearer ${Cookies.get("drift-token")}` + Authorization: `Bearer ${getCookie(TOKEN_COOKIE_NAME)}` }, body: JSON.stringify({ visibility, password }) }) diff --git a/client/components/new-post/index.tsx b/client/components/new-post/index.tsx index 12e68a96..f2362e65 100644 --- a/client/components/new-post/index.tsx +++ b/client/components/new-post/index.tsx @@ -2,12 +2,11 @@ import { Button, useToasts, Input, ButtonDropdown } from "@geist-ui/core/dist" import { useRouter } from "next/navigation" -import { useCallback, useEffect, useMemo, useRef, useState } from "react" +import { useCallback, useState } from "react" import generateUUID from "@lib/generate-uuid" import FileDropzone from "./drag-and-drop" import styles from "./post.module.css" import Title from "./title" -import Cookies from "js-cookie" import type { PostVisibility, Document as DocumentType } from "@lib/types" import PasswordModal from "./password-modal" import EditDocumentList from "@components/edit-document-list" @@ -16,7 +15,8 @@ import DatePicker from "react-datepicker" import getTitleForPostCopy from "@lib/get-title-for-post-copy" import Description from "./description" import { PostWithFiles } from "app/prisma" -import { USER_COOKIE_NAME } from "@lib/constants" +import { TOKEN_COOKIE_NAME, USER_COOKIE_NAME } from "@lib/constants" +import { getCookie } from "cookies-next" const emptyDoc = { title: "", @@ -68,7 +68,7 @@ const Post = ({ method: "POST", headers: { "Content-Type": "application/json", - Authorization: `Bearer ${Cookies.get("drift-token")}` + Authorization: `Bearer ${getCookie(TOKEN_COOKIE_NAME)}` }, body: JSON.stringify({ title, @@ -140,12 +140,13 @@ const Post = ({ return } + const cookieName = getCookie(USER_COOKIE_NAME) await sendRequest("/api/posts/create", { title, files: docs, visibility, password, - userId: Cookies.get(USER_COOKIE_NAME) || "", + userId: cookieName ? String(getCookie(USER_COOKIE_NAME)) : "", expiresAt: expiresAt || null, parentId: newPostParent }) diff --git a/client/components/post-list/index.tsx b/client/components/post-list/index.tsx index 67d890a1..2fed3a54 100644 --- a/client/components/post-list/index.tsx +++ b/client/components/post-list/index.tsx @@ -5,9 +5,10 @@ import ListItemSkeleton from "./list-item-skeleton" import ListItem from "./list-item" import { Post } from "@lib/types" import { ChangeEvent, useCallback, useEffect, useMemo, useState } from "react" -import Cookies from "js-cookie" import useDebounce from "@lib/hooks/use-debounce" import Link from "@components/link" +import { TOKEN_COOKIE_NAME } from "@lib/constants" +import { getCookie } from "cookies-next" type Props = { initialPosts: Post[] @@ -32,7 +33,7 @@ const PostList = ({ morePosts, initialPosts, error }: Props) => { method: "GET", headers: { "Content-Type": "application/json", - Authorization: `Bearer ${Cookies.get("drift-token")}`, + Authorization: `Bearer ${getCookie(TOKEN_COOKIE_NAME)}`, "x-page": `${posts.length / 10 + 1}` } }) @@ -61,7 +62,7 @@ const PostList = ({ morePosts, initialPosts, error }: Props) => { method: "GET", headers: { "Content-Type": "application/json", - Authorization: `Bearer ${Cookies.get("drift-token")}` + Authorization: `Bearer ${getCookie(TOKEN_COOKIE_NAME)}` // "tok": process.env.SECRET_KEY || '' } } @@ -97,7 +98,7 @@ const PostList = ({ morePosts, initialPosts, error }: Props) => { method: "DELETE", headers: { "Content-Type": "application/json", - Authorization: `Bearer ${Cookies.get("drift-token")}` + Authorization: `Bearer ${getCookie(TOKEN_COOKIE_NAME)}` } }) diff --git a/client/components/post-page/index.tsx b/client/components/post-page/index.tsx index e8e9d294..9f3528a1 100644 --- a/client/components/post-page/index.tsx +++ b/client/components/post-page/index.tsx @@ -15,10 +15,10 @@ import ScrollToTop from "@components/scroll-to-top" import { useRouter } from "next/router" import ExpirationBadge from "@components/badges/expiration-badge" import CreatedAgoBadge from "@components/badges/created-ago-badge" -import Cookies from "js-cookie" import PasswordModalPage from "./password-modal-wrapper" import VisibilityControl from "@components/badges/visibility-control" import { USER_COOKIE_NAME } from "@lib/constants" +import { getCookie } from "cookies-next" type Props = { post: Post @@ -33,7 +33,7 @@ const PostPage = ({ post: initialPost, isProtected }: Props) => { ) const [isLoading, setIsLoading] = useState(true) const [isOwner] = useState( - post.users ? post.users[0].id === Cookies.get(USER_COOKIE_NAME) : false + post.users ? post.users[0].id === getCookie(USER_COOKIE_NAME) : false ) const router = useRouter() const isMobile = useMediaQuery("mobile") diff --git a/client/components/preview/index.tsx b/client/components/preview/index.tsx index cbf7c0fa..377d0e40 100644 --- a/client/components/preview/index.tsx +++ b/client/components/preview/index.tsx @@ -1,3 +1,4 @@ +import { getCookie } from "cookies-next" import Cookies from "js-cookie" import { memo, useEffect, useState } from "react" import styles from "./preview.module.css" @@ -35,7 +36,7 @@ const MarkdownPreview = ({ height = 500, fileId, content, title }: Props) => { method: "GET", headers: { "Content-Type": "application/json", - Authorization: `Bearer ${Cookies.get("drift-token") || ""}` + Authorization: `Bearer ${getCookie("drift-token")}` }, }) diff --git a/client/components/settings/sections/password.tsx b/client/components/settings/sections/password.tsx index 19beb12f..87459876 100644 --- a/client/components/settings/sections/password.tsx +++ b/client/components/settings/sections/password.tsx @@ -1,4 +1,6 @@ import { Input, Button, useToasts } from "@geist-ui/core/dist" +import { TOKEN_COOKIE_NAME } from "@lib/constants" +import { getCookie } from "cookies-next" import Cookies from "js-cookie" import { useState } from "react" @@ -43,7 +45,7 @@ const Password = () => { method: "PUT", headers: { "Content-Type": "application/json", - Authorization: `Bearer ${Cookies.get("drift-token")}` + Authorization: `Bearer ${getCookie(TOKEN_COOKIE_NAME)}` }, body: JSON.stringify({ oldPassword: password, diff --git a/client/components/settings/sections/profile.tsx b/client/components/settings/sections/profile.tsx index 7e36ef08..7905fd9f 100644 --- a/client/components/settings/sections/profile.tsx +++ b/client/components/settings/sections/profile.tsx @@ -1,5 +1,7 @@ import { Note, Input, Textarea, Button, useToasts } from "@geist-ui/core/dist" +import { TOKEN_COOKIE_NAME } from "@lib/constants" import useUserData from "@lib/hooks/use-user-data" +import { getCookie } from "cookies-next" import Cookies from "js-cookie" import { useEffect, useState } from "react" @@ -50,7 +52,7 @@ const Profile = () => { method: "PUT", headers: { "Content-Type": "application/json", - Authorization: `Bearer ${Cookies.get("drift-token")}` + Authorization: `Bearer ${getCookie(TOKEN_COOKIE_NAME)}` }, body: JSON.stringify(data) }) diff --git a/client/lib/hooks/use-signed-in.ts b/client/lib/hooks/use-signed-in.ts index 49b6314c..a3010a11 100644 --- a/client/lib/hooks/use-signed-in.ts +++ b/client/lib/hooks/use-signed-in.ts @@ -9,6 +9,7 @@ const useSignedIn = () => { "signedIn", typeof window === "undefined" ? false : !!token ) + const signin = (token: string) => { setSignedIn(true) // TODO: investigate SameSite / CORS cookie security diff --git a/client/lib/hooks/use-user-data.ts b/client/lib/hooks/use-user-data.ts index 3cf93992..bfc6c51e 100644 --- a/client/lib/hooks/use-user-data.ts +++ b/client/lib/hooks/use-user-data.ts @@ -1,25 +1,26 @@ import { User } from "@lib/types" -import Cookies from "js-cookie" +import { deleteCookie, getCookie } from "cookies-next" import { useRouter } from "next/navigation" import { useEffect, useState } from "react" const useUserData = () => { + const cookie = getCookie("drift-token") const [authToken, setAuthToken] = useState( - Cookies.get("drift-token") || "" + cookie ? String(cookie) : "" ) const [user, setUser] = useState() const router = useRouter() useEffect(() => { - const token = Cookies.get("drift-token") + const token = getCookie("drift-token") if (token) { - setAuthToken(token) + setAuthToken(String(token)) } }, [setAuthToken]) useEffect(() => { if (authToken) { const fetchUser = async () => { - const response = await fetch(`/server-api/user/self`, { + const response = await fetch(`/api/user/self`, { headers: { Authorization: `Bearer ${authToken}` } @@ -28,7 +29,7 @@ const useUserData = () => { const user = await response.json() setUser(user) } else { - Cookies.remove("drift-token") + deleteCookie("drift-token") setAuthToken("") router.push("/") } diff --git a/client/lib/providers/auth/AuthClientContextProvider.tsx b/client/lib/providers/auth/AuthClientContextProvider.tsx deleted file mode 100644 index 283d653a..00000000 --- a/client/lib/providers/auth/AuthClientContextProvider.tsx +++ /dev/null @@ -1,60 +0,0 @@ -"use client"; - -import clsx from "clsx"; -import type { - ChangeEventHandler, - FunctionComponent, - PropsWithChildren, -} from "react"; -import Cookies from "js-cookie"; -import React, { useContext, useState, createContext } from "react"; -import { DEFAULT_THEME, Theme, THEME_COOKIE_NAME } from "./theme"; - -const ThemeContext = createContext(null); - -export function useTheme(): Theme { - return useContext(ThemeContext); -} - -interface Props extends PropsWithChildren { - defaultTheme: Theme; -} - -const ThemeClientContextProvider: FunctionComponent = ({ - defaultTheme, - children, -}) => { - const [theme, setTheme] = useState(defaultTheme); - const onChange: ChangeEventHandler = (e) => { - const value = e.target.value as Theme; - setTheme(value); - - if (value === DEFAULT_THEME) { - Cookies.remove(THEME_COOKIE_NAME); - } else { - Cookies.set(THEME_COOKIE_NAME, value); - } - }; - const onReset = () => { - setTheme(DEFAULT_THEME); - Cookies.remove(THEME_COOKIE_NAME); - }; - - return ( -
-
-

Theme Switcher

- - -
- {children} -
- ); -}; - -export default ThemeClientContextProvider; diff --git a/client/lib/providers/auth/AuthProvider.tsx b/client/lib/providers/auth/AuthProvider.tsx deleted file mode 100644 index 3a58f29d..00000000 --- a/client/lib/providers/auth/AuthProvider.tsx +++ /dev/null @@ -1,27 +0,0 @@ -import { FunctionComponent, PropsWithChildren } from "react"; -import ThemeClientContextProvider from "./ThemeClientContextProvider"; -import ThemeServerContextProvider, { - useServerTheme, -} from "./ThemeServerContextProvider"; - -const ThemeProviderWrapper: FunctionComponent = ({ - children, -}) => { - const theme = useServerTheme(); - - return ( - - {children} - - ); -}; - -const ThemeProvider: FunctionComponent = ({ children }) => { - return ( - - {children} - - ); -}; - -export default ThemeProvider; diff --git a/client/lib/providers/auth/AuthServerContextProvider.tsx b/client/lib/providers/auth/AuthServerContextProvider.tsx deleted file mode 100644 index dfdcc0c6..00000000 --- a/client/lib/providers/auth/AuthServerContextProvider.tsx +++ /dev/null @@ -1,25 +0,0 @@ -import type { FunctionComponent, PropsWithChildren } from "react"; -// @ts-ignore -- createServerContext is not in @types/react atm -import { useContext, createServerContext } from "react"; -import { cookies } from "next/headers"; -import { Theme, THEME_COOKIE_NAME } from "./theme"; -import { DEFAULT_THEME } from "./theme"; - -const ThemeContext = createServerContext(null); - -export function useServerTheme(): Theme { - return useContext(ThemeContext); -} - -const ThemeServerContextProvider: FunctionComponent = ({ - children, -}) => { - const cookiesList = cookies(); - const theme = cookiesList.get(THEME_COOKIE_NAME) ?? DEFAULT_THEME; - - return ( - {children} - ); -}; - -export default ThemeServerContextProvider; diff --git a/client/lib/api/generate-access-token.ts b/client/lib/server/generate-access-token.ts similarity index 96% rename from client/lib/api/generate-access-token.ts rename to client/lib/server/generate-access-token.ts index c52d43cc..f8dfb751 100644 --- a/client/lib/api/generate-access-token.ts +++ b/client/lib/server/generate-access-token.ts @@ -9,7 +9,7 @@ export async function generateAndExpireAccessToken(userId: User["id"]) { await prisma.authTokens.create({ data: { userId: userId, - token: token + token: token, } }) diff --git a/client/lib/api/get-html-from-drift-file.ts b/client/lib/server/get-html-from-drift-file.ts similarity index 100% rename from client/lib/api/get-html-from-drift-file.ts rename to client/lib/server/get-html-from-drift-file.ts diff --git a/client/lib/server/hooks/use-redirect-if-not-authed.ts b/client/lib/server/hooks/use-redirect-if-not-authed.ts new file mode 100644 index 00000000..c5a70d10 --- /dev/null +++ b/client/lib/server/hooks/use-redirect-if-not-authed.ts @@ -0,0 +1,12 @@ +import { useRouter } from 'next/navigation' +import { isSignedIn } from "../is-signed-in" + +export const useRedirectIfNotAuthed = (to = '/signin') => { + const router = useRouter(); + + const signedIn = isSignedIn(); + + if (!signedIn) { + router.push(to); + } +} diff --git a/client/lib/server/is-signed-in.ts b/client/lib/server/is-signed-in.ts new file mode 100644 index 00000000..a94ebf9a --- /dev/null +++ b/client/lib/server/is-signed-in.ts @@ -0,0 +1,6 @@ +import { cookies } from "next/headers" + +export const isSignedIn = () => { + const cookieList = cookies() + return cookieList.has("drift-token") && cookieList.has("drift-userid") +} diff --git a/client/lib/api/jwt.ts b/client/lib/server/jwt.ts similarity index 100% rename from client/lib/api/jwt.ts rename to client/lib/server/jwt.ts diff --git a/client/lib/api/parse-url-query.ts b/client/lib/server/parse-url-query.ts similarity index 100% rename from client/lib/api/parse-url-query.ts rename to client/lib/server/parse-url-query.ts diff --git a/client/lib/api/signin.ts b/client/lib/server/signin.ts similarity index 100% rename from client/lib/api/signin.ts rename to client/lib/server/signin.ts diff --git a/client/pages/api/auth/signin.ts b/client/pages/api/auth/signin.ts index 579bd898..69d23878 100644 --- a/client/pages/api/auth/signin.ts +++ b/client/pages/api/auth/signin.ts @@ -1,7 +1,7 @@ import { NextApiRequest, NextApiResponse } from "next" import prisma from "app/prisma" import bcrypt from "bcrypt" -import { signin } from "@lib/api/signin" +import { signin } from "@lib/server/signin" export default async function handler( req: NextApiRequest, diff --git a/client/pages/api/auth/signup.ts b/client/pages/api/auth/signup.ts index 4f9404f4..227dba22 100644 --- a/client/pages/api/auth/signup.ts +++ b/client/pages/api/auth/signup.ts @@ -2,7 +2,7 @@ import config from "@lib/config" import { NextApiRequest, NextApiResponse } from "next" import prisma from "app/prisma" import bcrypt, { genSalt } from "bcrypt" -import { generateAndExpireAccessToken } from "@lib/api/generate-access-token" +import { generateAndExpireAccessToken } from "@lib/server/generate-access-token" export default async function handler( req: NextApiRequest, @@ -35,7 +35,7 @@ export default async function handler( }, }) - const token = await generateAndExpireAccessToken(user) + const token = await generateAndExpireAccessToken(user.id) return res.status(201).json({ token: token, userId: user.id }) } diff --git a/client/pages/api/file/get-html.ts b/client/pages/api/file/get-html.ts index f5bf4e9e..9c47c38b 100644 --- a/client/pages/api/file/get-html.ts +++ b/client/pages/api/file/get-html.ts @@ -1,5 +1,5 @@ -import getHtmlFromFile from "@lib/api/get-html-from-drift-file" -import { parseUrlQuery } from "@lib/api/parse-url-query" +import getHtmlFromFile from "@lib/server/get-html-from-drift-file" +import { parseUrlQuery } from "@lib/server/parse-url-query" import prisma from "app/prisma" import { NextApiRequest, NextApiResponse } from "next" diff --git a/client/pages/api/posts/users-posts.ts b/client/pages/api/posts/users-posts.ts new file mode 100644 index 00000000..6bc862b1 --- /dev/null +++ b/client/pages/api/posts/users-posts.ts @@ -0,0 +1,11 @@ +import prisma from "app/prisma" + +export const getPostsByUser = async (userId: number) => { + const posts = await prisma.post.findMany({ + where: { + + } + }) + + return posts +} diff --git a/client/pages/api/welcome.ts b/client/pages/api/welcome.ts index 6915a935..8a6ab1ca 100644 --- a/client/pages/api/welcome.ts +++ b/client/pages/api/welcome.ts @@ -13,7 +13,6 @@ export const getWelcomeContent = async () => { console.log(introContent) - return { title: introTitle, content: introContent, diff --git a/client/pages/mine.tsx b/client/pages/mine.tsx deleted file mode 100644 index d24e050f..00000000 --- a/client/pages/mine.tsx +++ /dev/null @@ -1,67 +0,0 @@ -import styles from "@styles/Home.module.css" - -import Header from "@components/header" -import MyPosts from "@components/my-posts" -import cookie from "cookie" -import type { GetServerSideProps } from "next" -import { Post } from "@lib/types" -import { Page } from "@geist-ui/core/dist" - -const Home = ({ - morePosts, - posts, - error -}: { - morePosts: boolean - posts: Post[] - error: boolean -}) => { - return ( - - - - - - ) -} -// get server side props -export const getServerSideProps: GetServerSideProps = async ({ req }) => { - const driftToken = cookie.parse(req.headers.cookie || "")[`drift-token`] - if (!driftToken) { - return { - redirect: { - destination: "/", - permanent: false - } - } - } - - const posts = await fetch(process.env.API_URL + `/posts/mine`, { - method: "GET", - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${driftToken}`, - "x-secret-key": process.env.SECRET_KEY || "" - } - }) - - if (!posts.ok) { - return { - redirect: { - destination: "/", - permanent: false - } - } - } - - const data = await posts.json() - return { - props: { - posts: data.posts, - error: posts.status !== 200, - morePosts: data.hasMore - } - } -} - -export default Home