login_ctrl.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. var params = $location.search();
  61. if (params.redirect && params.redirect[0] === '/') {
  62. window.location.href = config.appSubUrl + params.redirect;
  63. }
  64. else if (result.redirectUrl) {
  65. window.location.href = result.redirectUrl;
  66. } else {
  67. window.location.href = config.appSubUrl + '/';
  68. }
  69. });
  70. };
  71. $scope.init();
  72. });
  73. });