mirror of
https://github.com/taobataoma/meanTorrent.git
synced 2026-01-17 12:52:22 +01:00
if sign up is open, all user can free register otherwise, sign up is closed, and show tooltip message to user
139 lines
3.9 KiB
JavaScript
139 lines
3.9 KiB
JavaScript
(function () {
|
|
'use strict';
|
|
|
|
angular
|
|
.module('users')
|
|
.controller('AuthenticationController', AuthenticationController);
|
|
|
|
AuthenticationController.$inject = ['$scope', '$state', 'UsersService', '$location', '$window', 'Authentication', 'PasswordValidator', 'Notification',
|
|
'MeanTorrentConfig', 'getStorageLangService'];
|
|
|
|
function AuthenticationController($scope, $state, UsersService, $location, $window, Authentication, PasswordValidator, Notification, MeanTorrentConfig,
|
|
getStorageLangService) {
|
|
var vm = this;
|
|
|
|
vm.lang = getStorageLangService.getLang();
|
|
vm.signConfig = MeanTorrentConfig.meanTorrentConfig.sign;
|
|
vm.authentication = Authentication;
|
|
vm.getPopoverMsg = PasswordValidator.getPopoverMsg;
|
|
vm.signup = signup;
|
|
vm.signin = signin;
|
|
vm.callOauthProvider = callOauthProvider;
|
|
vm.usernameRegex = /^(?=[\w.-]+$)(?!.*[._-]{2})(?!\.)(?!.*\.$).{3,34}$/;
|
|
|
|
// Get an eventual error defined in the URL query string:
|
|
if ($location.search().err) {
|
|
Notification.error({message: $location.search().err});
|
|
}
|
|
|
|
// If user is signed in then redirect back home
|
|
if (vm.authentication.user) {
|
|
$location.path('/');
|
|
}
|
|
|
|
/**
|
|
* signup
|
|
* @param isValid
|
|
* @returns {boolean}
|
|
*/
|
|
function signup(isValid) {
|
|
|
|
if (!isValid) {
|
|
$scope.$broadcast('show-errors-check-validity', 'vm.userForm');
|
|
|
|
return false;
|
|
}
|
|
|
|
UsersService.userSignup(vm.credentials)
|
|
.then(onUserSignupSuccess)
|
|
.catch(onUserSignupError);
|
|
}
|
|
|
|
/**
|
|
* signin
|
|
* @param isValid
|
|
* @returns {boolean}
|
|
*/
|
|
function signin(isValid) {
|
|
|
|
if (!isValid) {
|
|
$scope.$broadcast('show-errors-check-validity', 'vm.userForm');
|
|
|
|
return false;
|
|
}
|
|
|
|
UsersService.userSignin(vm.credentials)
|
|
.then(onUserSigninSuccess)
|
|
.catch(onUserSigninError);
|
|
}
|
|
|
|
// OAuth provider request
|
|
/**
|
|
* callOauthProvider
|
|
* @param url
|
|
*/
|
|
function callOauthProvider(url) {
|
|
if ($state.previous && $state.previous.href) {
|
|
url += '?redirect_to=' + encodeURIComponent($state.previous.href);
|
|
}
|
|
|
|
// Effectively call OAuth authentication route:
|
|
$window.location.href = url;
|
|
}
|
|
|
|
// Authentication Callbacks
|
|
/**
|
|
* onUserSignupSuccess
|
|
* @param response
|
|
*/
|
|
function onUserSignupSuccess(response) {
|
|
// If successful we assign the response to the global user model
|
|
vm.authentication.user = response;
|
|
Notification.success({message: '<i class="glyphicon glyphicon-ok"></i> Signup successful!'});
|
|
// And redirect to the previous or home page
|
|
$state.go($state.previous.state.name || 'home', $state.previous.params);
|
|
}
|
|
|
|
/**
|
|
* onUserSignupError
|
|
* @param response
|
|
*/
|
|
function onUserSignupError(response) {
|
|
Notification.error({message: response.data.message, title: '<i class="glyphicon glyphicon-remove"></i> Signup Error!', delay: 6000});
|
|
}
|
|
|
|
/**
|
|
* onUserSigninSuccess
|
|
* @param response
|
|
*/
|
|
function onUserSigninSuccess(response) {
|
|
// If successful we assign the response to the global user model
|
|
vm.authentication.user = response;
|
|
Notification.info({message: 'Welcome ' + response.firstName});
|
|
// And redirect to the previous or home page
|
|
$state.go($state.previous.state.name || 'home', $state.previous.params);
|
|
}
|
|
|
|
/**
|
|
* onUserSigninError
|
|
* @param response
|
|
*/
|
|
function onUserSigninError(response) {
|
|
Notification.error({message: response.data.message, title: '<i class="glyphicon glyphicon-remove"></i> Signin Error!', delay: 6000});
|
|
}
|
|
|
|
/**
|
|
* markLinkClick
|
|
* @param evt
|
|
* @param citem
|
|
*/
|
|
vm.markLinkClick = function (evt, citem) {
|
|
if (evt.originalEvent.srcElement.attributes.href.nodeValue === '/vip') {
|
|
evt.preventDefault();
|
|
$state.go('vip');
|
|
}
|
|
};
|
|
|
|
}
|
|
}());
|