I am trying to deploy a nuxt application in a server. The server uses nginx and acts as proxy to localhost. Everything seems fine, I can already access the appliaction with the domain of the server and I can make api calls to the server. However, the authentication does not work. I am using sidebase nuxt auth and when I am trying to login an user I get the error shown in the image.
Error in the browser’s console after an user tries to authenticate
To run the server I use pm2 and this is the ecosystem.config.cjs file:
module.exports = {
apps: [
{
name: 'eurocore-coopetition',
port: '3000',
exec_mode: 'cluster',
instances: '1',
script: './.output/server/index.mjs'
}
]
}
And this is my nuxt.config.ts file:
// https://nuxt.com/docs/api/configuration/nuxt-config
import vuetify, { transformAssetUrls } from 'vite-plugin-vuetify'
export default defineNuxtConfig({
devtools: { enabled: true },
build: {
transpile: ['vuetify'],
},
modules: [
(_options, nuxt) => {
nuxt.hooks.hook('vite:extendConfig', (config) => {
// @ts-expect-error
config.plugins.push(vuetify({ autoImport: true }))
})
},
'@sidebase/nuxt-auth'
//...
],
vite: {
vue: {
template: {
transformAssetUrls,
},
},
},
css: [
'/assets/custom-theme.scss',
],
auth: {
baseURL: process.env.NUXT_AUTH_ORIGIN,
globalAppMiddleware: true
},
runtimeConfig: {
authSecret: process.env.NUXT_AUTH_SECRET,
registrationCode: process.env.NUXT_REGISTRATION_CODE,
},
})
The auth origin is set to http://localhost:3000/
.
And this is my […].ts file located in /server/api/auth
:
import bcrypt from "bcrypt";
import { NuxtAuthHandler } from "#auth"
import { PrismaClient } from "@prisma/client"
import { PrismaAdapter } from "@next-auth/prisma-adapter"
import CredentialsProvider from "next-auth/providers/credentials"
const config = useRuntimeConfig();
const prisma = new PrismaClient()
declare module "next-auth"{
interface User {
role: string
}
}
export default NuxtAuthHandler({
pages: {
signIn: "/login",
},
session: {
strategy: "jwt"
},
callbacks: {
jwt: async ({token, user}) => {
const isSignIn = user ? true : false;
if (isSignIn){
token.id = user.id;
token.name = user.name;
token.email = user.email;
token.role = user.role;
//token.jwt = user ? (user as any).acess_token || '' : '';
//token.id = user ? user.id || '' : '';
//token.role = user ? (user as any).role || '' : '';
}
return token;
},
session: async ({session, token}) => {
(session as any).uid = token.id;
(session as any).role = token.role;
return session;
}
},
secret: config.authSecret,
adapter: PrismaAdapter(prisma),
providers: [
// @ts-expect-error
CredentialsProvider.default({
name: "Credentials",
async authorize(credentials: {email: string, password: string}) {
const user = await prisma.users.findUnique({where: { email: credentials.email }})
let valid;
if (user) {
valid = await bcrypt.compare(credentials.password, user.password);
}
if(!user || !valid){
throw createError({
statusCode: 403,
statusMessage: "Credentials not working",
})
}
return user
},
}),
],
})
I have tried to change the baseURL to the domain of the server but that did not work and it did not load any page. In development mode everything works if I test the deployment in my computer using pm2 it also works. So I don’t know what it’s the problem.
Any help is appreciated!