Skip to content

Commit

Permalink
Merge cfbd482 into ad05dbc
Browse files Browse the repository at this point in the history
  • Loading branch information
andela-fopara committed Feb 28, 2017
2 parents ad05dbc + cfbd482 commit eba00cb
Show file tree
Hide file tree
Showing 50 changed files with 1,791 additions and 976 deletions.
3 changes: 3 additions & 0 deletions .bowerrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"directory": "public/lib"
}
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
node_modules
bower_components
.sass-cache/
.DS_store
c4h_keys.txt
.env
bower_components
.idea/
.coverdata
coverage
Expand All @@ -13,4 +13,6 @@ Procfile
gruntfile.js
debug
public/lib
npm-debug.log
.vscode
npm-debug.log
Procfile
38 changes: 0 additions & 38 deletions .vscode/formatter.json

This file was deleted.

69 changes: 69 additions & 0 deletions app/controllers/games.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,72 @@ exports.startGame = function (req, res) {
});
});
};

exports.findAllRecord = function (req, res) {
Games.find()
.exec((err, games) => {
if (err) {
return res.json({
success: false,
msg: 'An unexpected error occurred'
});
} else {
res.send({ gameCollection: games });
}
});
};

exports.updateGame = function (req, res) {
Games.findOne({
id: req.params.id
}).exec((err, games) => {
if (err) {
res.json({
success: false,
msg: 'An unexpected error occurred'
});
}
if (games) {
gameId = Object.keys(games).length + 1;
}
var game = {
winner: req.body.winner.username,
players: req.body.players,
numberOfRounds: req.body.numberOfRounds,
state: req.body.state
};
const query = {
$and: [{
id: game.id
}, {
creator: game.creator
}]
};
Games.update({ id: req.params.id }, game, function (err, result) {
if (err) {
res.status(500).json({
message: 'An error occured while updating this data',
error: err
});
} else {
res.status(200).json({
message: 'Game updated sucessfully'
});
}
return res;
});
});
};

exports.getGame = function (req, res) {
Games.find({ creator: req.params.email }, function (err, result) {
if (err) {
res.status(500).json({
message: 'An error occured while updating this data',
error: err
});
} else {
res.status(200).json(result);
}
});
};
1 change: 0 additions & 1 deletion app/controllers/signin.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,3 @@ exports.userAuth = function (req, res) {
}
});
};

41 changes: 38 additions & 3 deletions app/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
* Module dependencies.
*/
var mongoose = require('mongoose'),
User = mongoose.model('User');
User = mongoose.model('User'),
jwt = require('jsonwebtoken');
var avatars = require('./avatars').all();
var jwt = require('jsonwebtoken');

/**
* Auth callback
Expand Down Expand Up @@ -50,6 +52,7 @@ exports.signout = function (req, res) {
*/
exports.session = function (req, res) {
res.redirect('/#!/app');

};

/**
Expand All @@ -73,7 +76,6 @@ exports.checkAvatar = function (req, res) {
// If user doesn't even exist, redirect to /
res.redirect('/');
}

};

/**
Expand Down Expand Up @@ -206,4 +208,37 @@ exports.authenticate = function (req, res, next) {
return;
}
}
};
};

/**
* check if user is authenticated
* before showing dashboard menu
* on navbar
*/
exports.isAuthenticated = function (req, res, next) {
if (!req.body.JWT) {
res.send(false);
} else {
req.user = jwt.verify(req.body.JWT, process.env.SECRET);
if (req.user) {
res.send(true);
} else {
res.send(false);
}
}
};

exports.findAllRecord = function (req, res) {
User.find()
.exec((err, userDetails) => {
if (err) {
return res.json({
success: false,
msg: 'An unexpected error occurred'
});
} else {
res.send({ users: userDetails });
}
});
};

