mirror of
https://github.com/pierre42100/ComunicWeb
synced 2024-11-22 12:09:21 +00:00
Added login system
This commit is contained in:
parent
48323ed9af
commit
82b009bdcc
@ -9,12 +9,23 @@
|
||||
*
|
||||
* @param {String} apiURI The URI to call in the API
|
||||
* @param {Object} params The params to include in request
|
||||
* @param {Boolean} requireLoginTokens Specify if login tokens are required or not
|
||||
* @param {Function} nextAction What to do next
|
||||
*/
|
||||
ComunicWeb.common.api.makeAPIrequest = function(apiURI, params, nextAction){
|
||||
ComunicWeb.common.api.makeAPIrequest = function(apiURI, params, requireLoginTokens, nextAction){
|
||||
//Prepare the request URL
|
||||
var requestURL = ComunicWeb.__config.apiURL + apiURI;
|
||||
|
||||
//Add login tokens to params if required
|
||||
if(requireLoginTokens){
|
||||
//Get login tokens
|
||||
tokens = ComunicWeb.user.loginTokens.getLoginTokens();
|
||||
|
||||
//Add tokens
|
||||
params.token1 = tokens.token1;
|
||||
params.token2 = tokens.token2;
|
||||
}
|
||||
|
||||
//Prepare data to send in request
|
||||
var count = 0;
|
||||
var datas = "";
|
||||
@ -27,7 +38,7 @@ ComunicWeb.common.api.makeAPIrequest = function(apiURI, params, nextAction){
|
||||
datas += encodeURIComponent(paramName) + "=" + encodeURIComponent(params[paramName]);
|
||||
|
||||
count++; //Increment counter
|
||||
}
|
||||
}
|
||||
|
||||
//Create request
|
||||
var apiXHR = new XMLHttpRequest();
|
||||
@ -38,10 +49,16 @@ ComunicWeb.common.api.makeAPIrequest = function(apiURI, params, nextAction){
|
||||
//We continue only if request is terminated
|
||||
if(apiXHR.readyState == 4){
|
||||
//Prepare result
|
||||
var result = {};
|
||||
var result = JSON.parse(apiXHR.responseText);
|
||||
|
||||
//We check if we got any error
|
||||
if(result.error){
|
||||
//Log error
|
||||
ComunicWeb.debug.logMessage("Got an error in a XHR request! " + result.toString());
|
||||
}
|
||||
|
||||
//We can do the next step
|
||||
nextAction(apiXHR.responseText);
|
||||
nextAction(result);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -21,12 +21,13 @@ ComunicWeb.common.error.submitError = function(errorLevel, errorMessage, errorCo
|
||||
"errorCode": errorCode,
|
||||
"errorData": errorData.toSource(),
|
||||
}
|
||||
var requireLoginToken = false;
|
||||
|
||||
//Not any next action for now
|
||||
nextAction = function(){};
|
||||
|
||||
//Send API request
|
||||
ComunicWeb.common.api.makeAPIrequest(apiURI, params, nextAction);
|
||||
ComunicWeb.common.api.makeAPIrequest(apiURI, params, requireLoginToken, nextAction);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -21,7 +21,7 @@ var ComunicWeb = {
|
||||
/**
|
||||
* Make an API request
|
||||
*/
|
||||
makeAPIrequest: function(apiURI, params, nextAction){},
|
||||
makeAPIrequest: function(apiURI, params, requireLoginTokens, nextAction){},
|
||||
},
|
||||
|
||||
/**
|
||||
@ -211,7 +211,47 @@ var ComunicWeb = {
|
||||
* Manage user login
|
||||
*/
|
||||
userLogin: {
|
||||
/**
|
||||
* @var {Boolean} Store user login state (true by default)
|
||||
*/
|
||||
__userLogin: true,
|
||||
|
||||
/**
|
||||
* @var {Integer} Store the current user ID
|
||||
*/
|
||||
__userID: 0,
|
||||
|
||||
|
||||
/**
|
||||
* Tell if user is logged in or not
|
||||
*/
|
||||
getUserLoginState: function(){},
|
||||
|
||||
/**
|
||||
* Get user ID (if logged in)
|
||||
*/
|
||||
getUserID: function(){},
|
||||
|
||||
/**
|
||||
* Try to get and store current user ID
|
||||
*/
|
||||
getCurrentUserId: function(afterGetCurrentUserID){},
|
||||
|
||||
/**
|
||||
* Refresh the user login state
|
||||
*/
|
||||
refreshLoginState: function(){},
|
||||
|
||||
/**
|
||||
* Try to login user
|
||||
*/
|
||||
loginUser: function(usermail, userpassword, permanentLogin, afterLogin){},
|
||||
|
||||
/**
|
||||
* Get user infos
|
||||
*/
|
||||
userInfos: {
|
||||
//TODO: implement
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
@ -10,7 +10,7 @@ ComunicWeb.user.loginTokens = {
|
||||
/**
|
||||
* Set User tokens
|
||||
*
|
||||
* @param {Object} tokens The token object
|
||||
* @param {Object} tokens The tokens object
|
||||
* @param {Type} storageType The token destination (local or session)
|
||||
*/
|
||||
setUserTokens: function(tokens, storageType){
|
||||
@ -64,15 +64,15 @@ ComunicWeb.user.loginTokens = {
|
||||
*/
|
||||
getLoginTokens: function(){
|
||||
//First, check in local storage
|
||||
if(localStorage.getItem("loginTokens") != "null"){
|
||||
if(localStorage.getItem("loginTokens") !== null){
|
||||
//Return localStorage login tokens
|
||||
var loginTokenString = localStorage.getItem("loginTokens")
|
||||
var loginTokenString = localStorage.getItem("loginTokens");
|
||||
}
|
||||
|
||||
//Then, check in session storage
|
||||
if(sessionStorage.getItem("loginTokens") != "null"){
|
||||
if(sessionStorage.getItem("loginTokens") !== null){
|
||||
//Return session storage login token
|
||||
var loginTokenString = sessionStorage.getItem("loginTokens")
|
||||
var loginTokenString = sessionStorage.getItem("loginTokens");
|
||||
}
|
||||
|
||||
//Check if we didn't get any login token
|
||||
|
19
assets/js/user/userInfos.js
Normal file
19
assets/js/user/userInfos.js
Normal file
@ -0,0 +1,19 @@
|
||||
/**
|
||||
* User informations functions
|
||||
*
|
||||
* @author Pierre HUBERT
|
||||
*/
|
||||
|
||||
ComunicWeb.user.getUserInfos = {
|
||||
|
||||
/**
|
||||
* @var {String} User infos cache
|
||||
*/
|
||||
|
||||
/**
|
||||
* Get user informations
|
||||
*
|
||||
* @param {String} userID User on which to make request (current to get connected user)
|
||||
*/
|
||||
//getUserInfos
|
||||
}
|
@ -5,5 +5,147 @@
|
||||
*/
|
||||
|
||||
ComunicWeb.user.userLogin = {
|
||||
|
||||
|
||||
/**
|
||||
* @var {Boolean} Store user login state (true by default)
|
||||
*/
|
||||
__userLogin: true,
|
||||
|
||||
/**
|
||||
* @var {Integer} Store the current user ID
|
||||
*/
|
||||
__userID: 0,
|
||||
|
||||
|
||||
/**
|
||||
* Tell if user is logged in or not
|
||||
*
|
||||
* @return {Boolean} Depend of the result
|
||||
*/
|
||||
getUserLoginState: function(){
|
||||
//Return login state
|
||||
return this.__userLogin;
|
||||
},
|
||||
|
||||
/**
|
||||
* Get user ID (if logged in)
|
||||
*
|
||||
* @return {String.Boolean} User ID or false if not logged in
|
||||
*/
|
||||
getUserID: function(){
|
||||
//If user is logged in
|
||||
if(this.getUserLoginState === true){
|
||||
//Return user ID
|
||||
return this.__userID;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Try to get and store current user ID
|
||||
*
|
||||
* @param {function} afterGetCurrentUserID What to do next
|
||||
* @return {Integer} 0 if it fails
|
||||
*/
|
||||
getCurrentUserId: function(afterGetCurrentUserID){
|
||||
//Prepare and make an API request
|
||||
var apiURI = "user/getCurrentUserID";
|
||||
var params = {};
|
||||
var requireLoginTokens = true;
|
||||
|
||||
//What to do after the request is completed
|
||||
var afterAPIrequest = function(result){
|
||||
//Check if we got any error
|
||||
if(result.error){
|
||||
//Set user ID to 0 (security purpose)
|
||||
ComunicWeb.user.userLogin.__userID = 0;
|
||||
|
||||
//Perform next action
|
||||
afterGetCurrentUserID(0);
|
||||
}
|
||||
else{
|
||||
//Update user ID
|
||||
ComunicWeb.user.userLogin.__userID = result.userID;
|
||||
|
||||
//Perform next action
|
||||
afterGetCurrentUserID(result.userID)
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
//Perform request
|
||||
ComunicWeb.common.api.makeAPIrequest(apiURI, params, requireLoginTokens, afterAPIrequest);
|
||||
},
|
||||
|
||||
/**
|
||||
* Refresh the user login state
|
||||
*/
|
||||
refreshLoginState: function(){
|
||||
//First, check if we have login tokens
|
||||
if(ComunicWeb.user.loginTokens.checkLoginTokens() !== true){
|
||||
//Specify the user isn't logged in
|
||||
this.__userLogin = false;
|
||||
this.__userID = 0;
|
||||
|
||||
//User not logged in
|
||||
return false;
|
||||
}
|
||||
|
||||
//Try to use tokens to get user infos
|
||||
var nextStep = function(userID){
|
||||
//We check received data
|
||||
if(userID == 0){
|
||||
//We consider user is not logged in
|
||||
ComunicWeb.user.userLogin.__userLogin = false;
|
||||
}
|
||||
};
|
||||
this.getCurrentUserId(nextStep);
|
||||
},
|
||||
|
||||
/**
|
||||
* Try to login user
|
||||
*
|
||||
* @param {String} usermail The mail of user
|
||||
* @param {String} userpassword The password of the user
|
||||
* @param {Boolean} permanentLogin Specify wether the login is permanent or not
|
||||
* @param {function} afterLogin What to do next
|
||||
*/
|
||||
loginUser: function(usermail, userpassword, permanentLogin, afterLogin){
|
||||
//Prepare and make an API request
|
||||
var apiURI = "user/connectUSER";
|
||||
var params = {
|
||||
userMail: usermail,
|
||||
userPassword: userpassword,
|
||||
};
|
||||
|
||||
//What to do after the request is completed
|
||||
var afterAPIrequest = function(result){
|
||||
//Prepare data return
|
||||
var loginstate = false;
|
||||
|
||||
//Check if there is a success message
|
||||
if(result.success){
|
||||
loginstate = true;
|
||||
|
||||
//Indicates user is logged in
|
||||
ComunicWeb.user.userLogin.__userLogin = true;
|
||||
|
||||
//Store tokens
|
||||
if(permanentLogin){
|
||||
var storageType = "local";
|
||||
}
|
||||
else {
|
||||
storageType = "session";
|
||||
}
|
||||
ComunicWeb.user.loginTokens.setUserTokens(result.tokens, storageType);
|
||||
}
|
||||
|
||||
//Perform next action
|
||||
afterLogin(loginstate);
|
||||
|
||||
};
|
||||
|
||||
//Perform request
|
||||
ComunicWeb.common.api.makeAPIrequest(apiURI, params, afterAPIrequest);
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user