From b18eaa102c684e8a2b4301ea03b5f9fdec9b454c Mon Sep 17 00:00:00 2001 From: Stefan Sterz Date: Tue, 23 Feb 2016 17:48:14 +0100 Subject: [PATCH] add authentication --- nodejs/public/javascripts/angularApp.js | 131 ++++++++++++++++++++++-- 1 file changed, 125 insertions(+), 6 deletions(-) diff --git a/nodejs/public/javascripts/angularApp.js b/nodejs/public/javascripts/angularApp.js index 7ab8da2..f1daec6 100644 --- a/nodejs/public/javascripts/angularApp.js +++ b/nodejs/public/javascripts/angularApp.js @@ -11,40 +11,126 @@ app.config(['$stateProvider', '$urlRouterProvider',function($stateProvider, $url postPromise: ['arduino', function(arduino) { return arduino.getRCs(); }] - } + }, + onEnter: ['$state', 'auth', function ($state, auth) { + if (!auth.isLoggedIn()) { + $state.go('login'); + } + }] + }) + .state('login', { + url: '/login', + templateUrl: '/login.html', + controller: 'AuthCtrl', + onEnter: ['$state', 'auth', function ($state, auth) { + if (auth.isLoggedIn()) { + $state.go('home'); + } + }] + }) + .state('register', { + url: '/register', + templateUrl: '/register.html', + controller: 'AuthCtrl', + onEnter: ['$state', 'auth', function ($state, auth) { + if (!auth.isLoggedIn()) { + $state.go('home'); + } + }] }); $urlRouterProvider.otherwise('home'); }]); +app.factory('auth', ['$http', '$window', function ($http, $window) { + var auth = {}; -app.factory('arduino', ['$http', function($http){ + auth.saveToken = function (token) { + $window.localStorage['autohome-token'] = token; + }; + + auth.getToken = function () { + return $window.localStorage['autohome-token']; + }; + + auth.isLoggedIn = function () { + var token = auth.getToken(); + + if (token) { + var payload = JSON.parse($window.atob(token.split('.')[1])); + + return payload.exp > Date.now() / 1000; + } + + return false; + }; + + auth.currentUser = function () { + if (auth.isLoggedIn()) { + var token = auth.getToken(); + var payload = JSON.parse($window.atob(token.split('.')[1])); + + return payload.username; + } + }; + + auth.register = function (user) { + return $http.post('/register', user, { + headers: {Authorization: 'Bearer '+auth.getToken()} + }).success(function (data) { + auth.saveToken(data.token); + }); + }; + + auth.logIn = function (user) { + return $http.post('/login', user).success(function (data) { + auth.saveToken(data.token); + }); + }; + + auth.logOut = function () { + $window.localStorage.removeItem('autohome-token'); + }; + + return auth; +}]); + +app.factory('arduino', ['$http', 'auth', function($http, auth){ var o = { RCs: [] }; o.getRCs = function ( ) { - return $http.get('/getRCs').success(function(data) { + return $http.get('/getRCs',{ + headers: {Authorization: 'Bearer '+auth.getToken()} + }).success(function(data) { angular.copy(data, o.RCs); }); } o.sendRC = function (RC) { var code = (RC.state) ? RC.off_value : RC.on_value; - return $http.post('/register', {code}).success(function (data) { + return $http.post('/sendRC', {code}, { + headers: {Authorization: 'Bearer '+auth.getToken()} + }).success(function (data) { RC.state = (RC.state) ? false : true; }); } o.irTest = function ( ) { - return $http.get('/irTest'); + return $http.get('/irTest',{ + headers: {Authorization: 'Bearer '+auth.getToken()} + }); } return o; }]); -app.controller('MainCtrl', ['$scope', 'arduino', function ($scope, arduino){ +app.controller('MainCtrl', [ + '$scope', + 'arduino', + function ($scope, arduino){ $scope.RCs = arduino.RCs; @@ -56,4 +142,37 @@ app.controller('MainCtrl', ['$scope', 'arduino', function ($scope, arduino){ arduino.irTest(); }; +}]); + +app.controller('AuthCtrl', [ +'$scope', +'$state', +'auth', +function($scope, $state, auth){ + $scope.user = {}; + + $scope.register = function(){ + auth.register($scope.user).error(function(error){ + $scope.error = error; + }).then(function(){ + $state.go('home'); + }); + }; + + $scope.logIn = function(){ + auth.logIn($scope.user).error(function(error){ + $scope.error = error; + }).then(function(){ + $state.go('home'); + }); + }; +}]); + +app.controller('NavCtrl', [ +'$scope', +'auth', +function($scope, auth){ + $scope.isLoggedIn = auth.isLoggedIn; + $scope.currentUser = auth.currentUser; + $scope.logOut = auth.logOut; }]); \ No newline at end of file