xref: /openbmc/phosphor-webui/app/login/controllers/login-controller.js (revision 09081ec7f06491150e90838a4bec53004358db51)
199d199f3SIftekharul Islam/**
299d199f3SIftekharul Islam * Controller for the login page
399d199f3SIftekharul Islam *
499d199f3SIftekharul Islam * @module app/login/controllers/index
599d199f3SIftekharul Islam * @exports LoginController
699d199f3SIftekharul Islam * @name LoginController
799d199f3SIftekharul Islam */
899d199f3SIftekharul Islam
999d199f3SIftekharul Islamwindow.angular && (function(angular) {
1099d199f3SIftekharul Islam  'use strict';
1199d199f3SIftekharul Islam
12d27bb135SAndrew Geissler  angular.module('app.login').controller('LoginController', [
131c43b312SEd Tanous    '$scope',
141c43b312SEd Tanous    '$window',
151c43b312SEd Tanous    'dataService',
161c43b312SEd Tanous    'userModel',
171c43b312SEd Tanous    '$location',
181c43b312SEd Tanous    function($scope, $window, dataService, userModel, $location) {
1999d199f3SIftekharul Islam      $scope.dataService = dataService;
20a09cc2daSbeccabroek      $scope.serverUnreachable = false;
21a09cc2daSbeccabroek      $scope.invalidCredentials = false;
221acb412dSIftekharul Islam      $scope.host = $scope.dataService.host.replace(/^https?\:\/\//ig, '');
2399d199f3SIftekharul Islam
241acb412dSIftekharul Islam      $scope.tryLogin = function(host, username, password, event) {
25f8f19f95SGunnar Mills        // keyCode 13 is the 'Enter' button. If the user hits 'Enter' while in
26f8f19f95SGunnar Mills        // one of the 3 fields, attempt to log in.
2799d199f3SIftekharul Islam        if (event.keyCode === 13) {
281acb412dSIftekharul Islam          $scope.login(host, username, password);
2999d199f3SIftekharul Islam        }
3099d199f3SIftekharul Islam      };
311acb412dSIftekharul Islam      $scope.login = function(host, username, password) {
32a09cc2daSbeccabroek        $scope.serverUnreachable = false;
33a09cc2daSbeccabroek        $scope.invalidCredentials = false;
34d27bb135SAndrew Geissler        if (!username || username == '' || !password || password == '' ||
35d27bb135SAndrew Geissler            !host || host == '') {
3699d199f3SIftekharul Islam          return false;
37d27bb135SAndrew Geissler        } else {
381acb412dSIftekharul Islam          $scope.dataService.setHost(host);
390e480f55SAlexander Filippov          userModel.login(username, password, function(status, description) {
4099d199f3SIftekharul Islam            if (status) {
4199d199f3SIftekharul Islam              $scope.$emit('user-logged-in', {});
421c43b312SEd Tanous              var next = $location.search().next;
43b0a0847aSJames Feist              // don't allow forwarding to non-local urls
44b0a0847aSJames Feist              if (next === undefined || next == null ||
45*09081ec7SJames Feist                  next.indexOf(':') >= 0) {
469dd479d9SMichael Davis                $window.location.hash = '#/overview/server';
47d27bb135SAndrew Geissler              } else {
481c43b312SEd Tanous                $window.location.href = next;
491c43b312SEd Tanous              }
501c43b312SEd Tanous            } else {
51b5c5dc5dSbeccabroek              if (description === 'Unauthorized') {
52a09cc2daSbeccabroek                $scope.invalidCredentials = true;
53a09cc2daSbeccabroek              } else {
54a09cc2daSbeccabroek                $scope.serverUnreachable = true;
5599d199f3SIftekharul Islam              }
56ba5e3f34SAndrew Geissler            }
5799d199f3SIftekharul Islam          });
5899d199f3SIftekharul Islam        }
59ba5e3f34SAndrew Geissler      };
601c43b312SEd Tanous    },
61ba5e3f34SAndrew Geissler  ]);
6299d199f3SIftekharul Islam})(angular);
63