Advertisement
Guest User

controller

a guest
Apr 6th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function () {
  2.     'use strict'
  3.     angular.module('myApp')
  4.         .controller("loginController", loginController);
  5.  
  6.  
  7.     loginController.$inject = ['loginService', 'userService', '$rootScope', '$location'];
  8.  
  9.  
  10.     function loginController(loginService, userService, $rootScope, $location) {
  11.         var vm = this;
  12.         vm.login = login;
  13.         vm.register = register;
  14.         vm.activate = activate;
  15.  
  16.         vm.enteringPassword = false;
  17.         vm.pageClass = "page-login";
  18.  
  19.         vm.registrationMessage = "";
  20.         vm.loginMessage = "";
  21.  
  22.         vm.loginModel = {
  23.             mail: "",
  24.             password: ""
  25.         }
  26.  
  27.         vm.registrationModel = {
  28.             mail: "",
  29.             username: "",
  30.             password: ""
  31.         }
  32.  
  33.         vm.activationcode = "";
  34.         vm.activationMessage = "";
  35.         function login()
  36.         {
  37.             loginService.login(vm.loginModel).then(function (response)
  38.             {
  39.                 if(response.status == 200)
  40.                 {
  41.  
  42.                     loginService.getUserForToken(userService.getToken()).then(function (result)
  43.                     {
  44.                         var data = result.data;
  45.                         userService.setUsername(data.username);
  46.                         userService.setUserID(data.ID);
  47.                         vm.loginMessage = "Hello " + data.username;
  48.  
  49.  
  50.                         var message = {userID: data.ID,username: data.username}
  51.                         $rootScope.$emit('loginEvent', message);
  52.  
  53.                         $location.path('/home');
  54.                     });
  55.  
  56.  
  57.  
  58.  
  59.                 }
  60.                 else if(response.status == 401)
  61.                 {
  62.                     vm.loginMessage = "Invalid credentials!";
  63.                 }
  64.                 else
  65.                 {
  66.                     vm.loginMessage = "Failed to login due to a server error!";
  67.                 }
  68.             });
  69.         }
  70.  
  71.         function activate()
  72.         {
  73.             loginService.activate(vm.activationcode).then(function (response)
  74.             {
  75.                 if(response.status == 200)
  76.                 {
  77.                    vm.activationMessage = "Succesfully activated your account, you can now log in!";
  78.                 }
  79.                 else
  80.                 {
  81.                     vm.activationMessage = "Invalid activation code!";
  82.                 }
  83.             });
  84.         }
  85.  
  86.         function register()
  87.         {
  88.             loginService.register(vm.registrationModel).then(function (response)
  89.             {
  90.                 if(response.status == 200)
  91.                 {
  92.                     $("#activation-form").delay(100).fadeIn(100);
  93.                     $("#login-form").fadeOut(100);
  94.                     $("#register-form").fadeOut(100);
  95.                     $('#login-form-link').removeClass('active');
  96.                     $('#register-form-link').removeClass('active');
  97.                     $("#activation-form-link").addClass('active');
  98.                 }
  99.                 else if(response.status == 403)
  100.                 {
  101.                     vm.registrationMessage = "Your email is already in use!";
  102.                 }
  103.                 else
  104.                 {
  105.                     vm.registrationMessage = "Failed to register due to a server error!";
  106.                 }
  107.             });
  108.         }
  109.     }
  110.  
  111.  
  112. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement