From: himetani_cafe Date: Wed, 29 Jul 2015 15:36:27 +0000 (+0900) Subject: simple signup and login. Next, will modify note and worksapce api adding user info X-Git-Tag: v0.3.0p0003~18 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=6d59d6cce840804c316d591dad180682c98f1dd4;p=eos%2Fzephyr.git simple signup and login. Next, will modify note and worksapce api adding user info --- diff --git a/client/app/app.js b/client/app/app.js index b86b968..0043f0b 100644 --- a/client/app/app.js +++ b/client/app/app.js @@ -6,11 +6,15 @@ angular.module('zephyrApp', ['ui.bootstrap', 'ui.router', 'restangular','ngRoute enabled: true, requireBase: false }) - $urlRouterProvider.otherwise('/login') + //$urlRouterProvider.otherwise('/login') $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest' + + // Debug + //$urlRouterProvider.otherwise('/board/504d41f91d2f81f02561b93a0e9ccde4') + }) -.run(function($rootScope, $state, Restangular) { - $rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) { +.run(function($rootScope, $state, Restangular, user) { + $rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams, user) { $rootScope.previous = fromState.name }) @@ -25,4 +29,8 @@ angular.module('zephyrApp', ['ui.bootstrap', 'ui.router', 'restangular','ngRoute return false }) + + // Debug + user.setEmail('fumifumi@yasunaga-lab.bio.kyutech.ac.jp') + user.setID('504d41f91d2f81f02561b93a0e9ccde4') }) diff --git a/client/app/components/board/board.controller.js b/client/app/components/board/board.controller.js index c72e44a..8ec0e17 100644 --- a/client/app/components/board/board.controller.js +++ b/client/app/components/board/board.controller.js @@ -9,7 +9,10 @@ angular.module('zephyrApp') $state.go('login') } - $scope.commandDirectives = new Array() + + $scope.commandDirectives = [] + + // Add new my-command directive $scope.openCommandModal = function() { var modalInstance = $modal.open({ templateUrl: '/client/app/components/commandModal/commandModal.html', @@ -27,9 +30,13 @@ angular.module('zephyrApp') })() }) } + + // Remove my-command directive $scope.removeCommandDirective= function(index) { $scope.commandDirectives.splice(index, 1) } + + // $scope.save = function() { $modal.open({ templateUrl: '/client/app/components/saveFileModal/saveFileModal.html', @@ -43,8 +50,9 @@ angular.module('zephyrApp') $scope.commandDirectives[index].isCompleted = true }) + // restore executeion log var note = $state.params.noteID - if(note !== undefined) { + if(!note) { $scope.noteName = note var baseNoteInfo = Restangular.all('/api/noteInfo/'+note) baseNoteInfo.getList().then(function(commands) { diff --git a/client/app/components/login/login.controller.js b/client/app/components/login/login.controller.js index b7da56b..a41e296 100644 --- a/client/app/components/login/login.controller.js +++ b/client/app/components/login/login.controller.js @@ -4,9 +4,12 @@ angular.module('zephyrApp') .controller('LoginController', function ($scope, $modal, Restangular, $state, user, $timeout, $rootScope) { var baseLogin = Restangular.all('/auth/login') - // show or not alert - if($rootScope.previous === 'signup') - $scope.alert = true + // show or not 'signup success alert' + if($rootScope.isSignupped) { + $scope.alert = true + $scope.isSignupped = false + } + $scope.$watch('alert') // hide alert @@ -24,13 +27,15 @@ angular.module('zephyrApp') $scope.submit = function() { var params = { - username: $scope.username, - password: $scope.password + email: $scope.email, } + params.password = $scope.password || 'secret' + console.log(params) baseLogin.post(JSON.stringify(params)).then(function(userID) { + user.email($scope.email) user.setID(userID) $state.go('board', { userID: userID }) }) diff --git a/client/app/components/login/login.html b/client/app/components/login/login.html index 900c206..ba2895f 100644 --- a/client/app/components/login/login.html +++ b/client/app/components/login/login.html @@ -5,9 +5,15 @@

Login

- + - + +
+ +

or diff --git a/client/app/components/saveFileModal/saveFileModal.controller.js b/client/app/components/saveFileModal/saveFileModal.controller.js index 026f701..ff1b8c0 100644 --- a/client/app/components/saveFileModal/saveFileModal.controller.js +++ b/client/app/components/saveFileModal/saveFileModal.controller.js @@ -3,6 +3,7 @@ angular.module('zephyrApp') .controller('SaveFileModalController', function($scope, $modalInstance, Restangular) { var baseNoteSaving = Restangular.all('/api/noteCreation') + $scope.save = function() { var note = { name: $scope.noteName @@ -24,6 +25,7 @@ angular.module('zephyrApp') $modalInstance.close() }) } + $scope.close = function() { $modalInstance.close() } diff --git a/client/app/components/signup/signup.controller.js b/client/app/components/signup/signup.controller.js index 54dd719..117714f 100644 --- a/client/app/components/signup/signup.controller.js +++ b/client/app/components/signup/signup.controller.js @@ -1,7 +1,7 @@ 'use strict'; angular.module('zephyrApp') -.controller('SignupController', function ($scope, $modal, Restangular, $state, user, $timeout) { +.controller('SignupController', function ($scope, $modal, Restangular, $state, user, $timeout, $rootScope) { var baseLogin = Restangular.all('/auth/signup') $scope.$watch('alert') @@ -12,19 +12,28 @@ angular.module('zephyrApp') $scope.alert = true $scope.alertmsg = "At least one select your role" timeout() + } else { + var params = { + email: $scope.email, + roles: [] + } + if($scope.superuser) + params.password = $scope.password + else + params.password = "secret" + + if($scope.role.superuser) + params.roles.push('superuser') + if($scope.role.expert) + params.roles.push('expert') + if($scope.role.amature) + params.roles.push('amature') + + baseLogin.post(JSON.stringify(params)).then(function(status) { + $rootScope.isSignupped = true + $state.go('login') + }) } - var params = { - email: $scope.email - } - if($scope.superuser) - params.password = $scope.password - else - params.password = "secret" - - baseLogin.post(JSON.stringify(params)).then(function(status) { - $state.go('login') - console.log(status) - }) } // hide alert diff --git a/client/app/shared/user/user.service.js b/client/app/shared/user/user.service.js index 0e79de8..02ef761 100644 --- a/client/app/shared/user/user.service.js +++ b/client/app/shared/user/user.service.js @@ -2,13 +2,20 @@ angular.module('zephyrApp') .service('user', function() { - var userID + var userID, + email return { setID: function(id) { userID = id }, getID: function() { return userID + }, + setEmail: function(addr) { + email: addr + }, + getEmail: function() { + return email } } }) diff --git a/gulpfile.js b/gulpfile.js index bdb9a70..a8cc4fd 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -122,6 +122,10 @@ gulp.task('set-production', function() { process.env.NODE_ENV = 'production' }) +gulp.task('dbdrop', function() { + process.env.DB_ZEPHYR = 'drop' +}) + gulp.task('build', ['angular-concat', 'js-vendor-concat', 'underscore', 'js-app', 'js-source-concat', 'css-concat', 'cache'], function() { }) @@ -132,3 +136,8 @@ gulp.task('default', function(callback) { gulp.task('deploy', function(callback) { runSequence('build', 'set-production', 'serve', callback) }) + +gulp.task('test', function(callback) { + runSequence('dbdrop', 'default', callback) +}) + diff --git a/server/app.js b/server/app.js index c593f8b..cc33e17 100644 --- a/server/app.js +++ b/server/app.js @@ -3,8 +3,7 @@ var express = require('express'), config = require('./config'), app = express(), - server = require('http').createServer(app), - jb = require('./jb') + server = require('http').createServer(app) require('./express')(app); require('./routes')(app); diff --git a/server/auth/login/index.js b/server/auth/login/index.js index 73a969d..ce982ae 100644 --- a/server/auth/login/index.js +++ b/server/auth/login/index.js @@ -5,62 +5,73 @@ var express = require('express'), path = require('path'), passport = require('passport'), LocalStrategy = require('passport-local').Strategy, - crypto = require('crypto') + crypto = require('crypto'), + jb = require('../../jb') -var users = [ - { id: 1, username: 'bob', password: 'secret', email: 'bob@example.com' } - , { id: 2, username: 'joe', password: 'birthday', email: 'joe@example.com' } -]; + /* + var users = [ + { id: 1, username: 'bob', password: 'secret', email: 'bob@example.com' } + , { id: 2, username: 'joe', password: 'birthday', email: 'joe@example.com' } + ]; -function findById(id, fn) { - var idx = id - 1; - if (users[idx]) { - fn(null, users[idx]); - } else { - fn(new Error('User ' + id + ' does not exist')); - } -} + function findById(id, fn) { + var idx = id - 1; + if (users[idx]) { + fn(null, users[idx]); + } else { + fn(new Error('User ' + id + ' does not exist')); + } + } -function findByUsername(username, fn) { - for (var i = 0, len = users.length; i < len; i++) { - var user = users[i]; - if (user.username === username) { - return fn(null, user); - } - } - return fn(null, null); -} + function findByUsername(username, fn) { + for (var i = 0, len = users.length; i < len; i++) { + var user = users[i]; + if (user.username === username) { + return fn(null, user); + } + } + return fn(null, null); + } -passport.serializeUser(function(user, done) { - done(null, user.id) -}) - -passport.deserializeUser(function(id, done) { - findById(id, function(err, user) { - done(null, user.id) - }) -}) + passport.serializeUser(function(user, done) { + done(null, user.id) + }) -passport.use(new LocalStrategy( - function(username, password, done) { - process.nextTick(function() { - findByUsername(username, function(err, user) { - if(err){ return done(err) } - if(!user) { return done(null, false, { message: 'Unknown user ' + username })} - if(user.password != password) { return done(null, false, { message: 'Invalid password' }) } - return done(null, user) - }) - }) - } -)) + passport.deserializeUser(function(id, done) { + findById(id, function(err, user) { + done(null, user.id) + }) + }) + passport.use(new LocalStrategy( + function(username, password, done) { + process.nextTick(function() { + findByUsername(username, function(err, user) { + if(err){ return done(err) } + if(!user) { return done(null, false, { message: 'Unknown user ' + username })} + if(user.password != password) { return done(null, false, { message: 'Invalid password' }) } + return done(null, user) + }) + }) + } + )) + */ -router.post('/', passport.authenticate('local'), - function(req, res) { - var username = req.body.username - var md5 = crypto.createHash('md5') - md5.update(username, 'utf8') - res.send(md5.digest('hex')); - }) +router.post('/', function(req, res) { + var email= req.body.email + var password = req.body.password + jb.find('user', + { email: email, + password: password}, + function(err, document) { + if(document.length === 0) { + res.sendStatus(401) + } else { + var md5 = crypto.createHash('md5') + md5.update(email, 'utf8') + res.send(md5.digest('hex')); + } + }) +}) module.exports = router diff --git a/server/auth/signup/index.js b/server/auth/signup/index.js index ec7bd6e..b9a6cda 100644 --- a/server/auth/signup/index.js +++ b/server/auth/signup/index.js @@ -6,11 +6,11 @@ var express = require('express'), router.post('/', function(req, res) { - var email = req.params.email - var password = req.params.password + var email = req.body.email + var password = req.body.password jb.find('user', - { email: req.params.email }, - function(err, document) { + { email: email }, + function(err, document) { if(document.length === 0) { var newuser = { email: email, @@ -24,7 +24,7 @@ router.post('/', function(req, res) { } else { res.sendStatus(409) } - }) + }) }) module.exports = router diff --git a/server/express.js b/server/express.js index 3080123..300f61a 100644 --- a/server/express.js +++ b/server/express.js @@ -1,13 +1,13 @@ 'use strict'; -var express = require('express'), - path = require('path'), - config = require('./config'), - bodyParser = require('body-parser'), - flash = require('connect-flash'), - session = require('express-session'), +var express = require('express'), + path = require('path'), + config = require('./config'), + bodyParser = require('body-parser'), + flash = require('connect-flash'), + session = require('express-session'), cookieParser = require('cookie-parser'), - passport = require('passport') + passport = require('passport') module.exports = function(app) { app.set('views', config.root+ '/server/views') @@ -28,6 +28,7 @@ module.exports = function(app) { if(app.get('env') === 'development') { app.use('/client/', express.static('./client/')) app.use(express.static(path.join(config.root, 'client'))) + } else if(app.get('env') === 'production') { app.use('/dist/', express.static('./dist/')) app.use(express.static(path.join(config.root, 'dist'))) diff --git a/server/jb.js b/server/jb.js index 7bd718a..bfc238d 100644 --- a/server/jb.js +++ b/server/jb.js @@ -1,6 +1,14 @@ 'use strict' -var EJDB = require('ejdb'), +var express = require('express'), + app = express(), + EJDB = require('ejdb'), jb = EJDB.open("db/user.db") +if(process.env.DB_ZEPHYR === 'drop') { + jb.dropCollection('user', + { prune: true }, + function() {}) +} + module.exports = jb