login_ctrl.js 2.1 KB

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