login_ctrl.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. // build info view model
  32. $scope.buildInfo = {
  33. version: config.buildInfo.version,
  34. commit: config.buildInfo.commit,
  35. buildstamp: new Date(config.buildInfo.buildstamp * 1000),
  36. latestVersion: config.buildInfo.latestVersion,
  37. hasUpdate: config.buildInfo.hasUpdate,
  38. };
  39. $scope.submit = function() {
  40. if ($scope.loginMode) {
  41. $scope.login();
  42. } else {
  43. $scope.signUp();
  44. }
  45. };
  46. $scope.loginModeChanged = function(newValue) {
  47. $scope.submitBtnText = newValue ? 'Log in' : 'Sign up';
  48. };
  49. $scope.signUp = function() {
  50. if (!$scope.loginForm.$valid) {
  51. return;
  52. }
  53. backendSrv.post('/api/user/signup', $scope.formModel).then(function(result) {
  54. if (result.status === 'SignUpCreated') {
  55. $location.path('/signup').search({email: $scope.formModel.email});
  56. } else {
  57. window.location.href = config.appSubUrl + '/';
  58. }
  59. });
  60. };
  61. $scope.login = function() {
  62. delete $scope.loginError;
  63. if (!$scope.loginForm.$valid) {
  64. return;
  65. }
  66. backendSrv.post('/login', $scope.formModel).then(function(result) {
  67. if (result.redirectUrl) {
  68. window.location.href = result.redirectUrl;
  69. } else {
  70. window.location.href = config.appSubUrl + '/';
  71. }
  72. });
  73. };
  74. $scope.init();
  75. });
  76. });