EggsLeggs
EggsLeggs
TTCTheo's Typesafe Cult
Created by EggsLeggs on 7/29/2023 in #questions
Hosting platform for docker images (discord bot specifically)
Ok so I used to have a mega chonky vps but im swapping away from that now. The primary use was a rather resource hungry Discord bot and it needs a new home. so for its web dashboard, its swapping to vercel BarbaraPoint for the postgres database, well i was gunna use neon but that looked quite pricey so ive swapped to planet scale and mysql. I use prisma so it was v easy to swap now the problem is the discord bot. its dockerized and automatically updates with watchtower rn so id prefer to keep the ability to handsfree deploy when i merge to branches. what tools if any have u guys seen for this? ps- its not something that can easily run serverless otherwise id swap to aws
45 replies
TTCTheo's Typesafe Cult
Created by EggsLeggs on 7/5/2023 in #questions
How to properly setup next auth for a discord dashboard
Context: - I am setting up a dashboard for a discord bot and I'm using next auth to setup discord oauth - I use prisma as my database adaptor and next 13 app directory as my framework - I am v new to next and next auth so this might be common sense What I'm stuck on: - At the moment I've got it working but I feel like I've done it in a way that has pitfalls - It currently uses their email to check if the jwt is valid but what if a discord user changes their email, does this just break everything and make a new user and account - Should I use the uuid instead of email I changed the jwt account check to use id not email - should the id be discord's id instead of the generated one in the user table, each user has a row in the account table rn with their discord id instead - Finally (less important as I can probs eventually figure out), how do I make an api endpoint in next13 which takes the user who requested its token, gets their discord auth token and then requests all of their guilds I'd be truly grateful for any help as Its quite hard to wrap my head around still. The code:
import { PrismaAdapter } from "@next-auth/prisma-adapter"
import { NextAuthOptions } from "next-auth"
import DiscordProvider from "next-auth/providers/discord"

import { env } from "@/env.mjs"
import { db } from "@/lib/db"

const scopes = ["identify", "guilds", "email"].join(" ")

export const authOptions: NextAuthOptions = {
adapter: PrismaAdapter(db as any),
session: {
strategy: "jwt",
},
pages: {
signIn: "/login",
},
providers: [
DiscordProvider({
clientId: env.DISCORD_CLIENT_ID,
clientSecret: env.DISCORD_CLIENT_SECRET,
authorization: { params: { scope: scopes } },
}),
],
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,
},
})

if (!dbUser) {
if (user) {
token.id = user?.id
}
return token
}

return {
id: dbUser.id,
name: dbUser.name,
email: dbUser.email,
picture: dbUser.image,
}
},
},
}
import { PrismaAdapter } from "@next-auth/prisma-adapter"
import { NextAuthOptions } from "next-auth"
import DiscordProvider from "next-auth/providers/discord"

import { env } from "@/env.mjs"
import { db } from "@/lib/db"

const scopes = ["identify", "guilds", "email"].join(" ")

export const authOptions: NextAuthOptions = {
adapter: PrismaAdapter(db as any),
session: {
strategy: "jwt",
},
pages: {
signIn: "/login",
},
providers: [
DiscordProvider({
clientId: env.DISCORD_CLIENT_ID,
clientSecret: env.DISCORD_CLIENT_SECRET,
authorization: { params: { scope: scopes } },
}),
],
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,
},
})

if (!dbUser) {
if (user) {
token.id = user?.id
}
return token
}

return {
id: dbUser.id,
name: dbUser.name,
email: dbUser.email,
picture: dbUser.image,
}
},
},
}
4 replies