Skip to content

Instantly share code, notes, and snippets.

@Slyracoon23
Created September 28, 2023 08:26
Show Gist options
  • Save Slyracoon23/e371efdeadb136422ffb1fe3946b2ec5 to your computer and use it in GitHub Desktop.
Save Slyracoon23/e371efdeadb136422ffb1fe3946b2ec5 to your computer and use it in GitHub Desktop.
Next-auth auth handler in Nextjs13
import type { NextApiRequest, NextApiResponse } from "next"
import NextAuth from "next-auth"
import GitHubProvider from "next-auth/providers/github";
import DiscordProvider from "next-auth/providers/discord"
import { env } from "@/env.mjs";
import { syncDiscord } from "@/app/discord-action"
import { authOptions } from "@/lib/next-auth"
// const handler = NextAuth(authOptions)
// export { handler as GET, handler as POST }
async function auth(req: Request, res: Response) {
// Do whatever you want here, before the request is passed down to `NextAuth`
//@ts-ignore
const { nextUrl: { search } } = req;
const urlSearchParams = new URLSearchParams(search);
console.log('Url Params,', urlSearchParams)
// @ts-ignore
return await NextAuth(req, res, {
session: {
strategy: "jwt",
},
pages: {
signIn: "/login2",
},
providers: [
GitHubProvider({
clientId: env.GITHUB_CLIENT_ID,
clientSecret: env.GITHUB_CLIENT_SECRET,
}),
DiscordProvider({
clientId: env.DISCORD_CLIENT_ID,
clientSecret: env.DISCORD_CLIENT_SECRET
}),
// Removed EmailProvider here
],
callbacks: {
async signIn({ user, account, profile, email, credentials }) {
// Your custom post-login actions go here
// For example, if you want to sync the user to Discord after they've successfully logged in:
if (account?.provider === 'github') {
try {
// Call your function to sync with Discord
// await syncDiscord({ guild_id: "1234" });
console.log('sync User')
// Return true to successfully complete the sign-in process
return true;
} catch (error) {
// Handle the error, for example, log it or send to a monitoring service
console.error("Failed to sync with Discord:", error);
// Optionally, return false to prevent the sign-in from proceeding
return false;
}
}
if (account?.provider === 'discord') {
try {
console.log(user, account, profile, email, credentials)
// Call your function to sync with Discord
// await syncDiscord({ guild_id: "1234" });
console.log('sync User Discord')
console.log('request:', req)
console.log('res:', res)
syncDiscord({ guild_id: (account as any)?.guild?.id, org_id: "2" })
// Return true to successfully complete the sign-in process
return true;
} catch (error) {
// Handle the error, for example, log it or send to a monitoring service
console.error("Failed to sync with Discord:", error);
// Optionally, return false to prevent the sign-in from proceeding
return false;
}
}
return true; // If the provider isn't GitHub or there's no specific action to do, proceed with the login
},
async session({ token, session }) {
if (token) {
if (!session.user) {
session.user = {};
}
// 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 }) {
// Removed Prisma logic here
// For now, I'm just returning the token as it is, but you can modify as required.
return token;
},
},
}
)
}
export { auth as GET, auth as POST }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment