loginCtrl.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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) {
  9. $scope.formModel = {
  10. user: '',
  11. email: '',
  12. password: '',
  13. };
  14. contextSrv.setSideMenuState(false);
  15. $scope.googleAuthEnabled = config.googleAuthEnabled;
  16. $scope.githubAuthEnabled = config.githubAuthEnabled;
  17. $scope.disableUserSignUp = config.disableUserSignUp;
  18. $scope.loginMode = true;
  19. $scope.submitBtnClass = 'btn-inverse';
  20. $scope.submitBtnText = 'Log in';
  21. $scope.strengthClass = '';
  22. $scope.init = function() {
  23. $scope.$watch("loginMode", $scope.loginModeChanged);
  24. $scope.passwordChanged();
  25. };
  26. // build info view model
  27. $scope.buildInfo = {
  28. version: config.buildInfo.version,
  29. commit: config.buildInfo.commit,
  30. buildstamp: new Date(config.buildInfo.buildstamp * 1000)
  31. };
  32. $scope.submit = function() {
  33. if ($scope.loginMode) {
  34. $scope.login();
  35. } else {
  36. $scope.signUp();
  37. }
  38. };
  39. $scope.loginModeChanged = function(newValue) {
  40. $scope.submitBtnText = newValue ? 'Log in' : 'Sign up';
  41. };
  42. $scope.passwordChanged = function(newValue) {
  43. if (!newValue) {
  44. $scope.strengthText = "";
  45. $scope.strengthClass = "hidden";
  46. return;
  47. }
  48. if (newValue.length < 4) {
  49. $scope.strengthText = "strength: weak sauce.";
  50. $scope.strengthClass = "password-strength-bad";
  51. return;
  52. }
  53. if (newValue.length <= 6) {
  54. $scope.strengthText = "strength: you can do better.";
  55. $scope.strengthClass = "password-strength-ok";
  56. return;
  57. }
  58. $scope.strengthText = "strength: strong like a bull.";
  59. $scope.strengthClass = "password-strength-good";
  60. };
  61. $scope.signUp = function() {
  62. if (!$scope.loginForm.$valid) {
  63. return;
  64. }
  65. backendSrv.post('/api/user/signup', $scope.formModel).then(function() {
  66. window.location.href = config.appSubUrl + '/';
  67. });
  68. };
  69. $scope.login = function() {
  70. delete $scope.loginError;
  71. if (!$scope.loginForm.$valid) {
  72. return;
  73. }
  74. backendSrv.post('/login', $scope.formModel).then(function(result) {
  75. if (result.redirectUrl) {
  76. window.location.href = result.redirectUrl;
  77. } else {
  78. window.location.href = config.appSubUrl + '/';
  79. }
  80. });
  81. };
  82. $scope.init();
  83. });
  84. });