login_ctrl.js 2.1 KB

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