From a5090d2cc30d9c87ed79ea9e5c0d04160d543641 Mon Sep 17 00:00:00 2001 From: Sianida26 Date: Mon, 22 Jan 2024 11:30:00 +0700 Subject: [PATCH] Added function for retrieve user by cookie --- package.json | 1 + pnpm-lock.yaml | 7 ++++++ postcss.config.js | 1 + src/app/dashboard/layout.tsx | 8 ++++++ src/features/auth/AuthError.ts | 1 + src/features/auth/actions/getUser.ts | 22 +++++++++++++++++ src/features/auth/authUtils.ts | 37 ++++++++++++++++++++++------ 7 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 src/features/auth/actions/getUser.ts diff --git a/package.json b/package.json index 8cbbd8c..edce5e7 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "react-dom": "^18.2.0", "react-icons": "^5.0.1", "sass": "^1.70.0", + "server-only": "^0.0.1", "superjson": "^2.2.1", "zod": "^3.22.4" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 77492bf..287c6b8 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -68,6 +68,9 @@ dependencies: sass: specifier: ^1.70.0 version: 1.70.0 + server-only: + specifier: ^0.0.1 + version: 0.0.1 superjson: specifier: ^2.2.1 version: 2.2.1 @@ -3084,6 +3087,10 @@ packages: dependencies: lru-cache: 6.0.0 + /server-only@0.0.1: + resolution: {integrity: sha512-qepMx2JxAa5jjfzxG79yPPq+8BuFToHd1hm7kI+Z4zAq1ftQiP7HcxMhDDItrbtwVeLg/cY2JnKnrcFkmiswNA==} + dev: false + /set-blocking@2.0.0: resolution: {integrity: sha512-KiKBS8AnWGEyLzofFfmvKwpdPzqiy16LvQfK3yv/fVH7Bj13/wl3JSR1J+rfgRE9q7xUJK4qvgS8raSOeLUehw==} dev: false diff --git a/postcss.config.js b/postcss.config.js index 91f8f41..2f7b444 100644 --- a/postcss.config.js +++ b/postcss.config.js @@ -1,5 +1,6 @@ module.exports = { plugins: { + 'tailwindcss/nesting': {}, tailwindcss: {}, autoprefixer: {}, 'postcss-preset-mantine': {}, diff --git a/src/app/dashboard/layout.tsx b/src/app/dashboard/layout.tsx index 18c2d96..265230f 100644 --- a/src/app/dashboard/layout.tsx +++ b/src/app/dashboard/layout.tsx @@ -6,6 +6,8 @@ import logo from "@/assets/logos/logo.png" import AppHeader from '../../components/AppHeader' import AppNavbar from '../../components/AppNavbar' import DashboardLayout from '@/components/DashboardLayout' +import getUser from '@/features/auth/actions/getUser' +import { redirect } from 'next/navigation' interface Props { children: React.ReactNode @@ -13,6 +15,12 @@ interface Props { export default function Layout(props: Props) { + const user = getUser() + + if (!user){ + redirect("/login") + } + return ( {props.children} diff --git a/src/features/auth/AuthError.ts b/src/features/auth/AuthError.ts index 65e7c3d..eb9a363 100644 --- a/src/features/auth/AuthError.ts +++ b/src/features/auth/AuthError.ts @@ -4,6 +4,7 @@ export enum AuthErrorCode { EMAIL_NOT_FOUND = "EMAIL_NOT_FOUND", EMPTY_USER_HASH = "EMPTY_USER_HASH", INVALID_CREDENTIALS = "INVALID_CREDENTIALS", + INVALID_JWT_TOKEN = "INVALID_JWT_TOKEN", JWT_SECRET_EMPTY = "JWT_SECRET_NOT_EMPTY", USER_ALREADY_EXISTS = "USER_ALREADY_EXISTS", } diff --git a/src/features/auth/actions/getUser.ts b/src/features/auth/actions/getUser.ts new file mode 100644 index 0000000..6662d20 --- /dev/null +++ b/src/features/auth/actions/getUser.ts @@ -0,0 +1,22 @@ +"use server" + +import { cookies } from "next/headers" +import "server-only" +import { decodeJwtToken } from "../authUtils"; +import prisma from "@/db"; + +export default async function getUser(){ + const token = cookies().get('token'); + + if (!token) return null; + + const decodedToken = decodeJwtToken(token.value) as {id: string, iat: number}; + + const user = await prisma.user.findFirst({ + where: { + id: decodedToken.id + } + }); + + return user; +} diff --git a/src/features/auth/authUtils.ts b/src/features/auth/authUtils.ts index d116ce8..9885b4c 100644 --- a/src/features/auth/authUtils.ts +++ b/src/features/auth/authUtils.ts @@ -1,9 +1,9 @@ -import prisma from "@/db"; +import bcrypt from "bcrypt"; +import jwt, { SignOptions, JwtPayload } from "jsonwebtoken"; import { User } from "@prisma/client"; -import * as bcrypt from "bcrypt"; +import prisma from "@/db"; import AuthError, { AuthErrorCode } from "./AuthError"; import authConfig from "@/config/auth"; -import jwt from "jsonwebtoken" import UserClaims from "./types/UserClaims"; /** @@ -27,9 +27,32 @@ export async function comparePassword(password: string, hash: string): Promise