login_ctrl.js 2.3 KB

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