How to Use Passport Strategies - Test 1 does not pass

Tell us what’s happening:
I can’t pass Failed: All steps should be correctly implemented in server.js.
Your project link(s)
I’m not using replit, but i’ll share my code.

'use strict';
require('dotenv').config();
const express = require('express');
const myDB = require('./connection');
const session = require('express-session');
const passport = require('passport');
const { ObjectID } = require('mongodb');
const LocalStrategy = require('passport-local');
const fccTesting = require('./freeCodeCamp/fcctesting.js');

const app = express();

fccTesting(app); //For FCC testing purposes
app.set('view engine', 'pug');
app.set('views', './views/pug');
app.use(session({
    secret: process.env.SESSION_SECRET,
    resave: true,
    saveUninitialized: true,
    cookie: { secure: false }
}));
app.use(passport.initialize());
app.use(passport.session());
app.use('/public', express.static(process.cwd() + '/public'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

myDB(async client => {
    const db = await client.db('dbtest').collection('users');

    app.get('/', (req, res) => {
        res.render('index', {
            'title': 'Home page',
            'message': 'Please login',
            'showLogin': true
        });
    });

    app.post('/login', passport.authenticate('local', {failureRedirect: '/'}), (req, res) => {
        res.redirect('/profile');
    });
    
    app.get('/profile', (req, res) => {
        res.render('profile');
    });

    passport.serializeUser((user, done) => {
        done(null, user._id);
    })

    passport.deserializeUser((id, done) => {
        db.findOne({_id: new ObjectID(id)}, (err, doc) => {
            done(null, doc);
        });
    });

    passport.use(new LocalStrategy((username, password, done) => {
        db.findOne({username: username}, (err, user) => {
            console.log(`User ${username} has attempted to log in.`);
            if (err) return done(err);
            if (!user) return done(null, false);
            if (password !== user.password) return done(null, false);
            return done(null, user);
        });
    }));

}).catch(err => {
    app.get('/', (req, res) => {
        res.render('index', {'title': err, 'message': 'Unable to connect to database'});
    })
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
    console.log('Listening on port ' + PORT);
});

Your browser information:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/109.0
Challenge:
Advanced Node and Express - How to Use Passport Strategies

I think I’ve done everything right so far, or am I missing something?

1 Like

Try changing it to use the route method. It looks like there is a regex that is expecting it (it is looking for the HTTP method after the path).

tests

assert.match(
  data,
  /login[^]*post[^]*local/,
  'You should have a route for login which accepts a POST and passport.authenticates local'
);
app.post('/login', passport.authenticate('local', {failureRedirect: '/'})

vs

app.route('/login').post(passport.authenticate('local', { failureRedirect: '/' })
1 Like

I used the route method on all my endpoints now and it passed!

Thanks man!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.