loginCtrl.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. define([
  2. 'angular',
  3. 'config',
  4. ],
  5. function (angular, config) {
  6. 'use strict';
  7. var module = angular.module('grafana.controllers');
  8. module.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() {
  51. window.location.href = config.appSubUrl + '/';
  52. });
  53. };
  54. $scope.login = function() {
  55. delete $scope.loginError;
  56. if (!$scope.loginForm.$valid) {
  57. return;
  58. }
  59. backendSrv.post('/login', $scope.formModel).then(function(result) {
  60. if (result.redirectUrl) {
  61. window.location.href = result.redirectUrl;
  62. } else {
  63. window.location.href = config.appSubUrl + '/';
  64. }
  65. });
  66. };
  67. $scope.init();
  68. });
  69. });