I am trying to use a passport-local strategy and a knex connection to postgresql for user authentication on a nodejs app. Below is the dependencies to the app file as well as the LocalStrategy instance:
index.js
var express = require('express'),
app = express(),
server = require('http').createServer(app),
session = require('express-session'),
passport = require('passport')
bcrypt = require('bcryptjs')
LocalStrategy = require('passport-local').Strategy;
db = require('./db');
queries = require('./queries');
app.use(passport.initialize());
app.use(passport.session());
options = {};
passport.use(new LocalStrategy(options, (username, password, done) =>{
queries.findOne().then((user)) => {
console.log(user)
if (!user) return done(null, false);
if (!autHelpers.comparePass(password, user.password)) {
return done(null, false);
} else {
return done(null, user);
}
}
}
));
In the queries file, I have placed the findOne() function to find users
queries.js
const knex = require ('./db');
module.exports = {
findOne() {
return knex('users').where("username", username).first()
}
}
However, running the app gives me the following error in the console:
.getOne()
SyntaxError: Unexpected token .
at createScript (vm. js:80:10)
...
It seems that it doesn't accept the queries.findOne() statement for some reason. I tried the passport.use without the statements inside the => {} and it doesn't lead to an error in the app.
Should I be writing the queries.findOne() in a different manner? I also thought of wrapping this function inside app.use() but I feel that is not logical.
It seems I have done a typo somewhere, but everything I try leads to a dead-end and I can't seem to figure this simple one out.
findOne()notgetOne(), I corrected accordingly in the post. Also for the username, how should I define it? You mean in one of the conditions, or instead do you mean as a parameter afteroptions?unexpected tokenerror when I replace the variable with a string of a username I already know.