loginCtrl.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.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() {
  50. window.location.href = config.appSubUrl + '/';
  51. });
  52. };
  53. $scope.login = function() {
  54. delete $scope.loginError;
  55. if (!$scope.loginForm.$valid) {
  56. return;
  57. }
  58. backendSrv.post('/login', $scope.formModel).then(function(result) {
  59. 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. });