diff --git a/app.js b/app.js index 8854d0c..bf78b45 100644 --- a/app.js +++ b/app.js @@ -30,7 +30,7 @@ app.use(session({ saveUninitialized: true, //添加这行 secret: 'ILoveYiJia', // 建议使用 128 个字符的随机字符串 cookie: { - maxAge: 60 * 1000 + maxAge: 3600 * 24 * 30 } })); diff --git a/package.json b/package.json index e0e5384..7b89412 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "dependencies": { "body-parser": "~1.15.1", "cookie-parser": "~1.4.3", + "crypto": "0.0.3", "debug": "~2.2.0", "ejs": "~2.4.1", "express": "~4.13.4", diff --git a/public/index.html b/public/index.html index 8bfa59a..6940e5c 100644 --- a/public/index.html +++ b/public/index.html @@ -1,43 +1,47 @@ - - - - - - 我的书签 - - - - - - -
- - -
-
-
-
-
- - + + + + + + + 我的书签 + + + + + + + +
+ + +
+
+
- - - - - - - - - - - - - - - - - +
+ + +
+ + + + + + + + + + + + + + + + + + + diff --git a/public/scripts/app-angular.js b/public/scripts/app-angular.js index 2b4570a..50054eb 100644 --- a/public/scripts/app-angular.js +++ b/public/scripts/app-angular.js @@ -48,3 +48,7 @@ app.config(function($stateProvider, $urlRouterProvider) { controller: 'homeCtr' }); }); + +app.config(function ($httpProvider) { + $httpProvider.interceptors.push('TokenInterceptor'); +}); diff --git a/public/scripts/controllers/bookmarks-controller.js b/public/scripts/controllers/bookmarks-controller.js index 38011c5..e2edf95 100644 --- a/public/scripts/controllers/bookmarks-controller.js +++ b/public/scripts/controllers/bookmarks-controller.js @@ -1,19 +1,19 @@ -app.controller('bookmarksCtr', ['$scope', '$stateParams', '$filter', '$window', 'bookmarkService', 'pubSubService', function($scope, $stateParams, $filter, $window, bookmarkService, pubSubService) { +app.controller('bookmarksCtr', ['$scope', '$state', '$stateParams', '$filter', '$window', 'bookmarkService', 'pubSubService', function($scope, $state, $stateParams, $filter, $window, bookmarkService, pubSubService) { console.log("Hello bookmarksCtr...", $stateParams); $scope.bookmarks = []; // 书签数据 $scope.showSearch = false; // 书签数据 + $scope.hoverItem = true; $scope.showStyle = 'navigate'; // 显示风格'navigate', 'card', 'table' semanticInit(); var params = { show: $scope.showStyle, } - getBookmarks(params); + $scope.jumpToUrl = function(url) { - console.log(url); $window.open(url, '_blank'); } - + getBookmarks(params); pubSubService.subscribe('MenuCtr.bookmarks', $scope, function(event, params) { console.log('subscribe MenuCtr.bookmarks', params); getBookmarks(params); @@ -33,9 +33,16 @@ app.controller('bookmarksCtr', ['$scope', '$stateParams', '$filter', '$window', bookmarkService.getBookmarks(params).then( function(data) { $scope.bookmarks = data; + pubSubService.publish('loginCtr.login', { + 'login': true, + }); }, - function(errorMsg) { - console.log(errorMsg); + function(data) { + console.log(data); + $state.go('/'); + pubSubService.publish('loginCtr.login', { + 'login': false, + }); } ); } diff --git a/public/scripts/controllers/edit-controller.js b/public/scripts/controllers/edit-controller.js index cb6ed76..2f92568 100644 --- a/public/scripts/controllers/edit-controller.js +++ b/public/scripts/controllers/edit-controller.js @@ -1,11 +1,10 @@ app.controller('editCtr', ['$scope', '$state', '$timeout', 'bookmarkService', 'pubSubService', function($scope, $state, $timeout, bookmarkService, pubSubService) { var maxSelections = 3; - console.log("Hello , I enter editCtr..."); + console.log("Hello , editCtr..."); init(); semanticInit(); $scope.$watch('url', function(newValue, oldValue, scope) { - console.log('url is changed', $('.ui.modal.js-add-bookmark').modal('is active')); $timeout(function() { $scope.urlError = $scope.url == '' && $('.ui.modal.js-add-bookmark').modal('is active'); }); diff --git a/public/scripts/controllers/home-controller.js b/public/scripts/controllers/home-controller.js index 45b84c4..d611bd1 100644 --- a/public/scripts/controllers/home-controller.js +++ b/public/scripts/controllers/home-controller.js @@ -1,12 +1,18 @@ -app.controller('homeCtr', ['$scope', '$stateParams', '$filter', '$window', 'bookmarkService', 'pubSubService', function($scope, $stateParams, $filter, $window, bookmarkService, pubSubService) { +app.controller('homeCtr', ['$scope', '$stateParams', '$filter', '$state', '$window', 'bookmarkService', 'pubSubService', function($scope, $stateParams, $filter, $state, $window, bookmarkService, pubSubService) { console.log('Hello homeCtr......'); - var params = { - userName: 'luchenqun', - pwd: '123456', - }; - bookmarkService.autoLogin(params).then( + bookmarkService.autoLogin().then( function(data) { console.log(data); + if (data.logined) { + pubSubService.publish('loginCtr.login', { + 'login': data.logined, + }); + $state.go('bookmarks', { + showStyle: 'navigate', + }) + } else { + console.log('login failed......................') + } }, function(errorMsg) { console.log(errorMsg); diff --git a/public/scripts/controllers/login-controller.js b/public/scripts/controllers/login-controller.js index c40d9d2..6a4faf9 100644 --- a/public/scripts/controllers/login-controller.js +++ b/public/scripts/controllers/login-controller.js @@ -1,47 +1,43 @@ app.controller('loginCtr', ['$scope', '$filter', '$state', 'bookmarkService', 'pubSubService', function($scope, $filter, $state, bookmarkService, pubSubService) { console.log("Hello loginCtr..."); - $scope.userName = ""; - $scope.pwd = ""; + $scope.username = "luchenqun"; + $scope.password = "fendoubuxi"; $scope.showErr = false; $scope.errInfo = ''; $scope.login = function() { - var atuoLogin = $('.ui.checkbox.js-auto-login').checkbox('is checked'); - if (!$scope.userName || !$scope.pwd) { + var autoLogin = $('.ui.checkbox.js-auto-login').checkbox('is checked'); + if (!$scope.username || !$scope.password) { $scope.showErr = true; $scope.errInfo = '用户明或者密码不能为空!'; } else { $scope.showErr = false; $scope.errInfo = ''; - console.log($scope.userName, $scope.pwd, atuoLogin); + console.log($scope.username, $scope.password, autoLogin); + var params = { + username: $scope.username, + password:$scope.password, + autoLogin:autoLogin, + }; + bookmarkService.login(params).then( + function(data) { + console.log(data); + if (data.logined) { + pubSubService.publish('loginCtr.login', { + 'login': data.logined, + }); + $state.go('bookmarks', { + showStyle: 'navigate', + }) + } else { + console.log('login failed......................') + } + }, + function(errorMsg) { + console.log(errorMsg); + } + ); } } - - // login({ - // userName: 'luchenqun', - // pwd: '123456', - // }); - // - // function login(params) { - // bookmarkService.login(params).then( - // function(data) { - // console.log(data); - // if (data.logined) { - // pubSubService.publish('loginCtr.login', { - // 'login': data.logined, - // }); - // $state.go('bookmarks', { - // showStyle: 'navigate', - // }) - // } else { - // console.log('login failed......................') - // } - // }, - // function(errorMsg) { - // console.log(errorMsg); - // } - // ); - // } - }]); diff --git a/public/scripts/services/bookmark-service.js b/public/scripts/services/bookmark-service.js index 7e697f2..5d1491b 100644 --- a/public/scripts/services/bookmark-service.js +++ b/public/scripts/services/bookmark-service.js @@ -29,11 +29,9 @@ app.factory('bookmarkService', ['$http', '$q', function($http, $q) { }); return def.promise; }, - autoLogin: function(params) { + autoLogin: function() { var def = $q.defer(); - $http.get('/api/autoLogin/', { - params: params - }) + $http.get('/api/autoLogin/') .success(function(data) { def.resolve(data); }) @@ -57,8 +55,8 @@ app.factory('bookmarkService', ['$http', '$q', function($http, $q) { .success(function(data) { def.resolve(data); }) - .error(function(data) { - console.log('Error: ' + data); + .error(function(data, status) { + console.log('Error: ' + data, status); def.reject('Failed to get todos'); }); return def.promise; @@ -120,3 +118,48 @@ app.factory('bookmarkService', ['$http', '$q', function($http, $q) { return service; }]); + +app.factory('AuthenticationService', function() { + var auth = { + isAuthenticated: false, + isAdmin: false + } + + return auth; +}); + +app.factory('TokenInterceptor', function ($q, $window, $location, AuthenticationService) { + return { + request: function (config) { + config.headers = config.headers || {}; + if ($window.sessionStorage.token) { + config.headers.Authorization = 'Bearer ' + $window.sessionStorage.token; + } + return config; + }, + + requestError: function(rejection) { + return $q.reject(rejection); + }, + + /* Set Authentication.isAuthenticated to true if 200 received */ + response: function (response) { + if (response != null && response.status == 200 && $window.sessionStorage.token && !AuthenticationService.isAuthenticated) { + AuthenticationService.isAuthenticated = true; + } + return response || $q.when(response); + }, + + /* Revoke client authentication if 401 is received */ + responseError: function(rejection) { + if (rejection != null && rejection.status === 401 && ($window.sessionStorage.token || AuthenticationService.isAuthenticated)) { + delete $window.sessionStorage.token; + AuthenticationService.isAuthenticated = false; + // $location.path("/admin/login"); + console.log('responseError') + } + + return $q.reject(rejection); + } + }; +}); diff --git a/public/scripts/services/data-service.js b/public/scripts/services/data-service.js new file mode 100644 index 0000000..def88b5 --- /dev/null +++ b/public/scripts/services/data-service.js @@ -0,0 +1,13 @@ +app.factory('dataService', [function() { + var service = { + loginParams: function() { + return { + username:'', + password:'', + autoLogin:true, + }; + }, + }; + + return service; +}]); diff --git a/public/views/bookmarks.html b/public/views/bookmarks.html index 35e9ea6..53664a4 100644 --- a/public/views/bookmarks.html +++ b/public/views/bookmarks.html @@ -20,10 +20,11 @@
- {{ tag.name }} + {{ tag.name }} + {{ hoverItem }}
-
+
{{ bookmark.title}}
diff --git a/public/views/login.html b/public/views/login.html index b5e6cc0..ab9c726 100644 --- a/public/views/login.html +++ b/public/views/login.html @@ -7,14 +7,14 @@
- +
- +
diff --git a/routes/api.js b/routes/api.js index 9687160..1bbb584 100644 --- a/routes/api.js +++ b/routes/api.js @@ -1,77 +1,87 @@ var api = require('express').Router(); var mysql = require('mysql'); -// var client = mysql.createConnection({ -// host: '172.24.13.5', -// user: 'root', -// password: 'root123', -// database: 'mybookmarks', -// multipleStatements: true, -// port: 3306 -// }); +var crypto = require('crypto'); var client = mysql.createConnection({ - host: '127.0.0.1', - user: 'lcq', - password: '123456', + host: '172.24.13.5', + user: 'root', + password: 'root123', database: 'mybookmarks', multipleStatements: true, port: 3306 }); +// var client = mysql.createConnection({ +// host: '127.0.0.1', +// user: 'lcq', +// password: '123456', +// database: 'mybookmarks', +// multipleStatements: true, +// port: 3306 +// }); client.connect(); api.post('/logout', function(req, res) { var params = req.body.params; console.log('logout......', params); - var userName = params.userName; - req.session.destroy(function(err) { - - }); + req.session.destroy(); res.json({ data: "logout success", }); - }); api.post('/login', function(req, res) { var params = req.body.params; - console.log('login......', params); - var userName = params.userName; - var pwd = params.pwd; - var logined = Math.random() > 0.5; - if (logined) { - res.cookie('isLogin', userName, { - maxAge: 60000 - }); - req.session.userName = userName; - console.log(req.session.userName); - } else { + var username = params.username; + var password = md5(params.password); + console.log('login......', params, password); - } - res.json({ - logined: logined, - }); + var sql = "SELECT * FROM `users` WHERE `username` = '"+ username +"'"; + client.query(sql, function(error, result, fields) { + var id = ''; + var logined = false; + console.log(password, result[0].password) + if (!error && result.length === 1 && password === result[0].password) { + req.session.username = username; + logined = true; + id = result[0].id; + } + res.json({ + logined: logined, + userId:id + }); + }) }); api.get('/autoLogin', function(req, res) { - console.log('autoLogin......', req.body.params); - // if (req.cookies.isLogin) { - // console.log('cookies:' + req.cookies.isLogin); - // req.session.userName = req.cookies.isLogin; - // } - - if (req.session.userName) { - console.log('session:' + req.session.userName); - res.json({ - data: "you have auto login", - }); + if (req.session.username) { + console.log('session:' + req.session.username); + var sql = "SELECT * FROM `users` WHERE `username` = '"+ req.session.username +"'"; + client.query(sql, function(error, result, fields) { + var id = ''; + var logined = false; + if (!error && result.length === 1) { + req.session.username = result[0].username; + req.session.id = result[0].id; + logined = true; + id = result[0].id; + } + res.json({ + logined: logined, + userId:id, + }); + }) } else { res.json({ - data: "Please Login", + logined: false, + userId:'', }); } }); api.get('/bookmarks', function(req, res) { console.log('hello bookmarks', JSON.stringify(req.query)); + if (!req.session.username) { + res.send(401); + } if (req.query.show === 'navigate') { var sql = "SELECT t.id as tag_id, t.name as tag_name, b.* FROM `tags` as t LEFT OUTER JOIN tags_bookmarks as tb ON t.id = tb.tag_id LEFT OUTER JOIN bookmarks as b ON tb.bookmark_id = b.id ORDER BY t.id ASC, b.click_count DESC"; client.query(sql, function(error, result, fields) { @@ -296,4 +306,11 @@ api.post('/addTags', function(req, res) { }); // client.end(); +function md5(str) { + return crypto + .createHash('md5') + .update(str) + .digest('hex'); +}; + module.exports = api;