loginCtrl.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.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. var params = $location.search();
  26. if (params.failedMsg) {
  27. $scope.appEvent('alert-warning', ['Login Failed', params.failedMsg]);
  28. delete params.failedMsg;
  29. $location.search(params);
  30. }
  31. };
  32. // build info view model
  33. $scope.buildInfo = {
  34. version: config.buildInfo.version,
  35. commit: config.buildInfo.commit,
  36. buildstamp: new Date(config.buildInfo.buildstamp * 1000)
  37. };
  38. $scope.submit = function() {
  39. if ($scope.loginMode) {
  40. $scope.login();
  41. } else {
  42. $scope.signUp();
  43. }
  44. };
  45. $scope.loginModeChanged = function(newValue) {
  46. $scope.submitBtnText = newValue ? 'Log in' : 'Sign up';
  47. };
  48. $scope.passwordChanged = function(newValue) {
  49. if (!newValue) {
  50. $scope.strengthText = "";
  51. $scope.strengthClass = "hidden";
  52. return;
  53. }
  54. if (newValue.length < 4) {
  55. $scope.strengthText = "strength: weak sauce.";
  56. $scope.strengthClass = "password-strength-bad";
  57. return;
  58. }
  59. if (newValue.length <= 8) {
  60. $scope.strengthText = "strength: you can do better.";
  61. $scope.strengthClass = "password-strength-ok";
  62. return;
  63. }
  64. $scope.strengthText = "strength: strong like a bull.";
  65. $scope.strengthClass = "password-strength-good";
  66. };
  67. $scope.signUp = function() {
  68. if (!$scope.loginForm.$valid) {
  69. return;
  70. }
  71. backendSrv.post('/api/user/signup', $scope.formModel).then(function() {
  72. window.location.href = config.appSubUrl + '/';
  73. });
  74. };
  75. $scope.login = function() {
  76. delete $scope.loginError;
  77. if (!$scope.loginForm.$valid) {
  78. return;
  79. }
  80. backendSrv.post('/login', $scope.formModel).then(function(result) {
  81. if (result.redirectUrl) {
  82. window.location.href = result.redirectUrl;
  83. } else {
  84. window.location.href = config.appSubUrl + '/';
  85. }
  86. });
  87. };
  88. $scope.init();
  89. });
  90. });