Files
grafana/public/app/core/controllers/login_ctrl.js
T

89 lines
2.1 KiB
JavaScript
Raw Normal View History

2014-08-12 15:06:54 +02:00
define([
'angular',
2016-09-28 15:10:50 +02:00
'lodash',
'../core_module',
2015-10-30 14:19:02 +01:00
'app/core/config',
2014-08-12 15:06:54 +02:00
],
2016-09-28 15:10:50 +02:00
function (angular, _, coreModule, config) {
2014-08-12 15:06:54 +02:00
'use strict';
config = config.default;
2015-12-16 12:21:13 +01:00
coreModule.default.controller('LoginCtrl', function($scope, backendSrv, contextSrv, $location) {
2015-01-22 11:51:44 +01:00
$scope.formModel = {
user: '',
2015-01-22 11:51:44 +01:00
email: '',
password: '',
};
contextSrv.sidemenu = false;
2016-09-28 15:10:50 +02:00
$scope.oauth = config.oauth;
$scope.oauthEnabled = _.keys(config.oauth).length > 0;
$scope.disableLoginForm = config.disableLoginForm;
2015-01-29 15:46:36 +01:00
$scope.disableUserSignUp = config.disableUserSignUp;
2015-08-20 11:15:36 -07:00
$scope.loginHint = config.loginHint;
$scope.loginMode = true;
2015-01-22 11:51:44 +01:00
$scope.submitBtnText = 'Log in';
$scope.init = function() {
$scope.$watch("loginMode", $scope.loginModeChanged);
if (config.loginError) {
$scope.appEvent('alert-warning', ['Login Failed', config.loginError]);
}
2015-01-22 11:51:44 +01:00
};
2014-08-12 15:06:54 +02:00
$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';
};
$scope.signUp = function() {
if (!$scope.loginForm.$valid) {
return;
}
backendSrv.post('/api/user/signup', $scope.formModel).then(function(result) {
if (result.status === 'SignUpCreated') {
$location.path('/signup').search({email: $scope.formModel.email});
} else {
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) {
var params = $location.search();
if (params.redirect && params.redirect[0] === '/') {
window.location.href = config.appSubUrl + params.redirect;
}
else 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
});
});