diff --git a/.env.example b/.env.example index b3a4cfa..46c0b58 100644 --- a/.env.example +++ b/.env.example @@ -32,4 +32,16 @@ ADMIN_EMAIL='dhravyashah@gmail.com' #Create a new redis database on upstash and copy/paste the keys from it #https://console.upstash.com/ UPSTASH_REDIS_REST_URL= -UPSTASH_REDIS_REST_TOKEN= \ No newline at end of file +UPSTASH_REDIS_REST_TOKEN= + +# GitHub OAuth +GITHUB_CLIENT_ID= +GITHUB_CLIENT_SECRET= + +# Twitter OAuth +TWITTER_CLIENT_ID= +TWITTER_CLIENT_SECRET= + +# Facebook OAuth +FACEBOOK_CLIENT_ID= +FACEBOOK_CLIENT_SECRET= diff --git a/src/app/actions.ts b/src/app/actions.ts index 03548ce..d2340c3 100644 --- a/src/app/actions.ts +++ b/src/app/actions.ts @@ -33,6 +33,17 @@ export async function handleNameSubmit( }; } + // password should be at least 8 characters long, contain at least one uppercase letter, one lowercase letter, one number, and one special character + if (!/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/.test(password)) { + return { + status: 400, + body: { + error: + "Password should be at least 8 characters long, contain at least one uppercase letter, one lowercase letter, one number, and one special character.", + }, + }; + } + const checkAvailability = await db.user.findFirst({ where: { username: name, diff --git a/src/app/api/auth/[...nextauth]/route.ts b/src/app/api/auth/[...nextauth]/route.ts index 1570f88..7eb3e67 100644 --- a/src/app/api/auth/[...nextauth]/route.ts +++ b/src/app/api/auth/[...nextauth]/route.ts @@ -1,7 +1,26 @@ import NextAuth from "next-auth"; - import { authOptions } from "@/server/auth"; +import { withAuth } from "next-auth/middleware"; +import { getSession } from "next-auth/react"; +import { sendVerificationRequest } from "@/server/mfa"; -// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment const handler = NextAuth(authOptions); + export { handler as GET, handler as POST }; + +export const config = { + matcher: ["/api/auth/:path*"], +}; + +export default withAuth({ + callbacks: { + async signIn({ user, account, profile, email, credentials }) { + const session = await getSession(); + if (session && session.user) { + // Send MFA verification request + await sendVerificationRequest(session.user.email); + } + return true; + }, + }, +}); diff --git a/src/server/auth.ts b/src/server/auth.ts index 9c7ebef..960f539 100644 --- a/src/server/auth.ts +++ b/src/server/auth.ts @@ -6,6 +6,9 @@ import { } from "next-auth"; // import EmailProvider from "next-auth/providers/email"; import GoogleProvider from "next-auth/providers/google"; +import GitHubProvider from "next-auth/providers/github"; +import TwitterProvider from "next-auth/providers/twitter"; +import FacebookProvider from "next-auth/providers/facebook"; import { env } from "@/env"; import { db } from "@/server/db"; @@ -82,6 +85,18 @@ export const authOptions: NextAuthOptions = { } } }), + GitHubProvider({ + clientId: env.GITHUB_CLIENT_ID, + clientSecret: env.GITHUB_CLIENT_SECRET, + }), + TwitterProvider({ + clientId: env.TWITTER_CLIENT_ID, + clientSecret: env.TWITTER_CLIENT_SECRET, + }), + FacebookProvider({ + clientId: env.FACEBOOK_CLIENT_ID, + clientSecret: env.FACEBOOK_CLIENT_SECRET, + }), ], };