0
votes

I am a newb here, so go easy on me. I have the following server.js:

var express = require('express');
var app = express();
var path = require('path');

var port = process.env.PORT || 3001;
var cookieParser = require("cookie-parser");
var session = require('express-session');
var morgan = require("morgan");
var mongoose = require("mongoose");
var bodyParser = require("body-parser");
var passport = require("passport");
var flash = require("connect-flash");

app.use('/bower_components',  express.static(__dirname + '/bower_components'));
app.use('./config/auth',  express.static(__dirname + './config/auth'));
app.use('/app',  express.static(__dirname + '/app'));
app.set('views', __dirname + '/dist/views');
app.set('view engine', 'jade');

require('./config/passport')(passport);
app.use(morgan('dev'));
app.use(cookieParser());
app.use(bodyParser.urlencoded({extended:false}));
app.use(session({secret: 'anystringoftext',
                saveUninitialized: true,
                resave: true}));

app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.use(express.static(__dirname + '/dist')); 

//app.use(express.static(path.join(__dirname, '/*')));
require('./app/routes.js')(app, passport);

app.listen(port);
console.log('server running on port:'+port);

I am trying to get passport to work with google authentication:

var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
var User = require('../app/models/user');
var configAuth = require('./config/auth');
module.exports = function(passport){
    passport.serializeUser(function(user, done){
        done(null, user.id);
    });
    passport.deserializeUser(function(id, done){
        User.findById(id, function(err, user){
            done(err, user);
        })
    });
    passport.use(new GoogleStrategy({
        clientID: configAuth.googleAuth.clientID,
        clientSecret: configAuth.googleAuth.clientSecret,
        callbackURL: configAuth.googleAuth.callbackURL
    }, function(accessToken, refreshToken, profile, done) {
        process.nextTick(function(){
            User.findOne({'google.id': profile.id}, function(err, user){
                if(err)
                    return done(err);
                if(user)
                    return done(null, user);
                else {
                    var newUser = new User();
                    newUser.google.id = profile.id;
                    newUser.google.token = accessToken;
                    newUser.google.name = profile.displayName;
                    newUser.google.email = profile.emails[0].value;

                    newUser.save(function(err){
                        if(err)
                            throw err;
                        return done(null, newUser);
                    })
                    console.log(profile);
                }
            });
        });
    }

   ));
}

and I am getting the following errors:

Error: Cannot find module './config/auth' at Function.Module._resolveFilename (module.js:325:15) at Function.Module._load (module.js:276:25) at Module.require (module.js:353:17) at require (internal/module.js:12:17) at Object. (/Users/toddcoulson/Documents/PTO/config/passport.js:3:18) at Module._compile (module.js:409:26) at Object.Module._extensions..js (module.js:416:10) at Module.load (module.js:343:32) at Function.Module._load (module.js:300:12) at Module.require (module.js:353:17) at require (internal/module.js:12:17) at Object. (/Users/toddcoulson/Documents/PTO/server.js:20:1) at Module._compile (module.js:409:26) at Object.Module._extensions..js (module.js:416:10) at Module.load (module.js:343:32) at Function.Module._load (module.js:300:12)

I have tried calling ./config/auth, /config/auth, /config, many different combinations, but it doesn't seem to be getting the config auth file read in. Any ideas on what I am missing? I have been following code from here: https://github.com/BrentAureli/Youtube-Tutorials/tree/master/NodeJS%20Tutorials/Tutorial%2011%20-%20Google%20OAuth but I don't think that is exactly my situation because I added browser-sync in with express, and got that working. So I am not sure if browser-sync has anything to do with this, but I thought I would add that. Thanks in advance for any help. In terms of folder structure all of my static files are in /dist, and on the same level as server.js I have a folder app and a folder config.

1

1 Answers

0
votes

It looks like your passport.js file, from which you're calling require('./config/auth'); is already in the config folder. So on that third line of passport.js, it should just be var configAuth = require('./auth'); since the file path is relative.