Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add forgotten files #32

Merged
merged 1 commit into from
Jun 30, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var path = require('path');
var logger = require('winston');
var mongoose = require('mongoose');
var passport = require('passport');
var localPassport = require('./lib/localpassport');
var flash = require('connect-flash');
var favicon = require('serve-favicon');
var cookieParser = require('cookie-parser');
Expand All @@ -14,14 +15,14 @@ var cloudServiceProviders = require('./routes/cloudserviceproviders');
var cloudServices = require('./routes/cloudservices');
var cloudServiceProfiles = require('./routes/cloudserviceprofiles');
var jwt = require('jwt-simple');
var login = require('./routes/login');

var app = express();

app.engine('html', require('ejs').renderFile);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'html');


// app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
Expand Down Expand Up @@ -49,25 +50,19 @@ app.get('/api', function (req, res) {
app.use('/api/countries', countries);
app.use('/api/providers', cloudServiceProviders);
app.use('/api/cloudservices', cloudServices);
app.use('/api/cloudserviceprofiles', cloudServiceProfiles);

// Configure passport
require('./auth/passport')(passport);
require('./auth/routes.js')(app, passport);


/// error handlers

app.use('/api/cloudserviceprofiles', cloudServiceProfiles);
app.use('/api/login', login);

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}

// production error handler
Expand All @@ -79,6 +74,11 @@ app.use(function(err, req, res, next) {
error: {}
});
});

app.use(function(err, req, res, next) {
console.error(err.stack);
next(err);
});

app.listen(conf.nodejs_port, function() {
console.log('Express server listening on port %d in %s mode',
Expand Down
44 changes: 0 additions & 44 deletions backend/auth/passport.js

This file was deleted.

43 changes: 0 additions & 43 deletions backend/auth/routes.js

This file was deleted.

36 changes: 20 additions & 16 deletions backend/db/models/User.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var bcrypt = require('bcrypt-nodejs');

module.exports = function(mongoose) {
var bcrypt = require('bcrypt');
var SALT_WORK_FACTOR = 10;

var UserSchema = new mongoose.Schema({
Expand All @@ -14,28 +15,31 @@ module.exports = function(mongoose) {


// Bcrypt middleware on UserSchema
User.pre('save', function(next) {
var user = this;
UserSchema.pre('save', function(next) {
var user = this;

if (!user.isModified('password')) return next();
if (!user.isModified('password')) next();

bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) {
if (err) return next(err);
bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) {
if (err) next(err);

bcrypt.hash(user.password, salt, function(err, hash) {
if (err) return next(err);
user.password = hash;
next();
});
bcrypt.hash(user.password, salt, function(err, hash) {
if (err) {
next(err);
} else {
user.password = hash;
next();
}
});
});
});

//Password verification
User.methods.comparePassword = function(pass, cb) {
bcrypt.compare(pass, this.password, function(err, isMatch) {
if (err) return cb(err);
cb(null, isMatch);
});
UserSchema.methods.comparePassword = function(pass, cb) {
bcrypt.compare(pass, this.password, function(err, isMatch) {
if (err) return cb(err);
return(cb(null, isMatch));
});
};

var User = mongoose.model('User', UserSchema);
Expand Down
File renamed without changes.
26 changes: 26 additions & 0 deletions backend/lib/localpassport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var mongoose = require('mongoose');
var User = require('../db/models/User')(mongoose);

passport.use(new LocalStrategy(
function(username, password, done) {

User.findOne({ username : username }, function(err, user) {
if (err)
return done(err);

// if no user is found, return the message
if (!user)
return done(null, false, { message: 'No user found.' });

// if the user is found but the password is wrong
if (!user.validPassword(password))
return done(null, false, { message: 'Wrong password.'});

// all is well, return successful user
return done(null, user);
});

}
));
2 changes: 0 additions & 2 deletions backend/routes/cloudservices.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ router.get('/', function (req, res) {
});

router.post('/', function (req, res){
console.log("POST: ");
console.log(req.body);
var cloudService = new CloudService({
name: req.body.name,
description: req.body.description
Expand Down
31 changes: 31 additions & 0 deletions backend/routes/login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var express = require('express');
var router = express.Router();
var moment = require('moment');
var passport = require('passport');
var jwt = require('jwt-simple');

router.post('/', function(req, res, next) {
console.log("*** HERE");
passport.authenticate('local', function(err, user, info) {
console.log("*** AND HERE");
if (err) {
return(next(err));
}
if (!user) {
return(res.json(info));
}
var expires = moment().add('days', 7).valueOf();
var token = jwt.encode({
iss: user.id,
exp: expires
}, app.get('jwtTokenSecret'));

return(res.json({
token : token,
expires: expires,
user: user.toJSON()
}));
})(req, res, next);
});

module.exports = router;
14 changes: 7 additions & 7 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@


<div id="geant-header-wrapper">
<div id="geant-header" class="container">
<div id="geant-logo">
<a href="/">
<img alt="Back to Home" src="/images/geant_logo.png">
</a>
</div>
<div class="title">GÉANT Cloud Marketplace</div>
<div id="geant-header" class="container">
<div id="geant-logo">
<a href="/">
<img alt="Back to Home" src="/images/geant_logo.png">
</a>
</div>
<div class="title">GÉANT Cloud Marketplace</div>
</div>
</div>

<div class="container">
Expand Down
22 changes: 18 additions & 4 deletions public/login/login-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,24 @@
var agoraAppLoginController =
angular.module('agoraAppLoginController', []);

agoraAppLoginController.controller('LoginCtrl', ['$scope', 'Login',
function ($scope, Login) {
$scope.submit = function() {
Login.login({email: $scope.email, password: $scope.password});
agoraAppLoginController.controller('LoginCtrl', ['$scope', 'Login', '$location',
function ($scope, Login, $location) {
$scope.login = function(form) {
Login.login({username: $scope.username, password: $scope.password},
function(err) {
$scope.error = {};
$scope.errors = {};

if (!err) {
$location.path('/');
} else {
angular.forEach(err.errors, function(error, field) {
form[field].$setValidity('mongoose', false);
$scope.errors[field] = error.type;
});
$scope.error.other = err.message;
}
});
};
}]);

4 changes: 2 additions & 2 deletions public/login/login-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ var agoraAppLoginService =
.value('version', '0.1.0');

agoraAppLoginService.factory('Login', ['$resource',
function($resource, email, password) {
function($resource, username, password) {
return $resource('api/login', {}, {
login: {method: 'POST',
params: {
email: email, password: password
username: username, password: password
}
}});
}]);
Expand Down
27 changes: 27 additions & 0 deletions public/login/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<div class="container-fluid" ng-controller="LoginCtrl">
<div class="row">
<div class="col-sm-6 col-md-4 col-md-offset-4">
<h1 class="text-center login-title">
Please Sign In
</h1>
<img class="profile-img" src="https://lh5.googleusercontent.com/-b0-k99FZlyE/AAAAAAAAAAI/AAAAAAAAAAA/eu7opA4byxI/photo.jpg?sz=120" alt="">

<form class="form-signin" role="form" ng-submit="login()">
<input type="text" class="form-control"
ng-model="username"
placeholder="Username" required autofocus>
<input type="password" class="form-control"
ng-model="password"
placeholder="Password" required>
<label class="checkbox">
<input type="checkbox" value="remember-me"> Remember me
</label>

<div class="form-group has-error">
<p class="help-block">{{ error.other }}</p>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
</div>
</div>
</div>