login_ctrl.js 2.5 KB

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