login_ctrl.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import _ from 'lodash';
  2. import coreModule from '../core_module';
  3. import config from 'app/core/config';
  4. export class LoginCtrl {
  5. /** @ngInject */
  6. constructor($scope, backendSrv, contextSrv, $location) {
  7. $scope.formModel = {
  8. user: '',
  9. email: '',
  10. password: '',
  11. };
  12. contextSrv.sidemenu = false;
  13. $scope.oauth = config.oauth;
  14. $scope.oauthEnabled = _.keys(config.oauth).length > 0;
  15. $scope.disableLoginForm = config.disableLoginForm;
  16. $scope.disableUserSignUp = config.disableUserSignUp;
  17. $scope.loginHint = config.loginHint;
  18. $scope.loginMode = true;
  19. $scope.submitBtnText = 'Log in';
  20. $scope.init = function() {
  21. $scope.$watch("loginMode", $scope.loginModeChanged);
  22. if (config.loginError) {
  23. $scope.appEvent('alert-warning', ['Login Failed', config.loginError]);
  24. }
  25. };
  26. $scope.submit = function() {
  27. if ($scope.loginMode) {
  28. $scope.login();
  29. } else {
  30. $scope.signUp();
  31. }
  32. };
  33. $scope.loginModeChanged = function(newValue) {
  34. $scope.submitBtnText = newValue ? 'Log in' : 'Sign up';
  35. };
  36. $scope.signUp = function() {
  37. if (!$scope.loginForm.$valid) {
  38. return;
  39. }
  40. backendSrv.post('/api/user/signup', $scope.formModel).then(function(result) {
  41. if (result.status === 'SignUpCreated') {
  42. $location.path('/signup').search({email: $scope.formModel.email});
  43. } else {
  44. window.location.href = config.appSubUrl + '/';
  45. }
  46. });
  47. };
  48. $scope.login = function() {
  49. delete $scope.loginError;
  50. if (!$scope.loginForm.$valid) {
  51. return;
  52. }
  53. backendSrv.post('/login', $scope.formModel).then(function(result) {
  54. var params = $location.search();
  55. if (params.redirect && params.redirect[0] === '/') {
  56. window.location.href = config.appSubUrl + params.redirect;
  57. } else if (result.redirectUrl) {
  58. window.location.href = result.redirectUrl;
  59. } else {
  60. window.location.href = config.appSubUrl + '/';
  61. }
  62. });
  63. };
  64. $scope.init();
  65. }
  66. }
  67. coreModule.controller('LoginCtrl', LoginCtrl);