login_ctrl.js 2.3 KB

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