I have established the local strategy within the config/passport.js file. Here is the snippet of code for the strategy:
passport.use('local', new localStrategy({
userNameField: 'email',
passwordFiled: 'password',
passReqToCallback: true
}, function(req, email, password, done){
console.log("\n\n\n\nInside passport.js")
User.findOne({'email':email}, function(err, user){
if(err){
return done(null, false);
}
if(user){
return done(null, false, {message:"Email is already in use"});
}
var newUser = new User();
newUser.email = email;
newUser.password = newUser.encryptPassword(password);
newUser.save(function(err, result){
if(err){
return done(err);
}
return done(null, newUser);
});
});
}));
The register route in routes/users.js utilizes this strategy. Here is the route:
router.get('/register', function(req, res, next){
res.render('user/register',{});
});
router.post('/register', passport.authenticate('local', {
successRedirect: '/',
failureRedirect: '/register',
failureFlash: true
}));
Below is the HTML view for the registration page:
<div class="row">
<div class="col-md-4 col-md-offset-4">
<h1>Sign Up</h1>
<br><br><br>
<form action="/users/register" method="post">
<div class="form-group">
<label for="EmailAddress">Email Address</label>
<input class="form-control" type="text" aria-describedby="emailHelp" name="email" placeholder="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b5d0cdd4d8c5d9d0f5d2d8d4dcd99bd6dad8">[email protected]</a>">
<small id="emailHelp" class="form-text text-muted">We will never share your email with anyone else</small>
</div>
<div class="form-group">
<label for="Password">Password</label>
<input class="form-control" type="password" aria-describedby="passwordHelp" name="password" placeholder="{{{hello}}}">
<small id="passwordHelp" class="form-text text-muted"></small>
</div>
<button type="submit" class="btn btn-primary"> Sign Up </button>
</form>
</div>
</div>
When accessing the route localhost:3000/users/register, the following error occurs:
Error: Strategy#authenticate must be overridden by subclass
at Strategy.authenticate (C:\Users\Muhammad Hassam\Documents\Visual Studio 2017\Projects\Shopping Cart\Shopping Cart\node_modules\passport-strategy\lib\strategy.js:21:9)
at attempt (C:\Users\Muhammad Hassam\Documents\Visual Studio 2017\Projects\Shopping Cart\Shopping Cart\node_modules\passport\lib\middleware\authenticate.js:361:16)
at authenticate (C:\Users\Muhammad Hassam\Documents\Visual Studio 2017\Projects\Shopping Cart\Shopping Cart\node_modules\passport\lib\middleware\authenticate.js:362:7)
at Layer.handle [as handle_request] (C:\Users\Muhammad Hassam\Documents\Visual Studio 2017\Projects\Shopping Cart\Shopping Cart\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Muhammad Hassam\Documents\Visual Studio 2017\Projects\Shopping Cart\Shopping Cart\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\Muhammad Hassam\Documents\Visual Studio 2017\Projects\Shopping Cart\Shopping Cart\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\Muhammad Hassam\Documents\Visual Studio 2017\Projects\Shopping Cart\Shopping Cart\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\Muhammad Hassam\Documents\Visual Studio 2017\Projects\Shopping Cart\Shopping Cart\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\Users\Muhammad Hassam\Documents\Visual Studio 2017\Projects\Shopping Cart\Shopping Cart\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\Muhammad Hassam\Documents\Visual Studio 2017\Projects\Shopping Cart\Shopping Cart\node_modules\express\lib\router\index.js:275:10)
at Function.handle (C:\Users\Muhammad Hassam\Documents\Visual Studio 2017\Projects\Shopping Cart\Shopping Cart\node_modules\express\lib\router\index.js:174:3)
at router (C:\Users\Muhammad Hassam\Documents\Visual Studio 2017\Projects\Shopping Cart\Shopping Cart\node_modules\express\lib\router\index.js:47:12)
at Layer.handle [as handle_request] (C:\Users\Muhammad Hassam\Documents\Visual Studio 2017\Projects\Shopping Cart\Shopping Cart\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\Muhammad Hassam\Documents\Visual Studio 2017\Projects\Shopping Cart\Shopping Cart\node_modules\express\lib\router\index.js:317:13)
at C:\Users\Muhammad Hassam\Documents\Visual Studio 2017\Projects\Shopping Cart\Shopping Cart\node_modules\express\lib\router\index.js:284:7
at Function.process_params (C:\Users\Muhammad Hassam\Documents\Visual Studio 2017\Projects\Shopping Cart\Shopping Cart\node_modules\express\lib\router\index.js:335:12)
I have searched for a solution but could not find any relevant information regarding this specific error.