I have a SaaS in NextJS, and for some reason, it happens that when I make a request even if it has no logic other than validating the path with Next Auth or even without that, the fetch takes a few seconds to process and display the information, since I have a loader, and that loader takes, I have been reviewing the code and I have not yet found the part that affects performance.
I don't know if there is any way to optimize the routes.
app/api/auth/[...nextauth].ts
import NextAuth from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
export default NextAuth({
providers: [
CredentialsProvider({
name: "Credentials",
credentials: {
username: { label: "Username", type: "text" },
password: { label: "Password", type: "password" }
},
authorize(credentials) {
if (credentials?.username === "user" && credentials?.password === "pass") {
return { id: 1, name: "Example User" };
}
return null;
}
})
],
pages: {
signIn: "/auth/signin"
}
});
app/protected.tsx
import { getSession } from "next-auth/react";
export default function ProtectedPage() {
return <h1>This is a protected page</h1>;
};
export async function getServerSideProps(context: any) {
const session = await getSession(context);
if (!session) {
return {
redirect: {
destination: "/auth/signin",
permanent: false
}
};
}
return {
props: {}
};
}
app/auth/signin.tsx
import { signIn } from "next-auth/react";
export default function SignIn() {
return (
<div>
<h1>Sign In</h1>
<button onClick={() => signIn()}>Sign in with credentials</button>
</div>
);
};