Will next-auth fetch the account information from API every-time I page refreshes or changes?

Or maybe useSession or getServerSession call? If it does how can I prevent this, because I am using discord provider and even with a 1/2 users using the website its getting 429 Ratelimited very often. Code of my auth.ts
const fetchGuilds = async (token: string, old_guilds?: Array<Guild>) => {
const response = await fetch("https://discord.com/api/users/@me/guilds", {
headers: {
Authorization: `Bearer ${token}`,
},
})

if (!response.ok) {
if (response.status == 429) {
console.log("Got Rate limited") // I do get this message printed. Many times
return old_guilds ?? []
}

throw new Error(`${response.status}: ${response.statusText}`)
}

const guilds = (await response.json()) as Array<Guild>
return guilds
}

export const authOptions: NextAuthOptions = {
callbacks: {
session: ({ session, token }) => {
session.user = token.user
session.guilds = token.guilds

return session
},
jwt: async ({ token, user, account }) => {
if (account && user) {
token.guilds = await fetchGuilds(account.access_token as string, [])
token.user = user
return token
}

return token
},
},
providers: [
DiscordProvider({
clientId: env.DISCORD_CLIENT_ID,
clientSecret: env.DISCORD_CLIENT_SECRET,
authorization: {
params: {
scope: "identify guilds email",
state: crypto.randomBytes(16).toString("hex"),
prompt: "none",
},
},
checks: "state",
}),
],
pages: {
signIn: "/",
error: "/",
signOut: "/",
},
secret: process.env.JWT_SECRET,
}
const fetchGuilds = async (token: string, old_guilds?: Array<Guild>) => {
const response = await fetch("https://discord.com/api/users/@me/guilds", {
headers: {
Authorization: `Bearer ${token}`,
},
})

if (!response.ok) {
if (response.status == 429) {
console.log("Got Rate limited") // I do get this message printed. Many times
return old_guilds ?? []
}

throw new Error(`${response.status}: ${response.statusText}`)
}

const guilds = (await response.json()) as Array<Guild>
return guilds
}

export const authOptions: NextAuthOptions = {
callbacks: {
session: ({ session, token }) => {
session.user = token.user
session.guilds = token.guilds

return session
},
jwt: async ({ token, user, account }) => {
if (account && user) {
token.guilds = await fetchGuilds(account.access_token as string, [])
token.user = user
return token
}

return token
},
},
providers: [
DiscordProvider({
clientId: env.DISCORD_CLIENT_ID,
clientSecret: env.DISCORD_CLIENT_SECRET,
authorization: {
params: {
scope: "identify guilds email",
state: crypto.randomBytes(16).toString("hex"),
prompt: "none",
},
},
checks: "state",
}),
],
pages: {
signIn: "/",
error: "/",
signOut: "/",
},
secret: process.env.JWT_SECRET,
}
10 Replies
Neto
Neto2y ago
This callback is called whenever a JSON Web Token is created (i.e. at sign in) or updated (i.e whenever a session is accessed in the client). The returned value will be encrypted, and it is stored in a cookie.
Neto
Neto2y ago
Callbacks | NextAuth.js
Callbacks are asynchronous functions you can use to control what happens when an action is performed.
Neto
Neto2y ago
from the docs and every time you access something you fetch the guilds from discord
Shahriyar
Shahriyar2y ago
from what I knew,
if (account && user) {

}
if (account && user) {

}
Makes it only execute on the first sign in
Neto
Neto2y ago
the main thing is "whenever a session is accessed in the client"
Shahriyar
Shahriyar2y ago
On next jwt call account and user is not present so the code inside it will never get called untill log out and log in so it will return the old token So where do I fetch the guilds
Neto
Neto2y ago
on client you can use react query to fetch and keep state just remember to disable the updates on the query otherwise there will be a lots of issues related to that
Neto
Neto2y ago
Discord Developer Portal
Discord Developer Portal — API Docs for Bots and Developers
Integrate your service with Discord — whether it's a bot or a game or whatever your wildest imagination can come up with.
Neto
Neto2y ago
you can check in the response headers the limit but that kind of info shouldn't be requested that soon
Shahriyar
Shahriyar2y ago
Can I use the module lru-cache on the fetchGuilds method to do it?
Want results from more Discord servers?
Add your server