Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var app = angular.module("main", ['ngRoute']);
- app.config(function($routeProvider) {
- $routeProvider.when('/' , {
- templateUrl : './components/home.html',
- controller: 'homeCtrl'
- }) .when('/login', {
- templateUrl: './components/login.html',
- controller: 'loginCntrl'
- }).when('/dashboard', {
- resolve: {
- check: function($location, user) {
- if(!user.isUserLoggedIn()) {
- $location.path('/login');
- }
- },
- },
- templateUrl: './dashboard.html',
- controller: 'dashboardCtrl'
- }).otherwise({
- template: '404'
- });
- });
- app.service("user", function() {
- var username;
- var loggedIn = false;
- var id;
- this.setID = function(userID) {
- id = userID;
- };
- this.getID = function() {
- return id;
- };
- this.setName = function(name) {
- username = name;
- };
- this.getName = function() {
- return username;
- };
- this.isUserLoggedIn = function() {
- return loggedIn;
- };
- this.userLoggedIn = function() {
- loggedIn = true;
- };
- });
- app.controller("homeCtrl", function($scope, $location) {
- $scope.goToLogin = function() {
- $location.path("/login");
- }
- $scope.register = function() {
- $location.path("/register");
- }
- });
- app.controller("loginCntrl", function($scope, $http, $location, user) {
- $scope.login = function() {
- var username = $scope.username;
- var password = $scope.password;
- $http({
- url: 'http://localhost/webpage/server.php',
- method: 'POST',
- headers: {
- 'Content-Type' : 'application/x-www-form-urlencoded'
- },
- data: 'username='+username+'&password='+password
- }).then(function(response) {
- if(response.data.status == 'loggedIn') {
- user.userLoggedIn();
- user.setName(response.data.user);
- $location.path('/dashboard');
- } else {
- alert('Invalid Login. Please check');
- }
- })
- }
- });
- app.controller("dashboardCtrl", function($scope, user) {
- $scope.user = user.getName();
- });
Add Comment
Please, Sign In to add comment