login_ctrl.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. if (result.redirectUrl) {
  60. window.location.href = result.redirectUrl;
  61. } else {
  62. window.location.href = config.appSubUrl + '/';
  63. }
  64. });
  65. };
  66. $scope.init();
  67. });
  68. });