Login using existing api and controller in admin bro

1.6k Views Asked by At

My project(nodejs backend) requires basic CRUD functionalities for few items: continent, country etc.

I was working on creating an admin dashboard and I found admin-bro plugin for creating the dashboard. But since this will be in live server I need to create authentication as well.

I already created the authentication api and has a good directory structure.directory structure

As shown in the picture I already have all the apis I need in the routes.js file

const express             = require('express');
const router              = express.Router();
const authController      = require('../api/v1/controllers/auth.controller');
const counrtyController   = require('../api/v1/controllers/country.controller')
const adminController     = require('../api/v1/controllers/admin.controller');

// rendering admin pages
router.get('/Admin/Login', authController.renderLoginView);
router.get('/admin/Dashboard', adminController.renderDashboardView);

// API End Points for admin auth and new admin register and signout
router.post('/api/v1/auth/login', authController.login);
router.post('/api/v1/auth/register', authController.register);
router.post('/api/v1/auth/signout', authController.signout);

// API endpoints for countries(get all, get one, add one, delete one, edit one)
router.post('/api/v1/countries/addOne', counrtyController.addOne);
router.get('/api/v1/countries/getAll', counrtyController.listAll);
router.get('/api/v1/countries/:country_name', counrtyController.listOne);
router.put('/api/v1/countries/:country_name', counrtyController.updateOne);
router.delete('/api/v1/countries/:country_name',counrtyController.deleteOne);
module.exports            = router;

This are some of the api's that I have inside the routes.js file

Each api calls a specific controller which has all the logic written inside it.

Now my problem is I want to use my existing authentication to get in the dashboard. What should I change?

I'll be keeping the apis cause in future I'll take the admin dashboard to front-end and I need the get methods to show information about continent/country.

I might comment out all the post/put/delete methods for now

What should I do next?

This is what I have inside admin.route.js

const AdminBro          = require('admin-bro');
const AdminBroExpress   = require('@admin-bro/express');
const AdminBroMongoose  = require('@admin-bro/mongoose');

const mongoose          = require('mongoose');

AdminBro.registerAdapter(AdminBroMongoose);

const Country           = require('../models/country.model');
const Continent         = require('../models/continent.model');
const Visa              = require('../models/visa.model');


const adminBro = new AdminBro({
    resources: [
        {
            resource: Continent,
            options: {
                parent: {
                    name: 'Continent'
                }
            }
        },
        {
            resource: Country,
            options: {
                parent: {
                    name: 'Country'
                }
            }
        },
    ],
    rootPath: '/admin',
})

const adminBroRouter = AdminBroExpress.buildRouter(adminBro)

module.exports = adminBroRouter;

And inside my index.js:

const express           = require('express');
const app               = express();
const mongoose          = require('mongoose');
const dotenv            = require('dotenv');
const bodyParser        = require('body-parser');
const engine            = require('./config/view.engine');
const router            = require('./routes/routes');
const session           = require('express-session');
const adminRouter       = require('./routes/admin.route');

// Middlewares
app.use(session({secret: 'mysupersecret', saveUninitialized: true, resave: true}));
app.use(bodyParser.urlencoded({extended: true}))
app.use(express.json());
app.use('/', router);


// connect to db
dotenv.config();
mongoose.connect(
    process.env.DB_CONNECT,
    {
        useUnifiedTopology: true,
        useNewUrlParser: true
    },
    () => console.log("connected to db")
);

// use engine for views
engine(app);

app.use('/admin', adminRouter);
app.get("/", (req, res) =>{
    res.send("This route wont return anything")
});

const PORT            = process.env.PORT || 5000
app.listen(PORT, () => console.log("server up and running"));

Few minor details: My login page should be /Admin/Login and dashboard should be /Admin/Dashboard

1

There are 1 best solutions below

0
On BEST ANSWER

This helped. I had to write custom auth controller and routes and not use the buildAuthenticatedRouter().

custom login page for firebase

This worked for me with a little customization with mongodb atlas