Next.js Discord

Discord Forum

NextAuth with Postgres

Unanswered
Oriental posted this in #help-forum
Open in Discord
OrientalOP
I'm fairly new to NextJS and AuthJS. I want to make an account system with login/register. I want to use Postgres as DB for this.

Is my assumption correct that I can simply install a Postgres database on my server and then connect it to the software via .env.

Could someone explain how I implement the fundamental things in this config file. The logic with the DB should be at //Implement your login logic here

// app/api/auth/[...nextauth]/route.js
import NextAuth from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";

export const authOptions = {
  providers: [
    CredentialsProvider({
      name: "Credentials",
      credentials: {
        username: { label: "Username", type: "text" },
        password: { label: "Password", type: "password" }
      },
      async authorize(credentials) {
        // Implement your login logic here
        const user = { id: 1, name: "John Doe", email: "john@example.com" };
        
        // If the login details are correct
        if (credentials.username === "admin" && credentials.password === "password") {
          return user;
        } else {
          // If the login details are incorrect
          return null;
        }
      }
    })
  ],
  pages: {
    signIn: '/auth/login',
  },
  session: {
    jwt: true,
  },
  callbacks: {
    async jwt(token, user) {
      if (user) {
        token.id = user.id;
      }
      return token;
    },
    async session(session, token) {
      session.user.id = token.id;
      return session;
    }
  }
};

const handler = NextAuth(authOptions);

export { handler as GET, handler as POST };

3 Replies

OrientalOP
Or why would I use NextAuth at all? Is it needed?
Swedish Lapphund
NextAuth is legit trash, I would recommend looking for alternatives. Read more about it here: https://www.reddit.com/r/nextjs/comments/18olw72/nextauth_is_hell/