Guest User

Untitled

a guest
Apr 3rd, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. var app = angular.module("main", ['ngRoute']);
  2.  
  3. app.config(function($routeProvider) {
  4. $routeProvider.when('/' , {
  5. templateUrl : './components/home.html',
  6. controller: 'homeCtrl'
  7. }) .when('/login', {
  8. templateUrl: './components/login.html',
  9. controller: 'loginCntrl'
  10. }).when('/dashboard', {
  11. resolve: {
  12. check: function($location, user) {
  13. if(!user.isUserLoggedIn()) {
  14. $location.path('/login');
  15. }
  16. },
  17. },
  18. templateUrl: './dashboard.html',
  19. controller: 'dashboardCtrl'
  20. }).otherwise({
  21. template: '404'
  22. });
  23. });
  24.  
  25. app.service("user", function() {
  26. var username;
  27. var loggedIn = false;
  28. var id;
  29.  
  30. this.setID = function(userID) {
  31. id = userID;
  32. };
  33.  
  34. this.getID = function() {
  35. return id;
  36. };
  37.  
  38. this.setName = function(name) {
  39. username = name;
  40. };
  41.  
  42. this.getName = function() {
  43. return username;
  44. };
  45.  
  46. this.isUserLoggedIn = function() {
  47. return loggedIn;
  48. };
  49.  
  50. this.userLoggedIn = function() {
  51. loggedIn = true;
  52. };
  53. });
  54.  
  55.  
  56. app.controller("homeCtrl", function($scope, $location) {
  57. $scope.goToLogin = function() {
  58. $location.path("/login");
  59. }
  60.  
  61. $scope.register = function() {
  62. $location.path("/register");
  63. }
  64. });
  65.  
  66.  
  67. app.controller("loginCntrl", function($scope, $http, $location, user) {
  68. $scope.login = function() {
  69. var username = $scope.username;
  70. var password = $scope.password;
  71.  
  72. $http({
  73. url: 'http://localhost/webpage/server.php',
  74. method: 'POST',
  75. headers: {
  76. 'Content-Type' : 'application/x-www-form-urlencoded'
  77. },
  78. data: 'username='+username+'&password='+password
  79. }).then(function(response) {
  80. if(response.data.status == 'loggedIn') {
  81. user.userLoggedIn();
  82. user.setName(response.data.user);
  83. $location.path('/dashboard');
  84. } else {
  85. alert('Invalid Login. Please check');
  86. }
  87. })
  88. }
  89. });
  90.  
  91. app.controller("dashboardCtrl", function($scope, user) {
  92. $scope.user = user.getName();
  93. });
Add Comment
Please, Sign In to add comment