Search code examples
dockernext.jsamazon-ec2next-authnext.js14

Next-auth redirect to localhost in my login page


Im deploying my Nextjs 14 app in AWS EC2 using AWS CLI, Docker, Github.

For authentication i use next-auth. Every time user gets access sucessfully next-auth redirects them to localhost.

.env

enter image description here

Dockerfile:

FROM node:21 as builder
WORKDIR /app
COPY package*.json ./

RUN npm install --force 


COPY . .


RUN npm run build

EXPOSE 80

CMD ["npm","run", "start"]

signIn action:

    signIn('credentials', {
          email: values.email,
          password: values.password,
          callbackUrl: `${process.env.NEXTAUTH_URL}`
        })

auth.ts:

    import NextAuth, { NextAuthConfig } from 'next-auth'
    import CredentialsProvider from 'next-auth/providers/credentials'
    
    export const authOptions: NextAuthConfig = {
      providers: [
        CredentialsProvider({
          async authorize(credentials, req) {
            if (!credentials.email || !credentials.password) return null
            const { email, password } = credentials
            console.log(credentials, 'credentials')
            try {
              const response = await fetch(
                'http://**********:6060/auth/signin',
                {
                  method: 'POST',
                  headers: {
                    'Content-Type': 'application/json'
                  },
                  body: JSON.stringify({ email, password })
                }
              )
              const data = await response.json()
              return data
            } catch (err) {
              console.log(err)
              return null
            }
          }
        })
      ],
      session: {
        strategy: 'jwt'
      },
      trustHost: true,
      secret: process.env.AUTH_SECRET,
      callbacks: {
        jwt({ token, user }) {
          if (user) {
            return { ...token, ...user }
          }
          return token
        },
        session: ({ token, session }) => {
          session.user = token.user
          session.token = token.token
          return session
        },
        authorized({ auth }: { auth: any }) {
          return !!auth?.user // this ensures there is a logged in user for -every- request
        }
      },
      pages: {
        signIn: '/sign-in' // overrides the next-auth default signin page https://authjs.dev/guides/basics/pages
      }
    }
    
    export const {
      handlers: { GET, POST },
      auth
    } = NextAuth(authOptions)

I get this error:

The NEXTAUTH_URL variable is available in the container logs but not in the browser. Please refer to the container logs for more information

enter image description here

enter image description here Thanks for helping!


Solution

  • My solution was: Install next-auth v5 beta16