login_ctrl.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. };
  14. coreModule.default.controller('LoginCtrl', function($scope, backendSrv, contextSrv, $location) {
  15. $scope.formModel = {
  16. user: '',
  17. email: '',
  18. password: '',
  19. };
  20. contextSrv.sidemenu = false;
  21. $scope.oauth = config.oauth;
  22. $scope.oauthEnabled = _.keys(config.oauth).length > 0;
  23. $scope.allowUserPassLogin = config.allowUserPassLogin;
  24. $scope.disableUserSignUp = config.disableUserSignUp;
  25. $scope.loginHint = config.loginHint;
  26. $scope.loginMode = true;
  27. $scope.submitBtnText = 'Log in';
  28. $scope.init = function() {
  29. $scope.$watch("loginMode", $scope.loginModeChanged);
  30. var params = $location.search();
  31. if (params.failCode) {
  32. $scope.appEvent('alert-warning', ['Login Failed', failCodes[params.failCode]]);
  33. delete params.failedMsg;
  34. $location.search(params);
  35. }
  36. };
  37. $scope.submit = function() {
  38. if ($scope.loginMode) {
  39. $scope.login();
  40. } else {
  41. $scope.signUp();
  42. }
  43. };
  44. $scope.loginModeChanged = function(newValue) {
  45. $scope.submitBtnText = newValue ? 'Log in' : 'Sign up';
  46. };
  47. $scope.signUp = function() {
  48. if (!$scope.loginForm.$valid) {
  49. return;
  50. }
  51. backendSrv.post('/api/user/signup', $scope.formModel).then(function(result) {
  52. if (result.status === 'SignUpCreated') {
  53. $location.path('/signup').search({email: $scope.formModel.email});
  54. } else {
  55. window.location.href = config.appSubUrl + '/';
  56. }
  57. });
  58. };
  59. $scope.login = function() {
  60. delete $scope.loginError;
  61. if (!$scope.loginForm.$valid) {
  62. return;
  63. }
  64. backendSrv.post('/login', $scope.formModel).then(function(result) {
  65. var params = $location.search();
  66. if (params.redirect && params.redirect[0] === '/') {
  67. window.location.href = config.appSubUrl + params.redirect;
  68. }
  69. else if (result.redirectUrl) {
  70. window.location.href = result.redirectUrl;
  71. } else {
  72. window.location.href = config.appSubUrl + '/';
  73. }
  74. });
  75. };
  76. $scope.init();
  77. });
  78. });