login_ctrl.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. define([
  2. 'angular',
  3. 'lodash',
  4. '../core_module',
  5. 'app/core/config',
  6. ],
  7. function (angular, _, coreModule, config) {
  8. 'use strict';
  9. var failCodes = {
  10. "1000": "Required team membership not fulfilled",
  11. "1001": "Required organization membership not fulfilled",
  12. "1002": "Required email domain not fulfilled",
  13. "1003": "Login provider denied login request",
  14. };
  15. coreModule.default.controller('LoginCtrl', function($scope, backendSrv, contextSrv, $location) {
  16. $scope.formModel = {
  17. user: '',
  18. email: '',
  19. password: '',
  20. };
  21. contextSrv.sidemenu = false;
  22. $scope.oauth = config.oauth;
  23. $scope.oauthEnabled = _.keys(config.oauth).length > 0;
  24. $scope.disableLoginForm = config.disableLoginForm;
  25. $scope.disableUserSignUp = config.disableUserSignUp;
  26. $scope.loginHint = config.loginHint;
  27. $scope.loginMode = true;
  28. $scope.submitBtnText = 'Log in';
  29. $scope.init = function() {
  30. $scope.$watch("loginMode", $scope.loginModeChanged);
  31. var params = $location.search();
  32. if (params.failCode) {
  33. $scope.appEvent('alert-warning', ['Login Failed', failCodes[params.failCode]]);
  34. delete params.failedMsg;
  35. $location.search(params);
  36. }
  37. };
  38. $scope.submit = function() {
  39. if ($scope.loginMode) {
  40. $scope.login();
  41. } else {
  42. $scope.signUp();
  43. }
  44. };
  45. $scope.loginModeChanged = function(newValue) {
  46. $scope.submitBtnText = newValue ? 'Log in' : 'Sign up';
  47. };
  48. $scope.signUp = function() {
  49. if (!$scope.loginForm.$valid) {
  50. return;
  51. }
  52. backendSrv.post('/api/user/signup', $scope.formModel).then(function(result) {
  53. if (result.status === 'SignUpCreated') {
  54. $location.path('/signup').search({email: $scope.formModel.email});
  55. } else {
  56. window.location.href = config.appSubUrl + '/';
  57. }
  58. });
  59. };
  60. $scope.login = function() {
  61. delete $scope.loginError;
  62. if (!$scope.loginForm.$valid) {
  63. return;
  64. }
  65. backendSrv.post('/login', $scope.formModel).then(function(result) {
  66. var params = $location.search();
  67. if (params.redirect && params.redirect[0] === '/') {
  68. window.location.href = config.appSubUrl + params.redirect;
  69. }
  70. else if (result.redirectUrl) {
  71. window.location.href = result.redirectUrl;
  72. } else {
  73. window.location.href = config.appSubUrl + '/';
  74. }
  75. });
  76. };
  77. $scope.init();
  78. });
  79. });