var $app = angular.module('LoginApp', ['ngRoute']); $app.config(function ($routeProvider, $locationProvider) { $routeProvider .when("/", { templateUrl: "/auth/login/login.html", controller: "LoginController" }) .when("/forgot", { templateUrl: "/auth/login/forgot.html", controller: "ForgotPasswordController" }) .when("/forgot-confirm", { templateUrl: "/auth/login/forgot-confirm.html", controller: "GenericController" }) .when("/change-confirm", { templateUrl: "/auth/login/change-confirm.html", controller: "GenericController" }) .when("/reset", { templateUrl: "/auth/login/change-password.html", controller: "ChangePasswordController" }) .when("/expired-link", { templateUrl: "/auth/login/expired-link.php", controller: "GenericController" }) .when("/:wildcard*\/", { /* this route allows us to redirect to a screen in the app after log-in */ templateUrl: "/auth/login/login.html", controller: "LoginController" }) .otherwise({ redirectTo: "/" }); }) $app.controller("LoginController", ["$scope", "$http", "$window", "$timeout", "AnimationService", "$location", function ($scope, $http, $window, $timeout, AnimationService, $location) { AnimationService.pageLoad($scope); $scope.login = function() { $scope.submitting = true; $scope.error = ""; $http.post("/auth/api/login.php", {"username": $scope.username, "password": $scope.password}) .then( //success function(data) { $window.location.href = '/#' + $location.url(); }, //error function(data) { $scope.submitting = false; console.log("Login error: " + data); $scope.error = data.data.message; $scope.password = ""; }); } }]); $app.controller("ForgotPasswordController", ["$scope", "$http", "$window", "$timeout", "AnimationService", function ($scope, $http, $window, $timeout, AnimationService) { AnimationService.pageLoad($scope); $scope.reset = function() { $scope.submitting = true; $scope.error = ""; $http.post("/auth/api/reset.php", {"email": $scope.email}) .then( //success function(data) { $window.location.href = '#/forgot-confirm'; }, //error function(data) { $scope.submitting = false; console.log("Login error: " + data); $scope.error = data.data.message; $scope.password = ""; }); } }]); $app.controller("ChangePasswordController", ["$scope", "$http", "$window", "$timeout", "AnimationService", "$location", function ($scope, $http, $window, $timeout, AnimationService, $location) { AnimationService.pageLoad($scope); $scope.key = $location.search().q; $scope.userid = $location.search().u; $scope.isLoggedIn = 0; $http.get('/auth/api/is-logged-in.php'). then( // success function(data){ $scope.isLoggedIn = 1; }, //error function(){ $scope.isLoggedIn = 0; }); $scope.change = function() { if($scope.password != $scope.confirmPassword) { $scope.error = "Passwords do not match."; $scope.password = ""; $scope.confirmPassword = ""; return; } $scope.submitting = true; $scope.error = ""; $http.post("/auth/api/change-password.php", {"password": $scope.password, "userid": $scope.userid, "token": $scope.key}) .then( //success function(data) { // if(!$scope.isLoggedIn) // $window.location.href = '#/change-confirm'; // else $window.location.href = '/'; }, //error function(data) { $scope.submitting = false; console.log("Login error: " + data); $scope.error = data.data.message; $scope.password = ""; $scope.confirmPassword = ""; }); } }]); $app.controller("GenericController", ["$scope", "$http", "$window", "$timeout", "AnimationService", function ($scope, $http, $window, $timeout, AnimationService) { AnimationService.pageLoad($scope); }]); $app.service('AnimationService', function ($timeout) { var self = this; self.pageLoad = function(scope) { scope.submitting = false; scope.loginFadein = 0; $timeout( function(){ scope.loginFadein = 1; }, 0 ); } });