login_ctrl.js 2.7 KB

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