login_ctrl.js 2.6 KB

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