Next.js Discord

Discord Forum

NextAuth discord provider

Answered
Siamese posted this in #help-forum
Open in Discord
SiameseOP
This is my jwt callback. I wanted to refetch the user guilds each time they open the site (and of course cache it so there's no API spam) and when the discord access token expires, force a relogin. What should I change? Beacuse the jwt callback runs a lot which will cause ratelimits and I can't figure out how to force relogin
async jwt({ token, account, profile }) {
      try {
        const response = await fetch('https://discord.com/api/users/@me/guilds', {
          headers: {
            Authorization: `Bearer ${token.accessToken}`,
          },
        });
        const guilds = await response?.json();
        token.guilds = guilds.filter((guild: { owner: boolean, permissions: number }) =>
          guild.owner ||
          (guild.permissions & 0x20) === 0x20 || // ADMINISTRATOR permission
          (guild.permissions & 0x08) === 0x08 // MANAGE_GUILD permission
        ).map((guild: any) => {
          return {
            ...guild,
            icon: guild.icon ? `https://cdn.discordapp.com/icons/${guild.id}/${guild.icon}.webp` : "https://cdn.discordapp.com/embed/avatars/0.png",
          }
        });
      } catch {
        token.guilds = [];
      }
      if (account) {
        if (profile) token = {
          ...token,
          id: profile.id,
          avatar: profile.image_url,
          accessToken: account.access_token,
        }
      }
      return token;
    },
Answered by Toyger
This is my jwt callback. I wanted to refetch the user guilds each time they open the site (and of course cache it so there's no API spam)
you can use react-query for this, it by default cache data for 5 minutes, you can change it on whatever is ok for you.

when the discord access token expires, force a relogin
discord? if you are talking about next-auth token then you need to set it timeout to minimal time that you user should be forced to logout if didn't use site for some amount of time
View full answer

25 Replies

SiameseOP
Bump
SiameseOP
bump
SiameseOP
.
SiameseOP
Anyone?
Toyger
This is my jwt callback. I wanted to refetch the user guilds each time they open the site (and of course cache it so there's no API spam)
you can use react-query for this, it by default cache data for 5 minutes, you can change it on whatever is ok for you.

when the discord access token expires, force a relogin
discord? if you are talking about next-auth token then you need to set it timeout to minimal time that you user should be forced to logout if didn't use site for some amount of time
Answer
Toyger
SiameseOP
Or is react-query the best choice
Toyger
you can use swr too https://swr.vercel.app/docs/revalidation they have revalidation as cache timeout
SiameseOP
Which would you recommend?
Toyger
I just used to react-query, but looks like swr is basically same
SiameseOP
hey @Toyger i need something server side tho
SiameseOP
swr looks like its client side, same with react query
SiameseOP
i want a cache that wont auto refetch the data. I want so it will be fetched only when requested and cache used when its there
Toyger
and this is how it works by default, it will not refetch anything by itself without new request and stale data.
SiameseOP
should i add revalidate
Toyger
depends on where you'll call it, if it's inside page you can have just
export const revalidate = 3600 // revalidate at most every hour

if it's some nested component then revalidate in fetch options

for unstable_cache you need to revalidate by yourself, so you can make some cookies where you'll set time of last data, and if it more than n time that you want then revalidate it
SiameseOP
hmm ok
SiameseOP
got it to work, used some cache from nextjs baked into nodejs native fetch
next: { revalidate: 900 }, // Cache for 15 minutes