Files
grafana/src/app/controllers/loginCtrl.js

104 lines
2.5 KiB
JavaScript
Raw Normal View History

2014-08-12 15:06:54 +02:00
define([
'angular',
2015-01-05 07:58:15 +01:00
'config',
2014-08-12 15:06:54 +02:00
],
2015-01-05 07:58:15 +01:00
function (angular, config) {
2014-08-12 15:06:54 +02:00
'use strict';
var module = angular.module('grafana.controllers');
2015-01-29 15:46:36 +01:00
module.controller('LoginCtrl', function($scope, backendSrv) {
2015-01-22 11:51:44 +01:00
$scope.formModel = {
user: '',
2015-01-22 11:51:44 +01:00
email: '',
password: '',
};
2014-09-17 15:25:19 +02:00
$scope.grafana.sidemenu = false;
$scope.googleAuthEnabled = config.googleAuthEnabled;
$scope.githubAuthEnabled = config.githubAuthEnabled;
2015-01-29 15:46:36 +01:00
$scope.disableUserSignUp = config.disableUserSignUp;
$scope.loginMode = true;
2015-01-22 11:51:44 +01:00
$scope.submitBtnClass = 'btn-inverse';
$scope.submitBtnText = 'Log in';
$scope.strengthClass = '';
$scope.init = function() {
$scope.$watch("loginMode", $scope.loginModeChanged);
$scope.passwordChanged();
};
2014-08-12 15:06:54 +02:00
2015-01-05 10:46:16 +01:00
// build info view model
$scope.buildInfo = {
version: config.buildInfo.version,
commit: config.buildInfo.commit,
buildstamp: new Date(config.buildInfo.buildstamp * 1000)
};
$scope.submit = function() {
if ($scope.loginMode) {
$scope.login();
} else {
$scope.signUp();
}
};
2015-01-22 11:51:44 +01:00
$scope.loginModeChanged = function(newValue) {
$scope.submitBtnText = newValue ? 'Log in' : 'Sign up';
};
2015-01-22 11:51:44 +01:00
$scope.passwordChanged = function(newValue) {
if (!newValue) {
$scope.strengthText = "";
$scope.strengthClass = "hidden";
return;
}
2015-01-22 11:51:44 +01:00
if (newValue.length < 4) {
$scope.strengthText = "strength: weak sauce.";
$scope.strengthClass = "password-strength-bad";
return;
2014-08-16 21:54:05 +02:00
}
2015-01-22 11:51:44 +01:00
if (newValue.length <= 6) {
$scope.strengthText = "strength: you can do better.";
$scope.strengthClass = "password-strength-ok";
return;
}
$scope.strengthText = "strength: strong like a bull.";
$scope.strengthClass = "password-strength-good";
2014-08-16 21:54:05 +02:00
};
$scope.signUp = function() {
if (!$scope.loginForm.$valid) {
return;
}
backendSrv.post('/api/user/signup', $scope.formModel).then(function() {
window.location.href = config.appSubUrl + '/';
});
};
2014-08-12 15:06:54 +02:00
$scope.login = function() {
delete $scope.loginError;
2014-08-16 21:54:05 +02:00
2014-08-12 15:06:54 +02:00
if (!$scope.loginForm.$valid) {
return;
}
backendSrv.post('/login', $scope.formModel).then(function(result) {
if (result.redirectUrl) {
window.location.href = result.redirectUrl;
} else {
window.location.href = config.appSubUrl + '/';
}
2014-08-12 15:06:54 +02:00
});
};
2014-08-16 21:54:05 +02:00
$scope.init();
2014-08-12 15:06:54 +02:00
});
});