login_ctrl.js 2.8 KB

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