diff --git a/src/constants/url.ts b/src/constants/url.ts new file mode 100644 index 0000000..7edc2c2 --- /dev/null +++ b/src/constants/url.ts @@ -0,0 +1,2 @@ +export const VALID_URL_REGEX = + /^(?:http(s)?:\/\/)[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.\%]+$/; diff --git a/src/pages/index.tsx b/src/pages/index.tsx index 6ec8d81..490568d 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -13,10 +13,10 @@ import PATHS from "constants/paths"; import useModal from "hooks/useModal"; import { Category, Link, SearchItem } from "types"; -import { BuildCategory } from "utils/front"; +import getUserCategories from "lib/category/getUserCategories"; +import getUser from "lib/user/getUser"; import { pushStateVanilla } from "utils/link"; -import prisma from "utils/prisma"; -import { getSessionOrThrow } from "utils/session"; +import { getSession } from "utils/session"; interface HomePageProps { categories: Category[]; @@ -187,29 +187,12 @@ function Home(props: HomePageProps) { } export async function getServerSideProps({ req, res, query }) { - const session = await getSessionOrThrow(req, res); + const session = await getSession(req, res); const queryCategoryId = (query?.categoryId as string) || ""; - const user = await prisma.user.findFirstOrThrow({ - where: { - email: session.user.email, - }, - }); - const categoriesDB = await prisma.category.findMany({ - include: { - links: { - where: { - authorId: user.id, - }, - }, - author: true, - }, - where: { - authorId: user.id, - }, - }); - - if (categoriesDB.length === 0) { + const user = await getUser(session); + const categories = await getUserCategories(user); + if (categories.length === 0) { return { redirect: { destination: PATHS.CATEGORY.CREATE, @@ -217,11 +200,9 @@ export async function getServerSideProps({ req, res, query }) { }; } - const categories = categoriesDB.map(BuildCategory); const currentCategory = categories.find( ({ id }) => id === Number(queryCategoryId) ); - return { props: { categories: JSON.parse(JSON.stringify(categories)), diff --git a/src/pages/link/create.tsx b/src/pages/link/create.tsx index 7694ee2..aceda9e 100644 --- a/src/pages/link/create.tsx +++ b/src/pages/link/create.tsx @@ -10,9 +10,11 @@ import Selector from "components/Selector"; import TextBox from "components/TextBox"; import useAutoFocus from "hooks/useAutoFocus"; +import getUserCategories from "lib/category/getUserCategories"; +import getUser from "lib/user/getUser"; import { Category, Link } from "types"; -import { BuildCategory, HandleAxiosError, IsValidURL } from "utils/front"; -import prisma from "utils/prisma"; +import { HandleAxiosError, IsValidURL } from "utils/front"; +import { getSession } from "utils/session"; import styles from "styles/create.module.scss"; @@ -110,14 +112,11 @@ function CreateLink({ categories }: { categories: Category[] }) { CreateLink.authRequired = true; export default CreateLink; -export async function getServerSideProps() { - const categoriesDB = await prisma.category.findMany({ - include: { author: true }, - }); - const categories = categoriesDB.map((categoryDB) => - BuildCategory(categoryDB) - ); +export async function getServerSideProps({ req, res }) { + const session = await getSession(req, res); + const user = await getUser(session); + const categories = await getUserCategories(user); if (categories.length === 0) { return { redirect: { diff --git a/src/pages/link/remove/[lid].tsx b/src/pages/link/remove/[lid].tsx index 481e303..6fe10a5 100644 --- a/src/pages/link/remove/[lid].tsx +++ b/src/pages/link/remove/[lid].tsx @@ -8,9 +8,11 @@ import FormLayout from "components/FormLayout"; import PageTransition from "components/PageTransition"; import TextBox from "components/TextBox"; +import getUserLink from "lib/link/getUserLink"; +import getUser from "lib/user/getUser"; import { Link } from "types"; -import { BuildLink, HandleAxiosError } from "utils/front"; -import prisma from "utils/prisma"; +import { HandleAxiosError } from "utils/front"; +import { getSession } from "utils/session"; import styles from "styles/create.module.scss"; @@ -94,14 +96,14 @@ function RemoveLink({ link }: { link: Link }) { RemoveLink.authRequired = true; export default RemoveLink; -export async function getServerSideProps({ query }) { +export async function getServerSideProps({ req, res, query }) { const { lid } = query; - const linkDB = await prisma.link.findFirst({ - where: { id: Number(lid) }, - include: { category: true, author: true }, - }); - if (!linkDB) { + const session = await getSession(req, res); + const user = await getUser(session); + + const link = await getUserLink(user, Number(lid)); + if (!link) { return { redirect: { destination: "/", @@ -109,10 +111,6 @@ export async function getServerSideProps({ query }) { }; } - const link = BuildLink(linkDB, { - categoryId: linkDB.categoryId, - categoryName: linkDB.category.name, - }); return { props: { link: JSON.parse(JSON.stringify(link)), diff --git a/src/pages/signin.tsx b/src/pages/signin.tsx index 54dd4f7..44e3aef 100644 --- a/src/pages/signin.tsx +++ b/src/pages/signin.tsx @@ -1,4 +1,3 @@ -import { getServerSession } from "next-auth/next"; import { Provider } from "next-auth/providers"; import { getProviders, signIn } from "next-auth/react"; import { NextSeo } from "next-seo"; @@ -6,9 +5,9 @@ import Link from "next/link"; import MessageManager from "components/MessageManager/MessageManager"; import PATHS from "constants/paths"; +import { getSession } from "utils/session"; import styles from "styles/login.module.scss"; -import { authOptions } from "./api/auth/[...nextauth]"; interface SignInProps { providers: Provider[]; @@ -39,7 +38,7 @@ export default function SignIn({ providers }: SignInProps) { } export async function getServerSideProps({ req, res }) { - const session = await getServerSession(req, res, authOptions); + const session = await getSession(req, res); if (session) { return { redirect: { diff --git a/src/types.d.ts b/src/types.d.ts index b8e8a38..b561a77 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -1,5 +1,7 @@ import { User } from "@prisma/client"; +// TODO: extend @prisma/client type with Link[] instead of +// recreate interface (same for Link) export interface Category { id: number; name: string; diff --git a/src/utils/front.ts b/src/utils/front.ts index 00dc492..ff5fd25 100644 --- a/src/utils/front.ts +++ b/src/utils/front.ts @@ -1,53 +1,8 @@ import axios from "axios"; - -import { Category, Link } from "types"; - -export function BuildCategory({ - id, - name, - authorId, - author, - links = [], - createdAt, - updatedAt, -}): Category { - return { - id, - name, - links: links.map((link) => - BuildLink(link, { categoryId: id, categoryName: name }) - ), - authorId, - author, - createdAt, - updatedAt, - }; -} - -export function BuildLink( - { id, name, url, authorId, author, favorite, createdAt, updatedAt }, - { categoryId, categoryName } -): Link { - return { - id, - name, - url, - category: { - id: categoryId, - name: categoryName, - }, - authorId, - author, - favorite, - createdAt, - updatedAt, - }; -} +import { VALID_URL_REGEX } from "constants/url"; export function IsValidURL(url: string): boolean { - const regex = new RegExp( - /^(?:http(s)?:\/\/)[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.\%]+$/ - ); + const regex = new RegExp(VALID_URL_REGEX); return url.match(regex) ? true : false; }