Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Poc/next auth prisma #48

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions front/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const nextConfig = {
experimental: {
typedRoutes: true,
serverComponentsExternalPackages: ['@prisma/client','bcrypt']
},
};

Expand Down
19 changes: 13 additions & 6 deletions front/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,33 @@
"lint": "next lint",
"test": "jest",
"generate": "yarn prisma generate --generator client_js",
"db-push": "npx prisma db push",
"db-reset": "yarn prisma db push --force-reset --skip-generate",
"seed": "yarn prisma db seed",
"format": "prettier --write ."
},
"prisma": {
"schema": "../prisma/schema.prisma"
"schema": "../prisma/schema.prisma",
"seed": "ts-node --compiler-options {\"module\":\"CommonJS\"} ./prisma/seed.ts"
},
"engines": {
"node": ">=18.0.0"
},
"dependencies": {
"@auth/prisma-adapter": "^1.0.1",
"@fortawesome/fontawesome-svg-core": "^6.4.2",
"@fortawesome/free-solid-svg-icons": "^6.4.2",
"@fortawesome/react-fontawesome": "^0.2.0",
"@headlessui/react": "^1.7.17",
"@prisma/client": "5.1.1",
"@types/node": "20.4.8",
"@prisma/client": "^5.2.0",
"@types/node": "^20.5.7",
"@types/react": "18.2.18",
"@types/react-dom": "18.2.7",
"autoprefixer": "10.4.14",
"bcrypt": "^5.1.1",
"eslint": "8.46.0",
"eslint-config-next": "13.4.13",
"next": "13.4.13",
"next-auth": "^4.23.1",
"postcss": "8.4.27",
"react": "18.2.0",
"react-dom": "18.2.0",
Expand All @@ -43,6 +48,7 @@
"devDependencies": {
"@testing-library/jest-dom": "^6.0.1",
"@testing-library/react": "^14.0.0",
"@types/bcrypt": "^5.0.0",
"@types/jest": "^29.5.3",
"@typescript-eslint/eslint-plugin": "^6.3.0",
"aws-cdk-lib": "2.84.0",
Expand All @@ -52,7 +58,8 @@
"jest": "^29.6.2",
"jest-environment-jsdom": "^29.6.2",
"prettier": "^3.0.1",
"prisma": "^5.1.1",
"sst": "^2.23.6"
"prisma": "^5.2.0",
"sst": "^2.23.6",
"ts-node": "^10.9.1"
}
}
25 changes: 25 additions & 0 deletions front/prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { PrismaClient } from "@prisma/client";
import { hash } from "bcrypt";

const prisma = new PrismaClient();

async function main() {
const password = await hash("test", 12);
const user = await prisma.user.upsert({
where: { email: "test@test.com" },
update: {},
create: {
email: "test@test.com",
name: "Test User",
password,
},
});
console.log({ user });
}
main()
.then(() => prisma.$disconnect())
.catch(async (e) => {
console.error(e);
await prisma.$disconnect();
process.exit(1);
});
82 changes: 82 additions & 0 deletions front/src/app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { prisma } from "@/services/PrismaClient";
import { compare } from "bcrypt";
import NextAuth, { type NextAuthOptions } from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";

export const authOptions: NextAuthOptions = {
session: {
strategy: "jwt",
},
providers: [
CredentialsProvider({
name: "Sign in",
credentials: {
email: {
label: "Email",
type: "email",
placeholder: "hello@example.com",
},
password: { label: "Password", type: "password" },
},
async authorize(credentials) {
if (!credentials?.email || !credentials.password) {
return null; // invalidate form
}

const user = await prisma.user.findUnique({
where: {
email: credentials.email,
},
});

if (!user) {
return null;
}

const isPasswordValid = await compare(
credentials.password,
user.password,
);

if (!isPasswordValid) {
return null;
}

return {
id: user.id + "",
email: user.email,
name: user.name,
randomKey: "Hey cool",
};
},
}),
],
callbacks: {
session: ({ session, token }) => {
console.log("Session Callback", { session, token });
return {
...session,
user: {
...session.user,
id: token.id,
randomKey: token.randomKey,
},
};
},
jwt: ({ token, user }) => {
console.log("JWT Callback", { token, user });
if (user) {
const u = user as unknown as any;
return {
...token,
id: u.id,
randomKey: u.randomKey,
};
}
return token;
},
},
};

const handler = NextAuth(authOptions);
export { handler as GET, handler as POST };
1 change: 1 addition & 0 deletions front/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "next-auth/middleware";
Loading