4 changes: 2 additions & 2 deletions app/models/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var GameSchema = new Schema({
id: {
type: Number
},
czar: {
creator: {
type: String
},
winner: {
Expand Down Expand Up @@ -48,4 +48,4 @@ GameSchema.pre('save', function (next) {
return next();
});

mongoose.model('Game', GameSchema);
mongoose.model('Game', GameSchema);
10 changes: 5 additions & 5 deletions app/views/includes/foot.jade
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ script.
(function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];
g.src='//www.google-analytics.com/ga.js';
s.parentNode.insertBefore(g,s)}(document,'script'));
script(type='text/javascript', src='/lib/jquery/dist/jquery.js')
//- script(type='text/javascript', src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js")
//- script(type='text/javascript', src='/lib/jquery/dist/jquery.js')
script(type='text/javascript', src="https://code.jquery.com/jquery-1.12.4.js")
script(type='text/javascript', src='/lib/underscore/underscore-min.js')

//Bootstrap
Expand Down Expand Up @@ -34,8 +34,7 @@ script(type="text/javascript" src="/lib/checklist-model/checklist-model.js")
//Angular UI
script(type='text/javascript', src='/lib/angular-bootstrap/ui-bootstrap-tpls.js')
script(type='text/javascript', src='/lib/angular-ui-utils/modules/route/route.js')

script(type='text/javascript', src='//cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.js')
script(type='text/javascript', src='https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.js')
script(type='text/javascript', src='https://cdnjs.cloudflare.com/ajax/libs/angular-moment/1.0.1/angular-moment.js')

//Application Init
Expand All @@ -55,6 +54,7 @@ script(type='text/javascript', src='/js/controllers/index.js')
script(type='text/javascript', src='/js/controllers/header.js')
script(type='text/javascript', src='/js/controllers/game.js')
script(type='text/javascript', src='/js/controllers/navbar.js')
script(type='text/javascript', src='/js/controllers/dashboard.js')
script(type='text/javascript', src='/js/init.js')

//IntroJS
Expand All @@ -65,4 +65,4 @@ script(type='text/javascript', src='/js/game_tour.js')
script(type='text/javascript', src='/socket.io/socket.io.js')

//Livereload script rendered
//script(type='text/javascript', src='http://localhost:35729/livereload.js')
//script(type='text/javascript', src='http://localhost:35729/livereload.js')
2 changes: 1 addition & 1 deletion app/views/includes/head.jade
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ head
link(rel='stylesheet', href='https://material.angularjs.org/1.1.3/docs.css')

link(rel='stylesheet', href='/lib/bootstrap/dist/css/bootstrap.css')

link(rel='stylesheet', href='/css/common.css')
link(rel='stylesheet', href='/css/animate.css')
link(rel='stylesheet', href='/css/design.css')
Expand All @@ -46,3 +45,4 @@ head

//if lt IE 9
script(src='https://html5shim.googlecode.com/svn/trunk/html5.js')
1 change: 1 addition & 0 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"angular-ui-utils": "0.0.4",
"jquery": "~1.9.1",
"underscore": "~1.5.2",
"angular-moment-service": "angular-momentjs-service#^0.1.0",
"checklist-model": "^0.10.0"
},
"exportsOverride": {
Expand Down
2 changes: 1 addition & 1 deletion config/middlewares/authorization.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ exports.requiresLogin = function(req, res, next) {
/**
* User authorizations routing middleware
*/
exports.user = {
exports.usezr = {
hasAuthorization: function(req, res, next) {
if (req.profile.id != req.user.id) {
return res.send(401, 'User is not authorized');
Expand Down
22 changes: 22 additions & 0 deletions config/middlewares/jwtAuthorization.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export default (req, res, next) => {
const authToken = req.get('Authorization') || '';
const userToken = authToken.split(' ')[1] || '';
const bearer = authToken.split(' ')[0];
if (bearer !== 'Bearer') {
return Response.unAuthorize(res, 'Authorization Bearer not found');
}
db.Token.findOne({
where: {
token_hash: Crypto.createHash('md5').update(userToken).digest('hex')
}
})
.then((token) => {
if (!token) {
throw new Error('You are not Authorize to access this route');
}
req.user = jwt.verify(userToken, 'enahomurphy');
req.token = userToken;
next();
})
.catch(err => Response.unAuthorize(res, err.message));
};
9 changes: 8 additions & 1 deletion config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ module.exports = function(app, passport, auth) {

// game playing routes
app.post('/api/games/:id/start', users.authenticate, games.startGame);
app.post("/api/games", users.authenticate, games.findAllRecord);
app.post("/api/users/getAllUserDetails", users.authenticate, users.findAllRecord);
app.post('/api/users/jwt/authenticated', users.isAuthenticated);

// game history
app.put('/api/games/:id/end', games.updateGame);
app.get('/api/games/history/:email', games.getGame);

app.post('/api/search/users', invite.invite);
app.get('/api/userEmail', invite.getEmail);
Expand Down Expand Up @@ -110,4 +117,4 @@ module.exports = function(app, passport, auth) {
app.get('/play', index.play);
app.get('/', index.render);
app.get('/gametour', index.gameTour);
};
};
Loading

0 comments on commit eba00cb

Please sign in to comment.