leonitousconforti/tinyburg

View on GitHub
apps/trading-site/app/auth.config.ts

Summary

Maintainability
A
0 mins
Test Coverage
import { NextAuthConfig } from "next-auth";

export const authConfig = {
    pages: {
        signIn: "/login",
    },
    secret: "f1ced1ab2b2d31f4178e27a6d377fbbf",
    providers: [
        // added later in auth.ts since it requires bcrypt which is only compatible with Node.js
        // while this file is also used in non-Node.js environments
    ],
    callbacks: {
        authorized({ auth, request: { nextUrl } }) {
            let isLoggedIn = !!auth?.user;
            let isOnDashboard = nextUrl.pathname.startsWith("/protected");

            if (isOnDashboard) {
                if (isLoggedIn) return true;
                return false; // Redirect unauthenticated users to login page
            } else if (isLoggedIn) {
                return Response.redirect(new URL("/protected", nextUrl));
            }

            return true;
        },
    },
} satisfies NextAuthConfig;