增加了登陆逻辑

This commit is contained in:
luchenqun 2016-11-02 17:53:17 +08:00
parent f1fe08948c
commit 698ca52666
13 changed files with 230 additions and 139 deletions

2
app.js
View File

@ -30,7 +30,7 @@ app.use(session({
saveUninitialized: true, //添加这行
secret: 'ILoveYiJia', // 建议使用 128 个字符的随机字符串
cookie: {
maxAge: 60 * 1000
maxAge: 3600 * 24 * 30
}
}));

View File

@ -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",

View File

@ -1,17 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="bookmark">
<meta name="author" content="luchenqun">
<title>我的书签</title>
<link href="/css/externe/semantic.min.css " rel="stylesheet"/>
<link href="/css/externe/calendar.min.css" rel="stylesheet"/>
<link href="/css/style.css " rel="stylesheet"/>
<link href="/css/externe/semantic.min.css " rel="stylesheet" />
<link href="/css/externe/calendar.min.css" rel="stylesheet" />
<link href="/css/style.css " rel="stylesheet" />
<base href="/">
</head>
<body ng-app="bookmarkApp">
</head>
<body ng-app="bookmarkApp">
<div class="ui container ">
<!-- directive菜单 -->
<menus></menus>
@ -28,6 +30,7 @@
<script src="/scripts/externe/angular-ui-router.min.js"></script>
<script src="/scripts/app-angular.js"></script>
<script src="/scripts/services/bookmark-service.js"></script>
<script src="/scripts/services/data-service.js"></script>
<script src="/scripts/services/pub-sub-service.js"></script>
<script src="/scripts/directives/edit-directive.js"></script>
<script src="/scripts/directives/menus-directive.js"></script>
@ -39,5 +42,6 @@
<script src="/scripts/controllers/edit-controller.js"></script>
<script src="/scripts/externe/semantic.min.js"></script>
<script src="/scripts/externe/calendar.min.js"></script>
</body>
</body>
</html>

View File

@ -48,3 +48,7 @@ app.config(function($stateProvider, $urlRouterProvider) {
controller: 'homeCtr'
});
});
app.config(function ($httpProvider) {
$httpProvider.interceptors.push('TokenInterceptor');
});

View File

@ -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,
});
}
);
}

View File

@ -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');
});

View File

@ -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);

View File

@ -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);
// }
// );
// }
}]);

View File

@ -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);
}
};
});

View File

@ -0,0 +1,13 @@
app.factory('dataService', [function() {
var service = {
loginParams: function() {
return {
username:'',
password:'',
autoLogin:true,
};
},
};
return service;
}]);

View File

@ -20,10 +20,11 @@
<!-- <a class="ui orange ribbon label" style="margin-bottom:20px">{{ tag.name }}</a> <span></span> -->
<div class="ui grid container">
<div class="two wide column">
<i class="bookmark icon"></i><span>{{ tag.name }}</span>
<i class="bookmark icon"></i><span>{{ tag.name }} </span>
<span>{{ hoverItem }}</span>
</div>
<div class="two wide column js-navigate-bookmark" ng-repeat="bookmark in tag.bookmarks" ng-click="jumpToUrl(bookmark.url)">
<div class="two wide column js-navigate-bookmark" ng-class="{div-hover:hoverItem}" ng-mouseover="hoverItem=true" ng-mouseleave="hoverItem=false" ng-repeat="bookmark in tag.bookmarks" ng-click="jumpToUrl(bookmark.url)">
<img class="ui ui middle aligned tiny image" src="http://api.byi.pw/favicon/?url={{ bookmark.url }}" style="width:16px;height:16px">
<span>{{ bookmark.title}}</span>
</div>

View File

@ -7,14 +7,14 @@
<div class="required field">
<label>用户名</label>
<div class="ui icon input">
<input type="text" ng-model="userName">
<input type="text" ng-model="username">
<i class="user icon"></i>
</div>
</div>
<div class="required field">
<label>密码</label>
<div class="ui icon input">
<input type="password" ng-model="pwd">
<input type="password" ng-model="password">
<i class="lock icon"></i>
</div>
</div>

View File

@ -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);
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);
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({
data: "you have auto login",
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;