login_ctrl.js 2.1 KB

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