Portfolio Project E-commerce (REST API only): How to test if passport is working

Hello everyone,

I hope all is well. I am currently working on the E-commerce project that only deals with the REST API and no frontend. Here is the link to the project: Link here

I’m currently on the part where I have to use passport for user verification. I’ve already implemented passport.js in my code, as seen here:

const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const { pool } = require('./db/index');
const bcrypt = require('bcrypt');

passport.use(new LocalStrategy(
    function(email,password,done){
        pool.query('SELECT * FROM customers WHERE email =$1',
            [email],async(error,user)=>{
                if(error)
                    return done(error);
                if(!email)
                    return done(null,false);
                
                const matchedPasswords = await bcrypt.compare(password,user.password);
                if(!matchedPasswords)
                    return done(null,false);
                
                return done(null,user);
            }
        )}
    )
);

passport.serializeUser((user,done)=>{
    done(null,user.id);
});
passport.deserializeUser((id,done)=>{
    pool.query(`SELECT * FROM customers WHERE id = $1`,[id],(error,results)=>{
        if(error)
            throw error;
        else
            return done(null,results.rows[0]); //or maybe return done(null,user);
    })
})

Here is my app.js:

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const cors = require('cors');
const passport = require("passport");
const session = require("express-session");
const port = 3000;

const db = require('./db/index');
//const loginRouter = require('./routes/login');

// Import Passport config
require("./passportConfig2");
// Add middleware for handling CORS requests from index.html
app.use(cors());

// Add middware for parsing request bodies here:
app.use(bodyParser.json());

// Mount your existing apiRouter below at the '/api' path.
const apiRouter = require('./api');
app.use('/api',apiRouter);

//different CRUD operation routes
app.get('/customers', db.getCustomers)
app.get('/customers/:id', db.getCustomerById)
app.post('/customers/register', db.createCustomer)
app.put('/customers/:id', db.updateCustomer)
app.delete('/customers/:id', db.deleteCustomer)

app.get('/billing_address', db.getAddresses)
app.get('/billing_address/:id', db.getAddressById)
app.post('/billing_address', db.createAddress)
app.put('/billing_address/:id', db.updateAddress)
app.delete('/billing_address/:id', db.deleteAddress)

app.get('/credit_cards', db.getCreditCards)
app.get('/credit_cards/:id',db.getCreditCardsById)
app.post('/credit_cards', db.createCreditCard)
app.put('/credit_cards/:id', db.updateCreditCard)
app.delete('/credit_cards/:id',db.deleteCreditCard)

app.get('/items', db.getItems)
app.get('/items/:id', db.getItemById)
app.post('/items', db.createItem)
app.put('/items/:id',db.updateItem)
app.delete('/items/:id',db.deleteItem)

app.get('/orders',db.getOrders)
app.get('/orders/:id',db.getOrderById)
app.post('/orders',db.createOrder)
app.put('/orders/:id',db.updateOrder)
app.delete('/orders/:id',db.deleteOrder)

app.get('/shopping_cart', db.getShopCart)
app.get('/shopping_cart/:id', db.getShopCartById)
app.post('/shopping_cart',db.createShopCart)
app.put('/shopping_cart/:id',db.updateShopCart)
app.delete('/shopping_cart/:id',db.deleteShopCart)

//app.post('/customers/register',db.registerCustomer)

//app.post('/customers/login',passport.initialize)

// Routes from login.js
app.use(require("./routes/login"));


// Session Config
app.use(session({
  secret:"qwerofi345",
  cookie:{maxAge:3000000000,secure:false},
  saveUninitialized: false,
  resave:false,
  sameSite: 'none',
  secure: true
}));

//passport config
app.use(passport.initialize());
app.use(passport.session());

app.get('/', (req, res) => {
  res.send('Andre 3000!')
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

And here is my login.js:

const express = require('express');
const loginRouter = express.Router();
const bodyParser = require('body-parser');
const passport = require('passport');
// const flash = require('express-flash');
//const initializePassport = require('../passportConfig');

//initializePassport(passport);

loginRouter.use(bodyParser.json());
//loginRouter.use(passport.initialize());
//loginRouter.use(passport.session());
// loginRouter.use(flash());

loginRouter.post('/login',passport.authenticate("local",{
    successRedirect:"/customers",
    failureRedirect:"/login"
    // failureFlash: true
    }),
    (req,res)=>{
        res.redirect("customers");
    }
);

loginRouter.get('/logout',(res,req)=>{
    req.logout();
    res.redirect('/login');
})

module.exports = loginRouter;

Now my question is how can I test if passport is working without any frontend? Would I have to use Postman as a way to test Passport? I’m not sure how to check that Passport is working without the client side. Or would I have to build the client side in order to test Passport? Any ideas? Any help is appreciated. Take care and happy coding :slight_smile: