login_ctrl.js 2.2 KB

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