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

Fix: Make jstack compatible with Next.js 15 (#11) #14

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Default is from neon.tech postgres, feel free to change
DATABASE_URL=

DATABASE_URL=
NEXT_PUBLIC_APP_URL=
# Optional for built-in caching: Get these from Upstash Redis console
REDIS_URL=
REDIS_TOKEN=
REDIS_URL=
REDIS_TOKEN=
36 changes: 36 additions & 0 deletions auth.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { NextAuthConfig } from "next-auth";
import Credentials from "next-auth/providers/credentials";
import { LoginSchema } from "@/src/features/auth/schemas";
import { getUserByEmail } from "@/src/features/auth/data/user";
import Github from "next-auth/providers/github"
import Google from "next-auth/providers/google"
import bcrypt from "bcryptjs";
export default {
providers: [
Github({
clientId: process.env.GITHUB_CLIENT_ID!,
clientSecret: process.env.GITHUB_CLIENT_SECRET!,
}),
Google({
clientId: process.env.GOOGLE_CLIENT_ID!,
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
}),
Credentials({
async authorize(credentials) {
const validatedFields = LoginSchema.safeParse(credentials);

if (validatedFields.success) {
const { email, password } = validatedFields.data;

const user = await getUserByEmail(email);
if (!user || !user.password) {
return null;
}
const passwordMatch = await bcrypt.compare(password, user.password);
if (passwordMatch) return user;
}
return null;
},
}),
],
} satisfies NextAuthConfig;
93 changes: 93 additions & 0 deletions auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import NextAuth from "next-auth";
import authConfig from "./auth.config";
import { PrismaAdapter } from "@auth/prisma-adapter";
import { db } from "@/src/db";
import { getUserById } from "@/src/features/auth/data/user";
import { UserRole } from "@prisma/client";
import { getTwoFactorConfirmationByUserId } from "@/src/features/auth/data/two-factor-confirmation";
import { getAccountByUserId } from "@/src/features/auth/data/account";

export const { handlers, signIn, signOut, auth } = NextAuth({
pages: {
signIn: "/auth/login",
error: "/auth/error",
// verifyRequest: "/auth/verify-request"
},
adapter: PrismaAdapter(db),
events: {
async linkAccount({ user }) {
await db.user.update({
where: {
id: user.id,
},
data: {
emailVerified: new Date(),
},
});
},
},
callbacks: {
async signIn({ user, account }) {
if (account?.provider !== "credentials") return true;
const existingUser = await getUserById(user.id!);
if (!existingUser || !existingUser.emailVerified) {
return false;
}
// todo: add 2fa check

if (existingUser.isTwoFactorEnabled) {
const twoFactorConfirmation = await getTwoFactorConfirmationByUserId(
existingUser.id
);
if (!twoFactorConfirmation) return false;

// TODO: Delete two factor confirmation for next signin
await db.twoFactorConfirmation.delete({
where: { id: twoFactorConfirmation.id },
});
}

return true;
},
async session({ token, session }) {
if (token.sub && session.user) {
session.user.id = token.sub;
}

if (token.role && session.user) {
session.user.role = token.role as UserRole;
}

if (session.user) {
session.user.isTwoFactorEnabled = token.isTwoFactorEnabled as boolean;
}

if (session.user) {
session.user.name = token.name;
session.user.email = token.email ?? '';
session.user.isOAuth = token.isOAuth as boolean;
}

return session;
},
async jwt({ token }) {
if (!token.sub) return token;

const existingUser = await getUserById(token.sub);

if (!existingUser) return token;

const existingAccount = await getAccountByUserId(existingUser.id);

token.isOAuth = !! existingAccount;
token.name = existingUser.name;
token.email = existingUser.email;
token.role = existingUser.role;
token.isTwoFactorEnabled = existingUser.isTwoFactorEnabled;

return token;
},
},
session: { strategy: "jwt" },
...authConfig,
});
Binary file removed bun.lockb
Binary file not shown.
21 changes: 21 additions & 0 deletions components.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"$schema": "https://ui.shadcn.com/schema.json",
"style": "default",
"rsc": true,
"tsx": true,
"tailwind": {
"config": "tailwind.config.ts",
"css": "src/app/globals.css",
"baseColor": "neutral",
"cssVariables": true,
"prefix": ""
},
"aliases": {
"components": "@/src/components",
"utils": "@/src/lib/utils",
"ui": "@/src/components/ui",
"lib": "@/src/lib",
"hooks": "@/src/hooks"
},
"iconLibrary": "lucide"
}
14 changes: 14 additions & 0 deletions next-auth.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { UserRole } from "@prisma/client";
import { DefaultSession } from "next-auth";

