login_ctrl.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import _ from "lodash";
  2. import coreModule from "../core_module";
  3. import config from "app/core/config";
  4. export class LoginCtrl {
  5. /** @ngInject */
  6. constructor($scope, backendSrv, contextSrv, $location) {
  7. $scope.formModel = {
  8. user: "",
  9. email: "",
  10. password: ""
  11. };
  12. contextSrv.sidemenu = false;
  13. $scope.oauth = config.oauth;
  14. $scope.oauthEnabled = _.keys(config.oauth).length > 0;
  15. $scope.disableLoginForm = config.disableLoginForm;
  16. $scope.disableUserSignUp = config.disableUserSignUp;
  17. $scope.loginHint = config.loginHint;
  18. $scope.loginMode = true;
  19. $scope.submitBtnText = "Log in";
  20. $scope.init = function() {
  21. $scope.$watch("loginMode", $scope.loginModeChanged);
  22. if (config.loginError) {
  23. $scope.appEvent("alert-warning", ["Login Failed", config.loginError]);
  24. }
  25. };
  26. $scope.submit = function() {
  27. if ($scope.loginMode) {
  28. $scope.login();
  29. } else {
  30. $scope.signUp();
  31. }
  32. };
  33. $scope.loginModeChanged = function(newValue) {
  34. $scope.submitBtnText = newValue ? "Log in" : "Sign up";
  35. };
  36. $scope.signUp = function() {
  37. if (!$scope.loginForm.$valid) {
  38. return;
  39. }
  40. backendSrv
  41. .post("/api/user/signup", $scope.formModel)
  42. .then(function(result) {
  43. if (result.status === "SignUpCreated") {
  44. $location.path("/signup").search({ email: $scope.formModel.email });
  45. } else {
  46. window.location.href = config.appSubUrl + "/";
  47. }
  48. });
  49. };
  50. $scope.login = function() {
  51. delete $scope.loginError;
  52. if (!$scope.loginForm.$valid) {
  53. return;
  54. }
  55. backendSrv.post("/login", $scope.formModel).then(function(result) {
  56. var params = $location.search();
  57. if (params.redirect && params.redirect[0] === "/") {
  58. window.location.href = config.appSubUrl + params.redirect;
  59. } else 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. }
  69. coreModule.controller("LoginCtrl", LoginCtrl);