0

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.

3
  • Where is the .getOne function ? The queries.js file only has a findOne function. In the body of that function an undefined 'username' variable can also cause troubles. Commented May 25, 2019 at 12:28
  • Sorry, I meant findOne() not getOne(), 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 after options ? Commented May 25, 2019 at 17:13
  • Ah, you mean in the findOne() function. I agree it can cause troubles, but I get the same unexpected token error when I replace the variable with a string of a username I already know. Commented May 25, 2019 at 21:05

1 Answer 1

1

The error probably comes from the promise syntax :

queries.findOne().then((user)) => {
   ...
})

It must be rewritten as :

queries.findOne().then((user) => {
   ...
})

And with the complete block below :

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);
        }
    }) // There was also a missing end parenthesis here
}));

To prevent this kind of syntax issues in the future, you can use a linter such as eslint.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! You are a gentleman and a scholar...such a ridiculous mistake, probably because it's the weekend :P I will accept the answer as it fixed the problem, and upvote for the linter comment ;) - I agree, I will start using them, will save a lot of time and headache.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.