export type extendedUser = DefaultSession["user"] & {
role: UserRole;
isTwoFactorEnabled: boolean;
isOAuth: boolean;
};

declare module "next-auth" {
interface Session {
user: extendedUser;
}
}
53 changes: 45 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,76 @@
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"dev": "next dev ",
"build": "next build",
"start": "next start",
"lint": "next lint",
"deploy": "wrangler deploy",
"postinstall": "prisma generate"
},
"dependencies": {
"@auth/prisma-adapter": "^2.7.4",
"@hookform/resolvers": "^3.9.1",
"@neondatabase/serverless": "^0.9.4",
"@prisma/adapter-neon": "^5.18.0",
"@prisma/client": "^5.18.0",
"@radix-ui/react-avatar": "^1.1.1",
"@radix-ui/react-dialog": "^1.1.2",
"@radix-ui/react-dropdown-menu": "^2.1.2",
"@radix-ui/react-icons": "^1.3.2",
"@radix-ui/react-label": "^2.1.0",
"@radix-ui/react-popover": "^1.1.2",
"@radix-ui/react-scroll-area": "^1.2.1",
"@radix-ui/react-select": "^2.1.2",
"@radix-ui/react-slot": "^1.1.0",
"@radix-ui/react-tooltip": "^1.1.4",
"@tanstack/react-query": "^5.51.23",
"@uploadthing/react": "^7.1.1",
"@upstash/redis": "^1.34.0",
"bcrypt": "^5.1.1",
"bcryptjs": "^2.4.3",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
"cmdk": "1.0.0",
"hono": "^4.5.5",
"next": "^14.2.8",
"lucide-react": "^0.462.0",
"next": "15.0.3",
"next-auth": "5.0.0-beta.25",
"next-themes": "^0.4.3",
"prisma": "^5.18.0",
"react": "^18",
"react-dom": "^18",
"react": "19.0.0-rc-66855b96-20241106",
"react-dom": "19.0.0-rc-66855b96-20241106",
"react-hook-form": "^7.53.2",
"react-icons": "^5.3.0",
"react-spinners": "^0.14.1",
"resend": "^4.0.1",
"sonner": "^1.7.0",
"superjson": "^2.2.1",
"tailwind-merge": "^2.5.2",
"tailwind-merge": "^2.5.4",
"tailwindcss-animate": "^1.0.7",
"uploadthing": "^7.3.0",
"uuid": "^11.0.3",
"vaul": "^1.1.1",
"wrangler": "^3.72.0",
"zod": "^3.23.8"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20240815.0",
"@types/bcrypt": "^5.0.2",
"@types/bcryptjs": "^2.4.6",
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"@types/react": "npm:[email protected]",
"@types/react-dom": "npm:[email protected]",
"eslint": "^8",
"eslint-config-next": "14.2.5",
"eslint-config-next": "15.0.3",
"postcss": "^8",
"tailwindcss": "^3.4.1",
"typescript": "^5"
},
"pnpm": {
"overrides": {
"@types/react": "npm:[email protected]",
"@types/react-dom": "npm:[email protected]"
}
}
}
Loading