bcrypt fix

basically webpack tries to compile it which is not good
so I edited to next.config
This commit is contained in:
andrzej 2024-09-13 19:24:39 +02:00
parent 1861630bf3
commit 5aa503a236
8 changed files with 91 additions and 35 deletions

View File

View File

@ -1,4 +1,9 @@
/** @type {import('next').NextConfig} */
const nextConfig = {};
const nextConfig = {
webpack: (config) => {
config.externals = [...config.externals, "bcrypt"];
return config;
},
};
export default nextConfig;

37
package-lock.json generated
View File

@ -9,6 +9,7 @@
"version": "0.1.0",
"dependencies": {
"@hookform/resolvers": "^3.6.0",
"@mapbox/node-pre-gyp": "^1.0.11",
"@prisma/client": "^5.15.0",
"@radix-ui/react-checkbox": "^1.0.4",
"@radix-ui/react-context-menu": "^2.2.1",
@ -21,8 +22,7 @@
"@radix-ui/react-slot": "^1.0.2",
"@radix-ui/react-toast": "^1.1.5",
"@tanstack/react-table": "^8.17.3",
"@types/bcrypt": "^5.0.2",
"bcrypt": "^5.1.1",
"bcryptjs": "^2.4.3",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"date-fns": "^3.6.0",
@ -1270,14 +1270,6 @@
"url": "https://github.com/sponsors/tannerlinsley"
}
},
"node_modules/@types/bcrypt": {
"version": "5.0.2",
"resolved": "https://registry.npmjs.org/@types/bcrypt/-/bcrypt-5.0.2.tgz",
"integrity": "sha512-6atioO8Y75fNcbmj0G7UjI9lXN2pQ/IGJ2FWT4a/btd0Lk9lQalHLKhkgKVZ3r+spnmWUKfbMi1GEe9wyHQfNQ==",
"dependencies": {
"@types/node": "*"
}
},
"node_modules/@types/d3-array": {
"version": "3.2.1",
"resolved": "https://registry.npmjs.org/@types/d3-array/-/d3-array-3.2.1.tgz",
@ -1342,6 +1334,7 @@
"version": "20.14.9",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.14.9.tgz",
"integrity": "sha512-06OCtnTXtWOZBJlRApleWndH4JsRVs1pDCc8dLSQp+7PpUpX3ePdHyeNSFTeSe7FtKyQkrlPvHwJOW3SLd8Oyg==",
"dev": true,
"dependencies": {
"undici-types": "~5.26.4"
}
@ -3074,18 +3067,10 @@
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz",
"integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw=="
},
"node_modules/bcrypt": {
"version": "5.1.1",
"resolved": "https://registry.npmjs.org/bcrypt/-/bcrypt-5.1.1.tgz",
"integrity": "sha512-AGBHOG5hPYZ5Xl9KXzU5iKq9516yEmvCKDg3ecP5kX2aB6UqTeXZxk2ELnDgDm6BQSMlLt9rDB4LoSMx0rYwww==",
"hasInstallScript": true,
"dependencies": {
"@mapbox/node-pre-gyp": "^1.0.11",
"node-addon-api": "^5.0.0"
},
"engines": {
"node": ">= 10.0.0"
}
"node_modules/bcryptjs": {
"version": "2.4.3",
"resolved": "https://registry.npmjs.org/bcryptjs/-/bcryptjs-2.4.3.tgz",
"integrity": "sha512-V/Hy/X9Vt7f3BbPJEi8BdVFMByHi+jNXrYkW3huaybV/kQ0KJg0Y6PkEMbn+zeT+i+SiKZ/HMqJGIIt4LZDqNQ=="
},
"node_modules/binary-extensions": {
"version": "2.3.0",
@ -6639,11 +6624,6 @@
"node": "^10 || ^12 || >=14"
}
},
"node_modules/node-addon-api": {
"version": "5.1.0",
"resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-5.1.0.tgz",
"integrity": "sha512-eh0GgfEkpnoWDq+VY8OyvYhFEzBk6jIYbRKdIlyTiAXIVJ8PyBaKb0rp7oDtoddbdoHWhq8wwr+XZ81F1rpNdA=="
},
"node_modules/node-fetch": {
"version": "2.7.0",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz",
@ -9828,7 +9808,8 @@
"node_modules/undici-types": {
"version": "5.26.5",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz",
"integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA=="
"integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==",
"dev": true
},
"node_modules/update-browserslist-db": {
"version": "1.0.16",

View File

@ -12,6 +12,7 @@
},
"dependencies": {
"@hookform/resolvers": "^3.6.0",
"@mapbox/node-pre-gyp": "^1.0.11",
"@prisma/client": "^5.15.0",
"@radix-ui/react-checkbox": "^1.0.4",
"@radix-ui/react-context-menu": "^2.2.1",
@ -24,8 +25,7 @@
"@radix-ui/react-slot": "^1.0.2",
"@radix-ui/react-toast": "^1.1.5",
"@tanstack/react-table": "^8.17.3",
"@types/bcrypt": "^5.0.2",
"bcrypt": "^5.1.1",
"bcryptjs": "^2.4.3",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"date-fns": "^3.6.0",

View File

@ -1,8 +1,7 @@
import prisma from 'app/lib/db';
import { jwtVerify, JWTPayload, decodeJwt } from 'jose';
import { cookies } from 'next/headers';
import bcrypt from 'bcrypt'
import { User } from '@prisma/client';
export function getJWTSecretKey() {
const secret = process.env.JWT_SECRET
@ -85,6 +84,7 @@ export async function login(userLogin: UserLogin) {
try {
const user = await prisma.user.findFirst({ where: { email: userLogin.email } })
if (!user) { throw new Error('user does not exist') }
const bcrypt = require("bcrypt");
const passwordIsValid = await bcrypt.compare(userLogin.password, user.password)
if (!passwordIsValid) throw new Error('invalid password')
//return the user object without the hashed password

View File

@ -1,5 +1,4 @@
import { NextResponse, NextRequest } from "next/server";
import prisma from "app/lib/db";
import { SignJWT } from "jose";
import { getJWTSecretKey, login, setUserDataCookie } from "../actions";

View File

@ -6,7 +6,6 @@ import { toast } from "@/components/ui/use-toast";
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button";
import { signIn } from "app/api/auth/actions/sign-in";
const formSchema = z.object({
email: z.string().email(),

72
src/middleware.ts Normal file
View File

@ -0,0 +1,72 @@
import { NextRequest, NextResponse } from "next/server";
import { verifyJwt } from "app/api/auth/actions";
const protectedRoutes = ['/story', '/submission', '/publication']
// Function to match the * wildcard character
function matchesWildcard(path: string, pattern: string): boolean {
if (pattern.endsWith('/*')) {
const basePattern = pattern.slice(0, -2);
return path.startsWith(basePattern);
}
return path === pattern;
}
export default async function(request: NextRequest) {
const LOGIN = `${process.env.NEXT_PUBLIC_BASE_URL}/login?redirect=${request.nextUrl.pathname + request.nextUrl.search}`
if (protectedRoutes.some(pattern => matchesWildcard(request.nextUrl.pathname, pattern))) {
const token = request.cookies.get('token')
//NOTE - may need to add logic to return 401 for api routes
if (!token) {
return NextResponse.redirect(LOGIN)
}
try {
//decode and verify jwt cookie
const jwtIsVerified = await verifyJwt(token.value)
if (!jwtIsVerified) {
//delete token
request.cookies.delete('token')
return NextResponse.redirect(LOGIN)
}
} catch {
//delete token (failsafe)
request.cookies.delete('token')
return NextResponse.redirect(LOGIN)
}
//redirect from login if already logged in
let redirectToApp = false
if (request.nextUrl.pathname === "/login") {
const token = request.cookies.get("token")
if (token) {
try {
const payload = await verifyJwt(token.value)
if (payload) {
redirectToApp = true
} else {
request.cookies.delete('token')
}
} catch (error) {
request.cookies.delete('token')
}
}
}
if (redirectToApp) {
return NextResponse.redirect(`${process.env.NEXT_PUBLIC_BASE_URL}/submission`)
} else {
return NextResponse.next()
}
}
}