From cd343980eb3f3a83ec62f0e4e77f4ebfce6405f5 Mon Sep 17 00:00:00 2001 From: shadcn Date: Tue, 25 Apr 2023 18:42:43 +0400 Subject: [PATCH] fix: move auth options --- app/api/auth/[...nextauth]/route.ts | 12 ++- lib/auth.ts | 152 ++++++++++++++-------------- 2 files changed, 86 insertions(+), 78 deletions(-) diff --git a/app/api/auth/[...nextauth]/route.ts b/app/api/auth/[...nextauth]/route.ts index 2e2718d..f320b5c 100644 --- a/app/api/auth/[...nextauth]/route.ts +++ b/app/api/auth/[...nextauth]/route.ts @@ -1,6 +1,14 @@ -import NextAuth from "next-auth" +import NextAuth, { NextAuthOptions } from "next-auth" +import GitHubProvider from "next-auth/providers/github" -import { authOptions } from "@/lib/auth" +export const authOptions: NextAuthOptions = { + providers: [ + GitHubProvider({ + clientId: process.env.GITHUB_CLIENT_ID || "", + clientSecret: process.env.GITHUB_CLIENT_SECRET || "", + }), + ], +} const handler = NextAuth(authOptions) diff --git a/lib/auth.ts b/lib/auth.ts index a31f0fd..885adf5 100644 --- a/lib/auth.ts +++ b/lib/auth.ts @@ -14,92 +14,92 @@ export const authOptions: NextAuthOptions = { // huh any! I know. // This is a temporary fix for prisma client. // @see https://github.com/prisma/prisma/issues/16117 - // adapter: PrismaAdapter(db as any), - // session: { - // strategy: "jwt", - // }, - // pages: { - // signIn: "/login", - // }, + adapter: PrismaAdapter(db as any), + session: { + strategy: "jwt", + }, + pages: { + signIn: "/login", + }, providers: [ GitHubProvider({ clientId: process.env.GITHUB_CLIENT_ID || "", clientSecret: process.env.GITHUB_CLIENT_SECRET || "", }), - // EmailProvider({ - // from: process.env.SMTP_FROM, - // sendVerificationRequest: async ({ identifier, url, provider }) => { - // const user = await db.user.findUnique({ - // where: { - // email: identifier, - // }, - // select: { - // emailVerified: true, - // }, - // }) + EmailProvider({ + from: process.env.SMTP_FROM, + sendVerificationRequest: async ({ identifier, url, provider }) => { + const user = await db.user.findUnique({ + where: { + email: identifier, + }, + select: { + emailVerified: true, + }, + }) - // const templateId = user?.emailVerified - // ? process.env.POSTMARK_SIGN_IN_TEMPLATE - // : process.env.POSTMARK_ACTIVATION_TEMPLATE - // if (!templateId) { - // throw new Error("Missing template id") - // } + const templateId = user?.emailVerified + ? process.env.POSTMARK_SIGN_IN_TEMPLATE + : process.env.POSTMARK_ACTIVATION_TEMPLATE + if (!templateId) { + throw new Error("Missing template id") + } - // const result = await postmarkClient.sendEmailWithTemplate({ - // TemplateId: parseInt(templateId), - // To: identifier, - // From: provider.from as string, - // TemplateModel: { - // action_url: url, - // product_name: siteConfig.name, - // }, - // Headers: [ - // { - // // Set this to prevent Gmail from threading emails. - // // See https://stackoverflow.com/questions/23434110/force-emails-not-to-be-grouped-into-conversations/25435722. - // Name: "X-Entity-Ref-ID", - // Value: new Date().getTime() + "", - // }, - // ], - // }) + const result = await postmarkClient.sendEmailWithTemplate({ + TemplateId: parseInt(templateId), + To: identifier, + From: provider.from as string, + TemplateModel: { + action_url: url, + product_name: siteConfig.name, + }, + Headers: [ + { + // Set this to prevent Gmail from threading emails. + // See https://stackoverflow.com/questions/23434110/force-emails-not-to-be-grouped-into-conversations/25435722. + Name: "X-Entity-Ref-ID", + Value: new Date().getTime() + "", + }, + ], + }) - // if (result.ErrorCode) { - // throw new Error(result.Message) - // } - // }, - // }), + if (result.ErrorCode) { + throw new Error(result.Message) + } + }, + }), ], - // callbacks: { - // async session({ token, session }) { - // if (token) { - // session.user.id = token.id - // session.user.name = token.name - // session.user.email = token.email - // session.user.image = token.picture - // } + callbacks: { + async session({ token, session }) { + if (token) { + session.user.id = token.id + session.user.name = token.name + session.user.email = token.email + session.user.image = token.picture + } - // return session - // }, - // async jwt({ token, user }) { - // const dbUser = await db.user.findFirst({ - // where: { - // email: token.email, - // }, - // }) + return session + }, + async jwt({ token, user }) { + const dbUser = await db.user.findFirst({ + where: { + email: token.email, + }, + }) - // if (!dbUser) { - // if (user) { - // token.id = user?.id - // } - // return token - // } + if (!dbUser) { + if (user) { + token.id = user?.id + } + return token + } - // return { - // id: dbUser.id, - // name: dbUser.name, - // email: dbUser.email, - // picture: dbUser.image, - // } - // }, - // }, + return { + id: dbUser.id, + name: dbUser.name, + email: dbUser.email, + picture: dbUser.image, + } + }, + }, }