Nightlife app

There are three main parts for the  Nightlife App :

The user can search a bar near his place. This user can be authenticated or unauthenticated.

But the user must be authenticated in order to save the places that he wants to his profile.

So the most important part for me on this application was the authentication.

In order to make the authentication proccess and to save the data we used Express JS and PassportJs middleware (for the routing and authentication) and MongoDB(for storing the data).

Let's see how PassportJs works:

Passport is an authenticated middleware using OAuth provider like Facebook or Google plus. In this application we are using Google Plus.

Usually we add the logic of passport to a specific directory usually we are calling it config.js.

A part of this code is explained with comments to understand further how it works.


passport.use(new GoogleStrategy({

clientID : configAuth.googleAuth.clientID,
clientSecret : configAuth.googleAuth.clientSecret,
callbackURL : configAuth.googleAuth.callbackURL,

},
function(token, refreshToken, profile, done) {

// make the code asynchronous
// User.findOne won't fire until we have all our data back from Google
process.nextTick(function() {

// try to find the user based on their google id
User.findOne({ 'google.id' : profile.id }, function(err, user) {
if (err)
return done(err);

if (user) {

// if a user is found, log them in
return done(null, user);
} else {
// if the user isnt in our database, create a new user
var newUser = new User();

// set all of the relevant information
newUser.google.id = profile.id;
newUser.google.token = token;
newUser.google.name = profile.displayName;
newUser.google.email = profile.emails[0].value; // pull the first email

// save the user
newUser.save(function(err) {
if (err)
throw err;
return done(null, newUser);
});
}
});
});


As you saw above there is a comment about User.findOne.

This refered to our Mongo User Model where is structured like this:


var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');

// define the schema for our user model
var userSchema = mongoose.Schema({

local : {
email : String,
password : String,
},
google : {
id : String,
token : String,
email : String,
name : String,

},
name: { type: String }

});

// methods ======================
// generating a hash
userSchema.methods.generateHash = function(password) {
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
};

// checking if password is valid
userSchema.methods.validPassword = function(password) {
return bcrypt.compareSync(password, this.local.password);
};

// create the model for users and expose it to our app
module.exports = mongoose.model('User', userSchema);

So in our route file (route.js) we can add our authenticate routes in order to redirects the user according to his authenticated condition:

app.get('/auth/google', passport.authenticate('google', { scope: ['profile', 'email'] }));

// the callback after google has authenticated the user
app.get('/auth/google/callback',
passport.authenticate('google', {
successRedirect: '/profile',
failureRedirect: '/fail'
}));
};

// route middleware to make sure a user is logged in
function isLoggedIn(req, res, next) {
// if user is authenticated in the session, carry on
if (req.isAuthenticated())
return next();
// if they aren't redirect them to the home page
res.redirect('/');
}


In view we can add this:

<a href="/auth/google" class="btn btn-danger"><span class="fa fa-google-plus"></span> Google</a>[/code]
and the user will start the authentication process.

You can take a look to the source code for further details: https://github.com/Ierofantis/Nightlife-Coordination-App

Σχόλια