Search code examples
node.jsexpresspassport.js

Express route controller works inline but not exported - PassportJS


I'm coding a login post route using passport JS. It works fine, but I'm having issues getting my exported version working.

Works

router.post('/users/login', passport.authenticate('local'), (req, res) => {
    console.log(req.user);
});

Doesn't work - Returns Cannot POST /users/login

exports.loginAuth = passport.authenticate('local'), (req, res) => {
    console.log(req.user);
}

Solution

  • OKi, one thing. on yow loginAuth where are you telling the route ? Well the problem is bcuz you of passport.authenticate('local'), that is a middleware

    try this. let's say we have the routes.js and the controller.js

    on the rotes.js as the name says we have them routes, so it looks like this

    import {Router} from "express";
    //const {Router} = require("express");
    import {theEndPoint} from "./controller";
     
    export default function themRoutes () {
     const api = Router()
    
    // also dont forget to add the require them thingies required by passport and stuff
    api.post("/api/login", [passport.authenticate('local')],theEndPoint);
    
    
    return api;
    }
    
    

    and on our controller.js file we have some like

    export function theEndPoint (req, res) {
    
    ....
    
    res.sendStatus(202);
    }