login_ctrl.js 2.1 KB

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