Next.js Discord

Discord Forum

Hello, my English is bad, I hope I was able to explain, I get such an error, I could not solve it, c

Answered
Cape horse mackerel posted this in #help-forum
Open in Discord
Cape horse mackerelOP
this is my code

import NextAuth from "next-auth";
import KeycloakProvider from "next-auth/providers/keycloak";
import jwt_decode from "jwt-decode";
import { encrypt } from "@/utils/encryption";
async function refreshAccessToken(token) {
  const resp = await fetch(`${process.env.REFRESH_TOKEN_URL}`, {
    headers: { "Content-Type": "application/x-www-form-urlencoded" },
    body: new URLSearchParams({
      client_id: process.env.DEMO_FRONTEND_CLIENT_ID,
      client_secret: process.env.DEMO_FRONTEND_CLIENT_SECRET,
      grant_type: "refresh_token",
      refresh_token: token.refresh_token,
    }),
    method: "POST",
  });
  const refreshToken = await resp.json();
  if (!resp.ok) throw refreshToken;

  return {
    ...token,
    access_token: refreshToken.access_token,
    decoded: jwt_decode(refreshToken.access_token),
    id_token: refreshToken.id_token,
    expires_at: Math.floor(Date.now() / 1000) + refreshToken.expires_in,
    refresh_token: refreshToken.refresh_token,
  };
}
export const authOptions = {
  providers: [
    KeycloakProvider({
      clientId: `${process.env.DEMO_FRONTEND_CLIENT_ID}`,
      clientSecret: `${process.env.DEMO_FRONTEND_CLIENT_SECRET}`,
      issuer: `${process.env.AUTH_ISSUER}`,
    }),
  ],
`
Answered by Madeiran sardinella
Hi, I'm not sure but it looks like you shouldn't export authOptions from an API route. I'd move it to a separate file.
View full answer

5 Replies

Cape horse mackerelOP
  
callbacks: {
    async jwt({ token, account }) {
      const nowTimeStamp = Math.floor(Date.now() / 1000);
      if (account) {
        token.decoded = jwt_decode(account.access_token);
        token.access_token = account.access_token;
        token.id_token = account.id_token;
        token.expires_at = account.expires_at;
        token.refresh_token = account.refresh_token;
        return token;
      } else if (nowTimeStamp < token.expires_at) {
        return token;
      } else {
        try {
          const refreshedToken = await refreshAccessToken(token);
          return refreshedToken;
        } catch (error) {
          return { ...token, error: "RefreshAccessTokenError" };
        }
      }
    },
    async session({ session, token }) {
      // Send encrypted properties to the client.
      session.access_token = encrypt(token.access_token);
      session.id_token = encrypt(token.id_token);
      session.roles = token.decoded.realm_access.roles;
      session.error = token.error;
      return session;
    },
  },
};
const handler = NextAuth(authOptions);
export { handler as GET, handler as POST };
i get error

.next/types/app/api/auth/[...nextauth]/route.ts:8:13 Type error: Type 'OmitWithTag<typeof import("C:/Users/Sinan/Desktop/next/app/api/auth/[...nextauth]/route"), "POST" | "GET" | "HEAD" | "OPTIONS" | "PUT" | "DELETE" | "PATCH" | "config" | "generateStaticParams" | ... 6 more ... | "maxDuration", "">' does not satisfy the constraint '{ [x: string]: never; }'. Property 'authOptions' is incompatible with index signature. Type '{ providers: OAuthConfig<KeycloakProfile>[]; callbacks: { jwt({ token, account }: { token: any; account: any; }): Promise<any>; session({ session, token }: { session: any; token: any; }): Promise<...>; }; }' is not assignable to type 'never'. 6 | 7 | // Check that the entry is a valid entry > 8 | checkFields<Diff<{ | ^ 9 | GET?: Function 10 | HEAD?: Function 11 | OPTIONS?: Function  ELIFECYCLE  Command failed with exit code 1. PS C:\Users\Sinan\Desktop\next>

### Additional information

No response

### Example

No response
Madeiran sardinella
Hi, I'm not sure but it looks like you shouldn't export authOptions from an API route. I'd move it to a separate file.
Answer
Cape horse mackerelOP
Thanks, I will ask something, ts and tsx which one should I not use, is there a difference in terms of performance?
tsx if you need jsx syntax
ts otherwise