0

I'm currently working on a Node.js application where I'm using AdminBro as the admin panel. I have encountered an authentication issue specifically when calling the AdminBro API to access data.

Here's a simplified version of my code:

const express = require('express');
const cors = require('cors');
const app = express();

// Enable CORS for all routes
app.use(cors());

// ...

// AdminBro
const AdminBro = require('admin-bro');
const expressAdminBro = require('@admin-bro/express');
const mongooseAdminBro = require('@admin-bro/mongoose');

// Models
const Product = require('./models/Product');
const Company = require('./models/Company');

AdminBro.registerAdapter(mongooseAdminBro);
const AdminBroOptions = { resources: [Product, Company] };

const adminBro = new AdminBro(AdminBroOptions);

const DEFAULT_ADMIN = {
  email: 'admin@example.com',
  password: 'password',
};

const authenticate = async (email, password) => {
  if (email === DEFAULT_ADMIN.email && password === DEFAULT_ADMIN.password) {
    return Promise.resolve(DEFAULT_ADMIN);
  }
  return null;
};

const router = expressAdminBro.buildAuthenticatedRouter(adminBro, {
  authenticate,
  cookieName: 'adminbro',
  cookiePassword: 'sessionsecret',
});

app.use(adminBro.options.rootPath, router);

// ...

const port = process.env.PORT || 3000;
const url = process.env.APP_URL || 'http://localhost:3000/admin';

app.listen(port, () => {
  console.log(`Server UP! at ${url}`);
});

The problem I'm facing is that whenever I try to call the AdminBro API endpoint, such as "/api/resources/[RESOURCE-ID]/actions/list", it requires authentication. However, I'm unable to successfully authenticate and as a result, I can't access the data I need.

I have already implemented the authentication logic using the authenticate function, comparing the provided email and password with the default admin credentials. However, it doesn't seem to be working as expected.

I would greatly appreciate any help or guidance on how to resolve this authentication issue specifically when calling the AdminBro API in my Node.js application.

Thank you in advance for your assistance!

0 Answers0