Login (1)
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
export const serv_constants = {
|
||||
RIS_CODE_EMAIL_ALREADY_VERIFIED: -20,
|
||||
RIS_CODE_EMAIL_VERIFIED: 1,
|
||||
|
||||
RIS_CODE_LOGIN_ERR: -10,
|
||||
RIS_CODE_LOGIN_OK: 1,
|
||||
};
|
||||
|
||||
@@ -1,12 +1,18 @@
|
||||
import Vue from 'vue'
|
||||
import Vuex from 'vuex'
|
||||
|
||||
import md5 from 'js-md5';
|
||||
|
||||
const bcrypt = require('bcryptjs');
|
||||
|
||||
Vue.use(Vuex);
|
||||
|
||||
import * as types from '../mutation-types'
|
||||
import {serv_constants} from "./serv_constants";
|
||||
|
||||
//import tools from '../../../tools/tools'
|
||||
|
||||
function getlang(){
|
||||
function getlang() {
|
||||
if (state.user.lang !== "")
|
||||
return state.user.lang;
|
||||
else
|
||||
@@ -17,6 +23,7 @@ export const Errori_MongoDb = {
|
||||
CALLING: 10,
|
||||
OK: 20,
|
||||
ERR_GENERICO: -1,
|
||||
ERR_LOGIN_ERRATO: -10,
|
||||
DUPLICATE_EMAIL_ID: 11000,
|
||||
DUPLICATE_USERNAME_ID: 11100
|
||||
};
|
||||
@@ -41,6 +48,8 @@ export const state = {
|
||||
},
|
||||
userServer: null,
|
||||
servercode: 0,
|
||||
idToken: 0,
|
||||
userId: 0,
|
||||
};
|
||||
|
||||
function sendRequest(url, method, mydata) {
|
||||
@@ -91,8 +100,10 @@ export const mutations = {
|
||||
state.user.dateOfBirth = payload;
|
||||
},
|
||||
|
||||
authUser(state, email, mytoken) {
|
||||
authUser(state, email, userid, mytoken) {
|
||||
state.user.tokens.push({access: "auth", token: mytoken});
|
||||
state.idToken = mytoken;
|
||||
state.userId = userid;
|
||||
},
|
||||
setUser(state, user) {
|
||||
state.userServer = user
|
||||
@@ -103,6 +114,8 @@ export const mutations = {
|
||||
},
|
||||
clearAuthData(state) {
|
||||
state.tokens = [];
|
||||
state.idToken = null;
|
||||
state.userId = null;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -131,18 +144,18 @@ export const actions = {
|
||||
var call = process.env.MONGODB_HOST + '/vreg';
|
||||
console.log("CALL " + call);
|
||||
|
||||
let params = {
|
||||
let usertosend = {
|
||||
keyappid: process.env.PAO_APP_ID,
|
||||
idapp: process.env.APP_ID,
|
||||
idlink: paramquery.idlink,
|
||||
};
|
||||
console.log(params);
|
||||
console.log(usertosend);
|
||||
|
||||
commit('setServerCode', Errori_MongoDb.CALLING);
|
||||
|
||||
var myres;
|
||||
|
||||
return sendRequest(call, "POST", params)
|
||||
return sendRequest(call, "POST", usertosend)
|
||||
.then((res) => {
|
||||
//console.log("RITORNO 1 ");
|
||||
console.log(res);
|
||||
@@ -170,85 +183,217 @@ export const actions = {
|
||||
var call = process.env.MONGODB_HOST + '/users';
|
||||
console.log("CALL " + call);
|
||||
|
||||
let params = {
|
||||
keyappid: process.env.PAO_APP_ID,
|
||||
lang: state.user.lang,
|
||||
email: authData.email,
|
||||
password: authData.password,
|
||||
username: authData.username,
|
||||
idapp: process.env.APP_ID,
|
||||
};
|
||||
console.log("MYLANG = " + getlang());
|
||||
|
||||
console.log(params);
|
||||
bcrypt.genSalt(10, (err, salt) => {
|
||||
return bcrypt
|
||||
.hash(authData.password, salt)
|
||||
.then(hashedPassword => {
|
||||
let usertosend = {
|
||||
keyappid: process.env.PAO_APP_ID,
|
||||
lang: getlang(),
|
||||
email: authData.email,
|
||||
password: hashedPassword,
|
||||
username: authData.username,
|
||||
idapp: process.env.APP_ID,
|
||||
};
|
||||
|
||||
var myres = null;
|
||||
console.log(usertosend);
|
||||
|
||||
commit('setServerCode', Errori_MongoDb.CALLING);
|
||||
var myres = null;
|
||||
|
||||
var x_auth_token = null;
|
||||
commit('setServerCode', Errori_MongoDb.CALLING);
|
||||
|
||||
return sendRequest(call, "POST", params)
|
||||
.then((res) => {
|
||||
console.log("HEADERS:");
|
||||
var x_auth_token = null;
|
||||
|
||||
for (let header of res.headers) {
|
||||
console.log(header);
|
||||
}
|
||||
return sendRequest(call, "POST", usertosend)
|
||||
.then((res) => {
|
||||
console.log("HEADERS:");
|
||||
|
||||
x_auth_token = res.headers.get('x-auth');
|
||||
myres = res;
|
||||
return res.json();
|
||||
})
|
||||
.then((body) => {
|
||||
if (process.env.DEV) {
|
||||
console.log("RISULTATO ");
|
||||
console.log("STATUS " + myres.status + " " + (myres.statusText));
|
||||
console.log("BODY:");
|
||||
console.log(body);
|
||||
}
|
||||
for (let header of res.headers) {
|
||||
console.log(header);
|
||||
}
|
||||
|
||||
commit('setServerCode', myres);
|
||||
commit('setUser', body);
|
||||
x_auth_token = res.headers.get('x-auth');
|
||||
myres = res;
|
||||
return res.json();
|
||||
})
|
||||
.then((body) => {
|
||||
if (process.env.DEV) {
|
||||
console.log("RISULTATO ");
|
||||
console.log("STATUS " + myres.status + " " + (myres.statusText));
|
||||
console.log("BODY:");
|
||||
console.log(body);
|
||||
}
|
||||
|
||||
if (myres.status === 200) {
|
||||
var iduser = body._id;
|
||||
var email = body.email;
|
||||
if (process.env.DEV) {
|
||||
console.log("EMAIL = " + body.email);
|
||||
console.log("IDUSER= " + iduser);
|
||||
commit('authUser', email, x_auth_token);
|
||||
}
|
||||
commit('setServerCode', myres);
|
||||
commit('setUser', body);
|
||||
|
||||
if (myres.status === 200) {
|
||||
var iduser = body._id;
|
||||
var email = body.email;
|
||||
if (process.env.DEV) {
|
||||
console.log("EMAIL = " + body.email);
|
||||
console.log("IDUSER= " + iduser);
|
||||
commit('authUser', email, iduser, x_auth_token);
|
||||
}
|
||||
|
||||
const now = new Date();
|
||||
//const expirationDate = new Date(now.getTime() + myres.data.expiresIn * 1000);
|
||||
const expirationDate = new Date(now.getTime() + 1000);
|
||||
localStorage.setItem('username', authData.username);
|
||||
localStorage.setItem('token', x_auth_token);
|
||||
localStorage.setItem('userId', iduser);
|
||||
localStorage.setItem('expirationDate', expirationDate);
|
||||
//dispatch('storeUser', authData);
|
||||
//dispatch('setLogoutTimer', myres.data.expiresIn);
|
||||
|
||||
return Errori_MongoDb.OK;
|
||||
} else if (myres.status === 404) {
|
||||
if (process.env.DEV) {
|
||||
console.log("CODE = " + body.code);
|
||||
}
|
||||
return body.code;
|
||||
} else {
|
||||
if (process.env.DEV) {
|
||||
console.log("CODE = " + body.code);
|
||||
}
|
||||
return body.code;
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
if (process.env.DEV) {
|
||||
console.log("ERROREEEEEEEEE");
|
||||
console.log(error);
|
||||
}
|
||||
commit('setServerCode', Errori_MongoDb.ERR_GENERICO);
|
||||
return Errori_MongoDb.ERR_GENERICO;
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
const now = new Date();
|
||||
//const expirationDate = new Date(now.getTime() + myres.data.expiresIn * 1000);
|
||||
const expirationDate = new Date(now.getTime() + 1000);
|
||||
localStorage.setItem('token', x_auth_token);
|
||||
localStorage.setItem('userId', iduser);
|
||||
localStorage.setItem('expirationDate', expirationDate);
|
||||
//dispatch('storeUser', authData);
|
||||
//dispatch('setLogoutTimer', myres.data.expiresIn);
|
||||
return Errori_MongoDb.OK;
|
||||
} else if (myres.status === 404) {
|
||||
if (process.env.DEV) {
|
||||
console.log("CODE = " + body.code);
|
||||
}
|
||||
return body.code;
|
||||
} else {
|
||||
if (process.env.DEV) {
|
||||
console.log("CODE = " + body.code);
|
||||
}
|
||||
return body.code;
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
if (process.env.DEV) {
|
||||
console.log("ERROREEEEEEEEE");
|
||||
console.log(error);
|
||||
}
|
||||
commit('setServerCode', Errori_MongoDb.ERR_GENERICO);
|
||||
return Errori_MongoDb.ERR_GENERICO;
|
||||
});
|
||||
},
|
||||
[types.USER_SIGNIN]: ({commit}, authData) => {
|
||||
var call = process.env.MONGODB_HOST + '/users/login';
|
||||
console.log("LOGIN " + call);
|
||||
|
||||
console.log("MYLANG = " + getlang());
|
||||
|
||||
bcrypt.genSalt(10, (err, salt) => {
|
||||
return bcrypt
|
||||
.hash(authData.password, salt)
|
||||
.then(hashedPassword => {
|
||||
|
||||
const usertosend = {
|
||||
username: authData.username,
|
||||
password: hashedPassword,
|
||||
idapp: process.env.APP_ID,
|
||||
keyappid: process.env.PAO_APP_ID,
|
||||
lang: getlang(),
|
||||
};
|
||||
|
||||
console.log(usertosend);
|
||||
|
||||
var myres = null;
|
||||
|
||||
commit('setServerCode', Errori_MongoDb.CALLING);
|
||||
|
||||
var x_auth_token = null;
|
||||
|
||||
return sendRequest(call, "POST", usertosend)
|
||||
.then((res) => {
|
||||
myres = res;
|
||||
return res.json();
|
||||
})
|
||||
.then((body) => {
|
||||
if (process.env.DEV) {
|
||||
console.log("RISULTATO ");
|
||||
console.log("STATUS " + myres.status + " " + (myres.statusText));
|
||||
console.log("BODY:");
|
||||
console.log(body);
|
||||
}
|
||||
|
||||
if (body.code === serv_constants.RIS_CODE_LOGIN_ERR) {
|
||||
commit('setServerCode', Errori_MongoDb.ERR_LOGIN_ERRATO);
|
||||
return Errori_MongoDb.ERR_LOGIN_ERRATO;
|
||||
}
|
||||
|
||||
x_auth_token = body.token;
|
||||
|
||||
commit('setServerCode', myres);
|
||||
|
||||
if (myres.status === 200) {
|
||||
var iduser = body._id;
|
||||
var email = body.email;
|
||||
if (process.env.DEV) {
|
||||
console.log("EMAIL = " + email);
|
||||
console.log("IDUSER= " + iduser);
|
||||
commit('authUser', '', iduser, x_auth_token);
|
||||
}
|
||||
|
||||
const now = new Date();
|
||||
//const expirationDate = new Date(now.getTime() + myres.data.expiresIn * 1000);
|
||||
const expirationDate = new Date(now.getTime() + 1000);
|
||||
localStorage.setItem('username', authData.username);
|
||||
localStorage.setItem('token', x_auth_token);
|
||||
localStorage.setItem('userId', iduser);
|
||||
localStorage.setItem('expirationDate', expirationDate);
|
||||
localStorage.setItem('isLoggedin', true);
|
||||
|
||||
//dispatch('storeUser', authData);
|
||||
//dispatch('setLogoutTimer', myres.data.expiresIn);
|
||||
return Errori_MongoDb.OK;
|
||||
} else if (myres.status === 404) {
|
||||
if (process.env.DEV) {
|
||||
console.log("CODE = " + body.code);
|
||||
}
|
||||
return body.code;
|
||||
} else {
|
||||
if (process.env.DEV) {
|
||||
console.log("CODE = " + body.code);
|
||||
}
|
||||
return body.code;
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
if (process.env.DEV) {
|
||||
console.log("ERROREEEEEEEEE");
|
||||
console.log(error);
|
||||
}
|
||||
commit('setServerCode', Errori_MongoDb.ERR_GENERICO);
|
||||
return Errori_MongoDb.ERR_GENERICO;
|
||||
});
|
||||
});
|
||||
});
|
||||
},
|
||||
[types.USER_AUTOLOGIN]: ({commit}) => {
|
||||
const token = localStorage.getItem('token');
|
||||
if (!token) {
|
||||
return
|
||||
}
|
||||
const expirationDate = localStorage.getItem('expirationDate');
|
||||
const now = new Date();
|
||||
if (now >= expirationDate) {
|
||||
return
|
||||
}
|
||||
const userId = localStorage.getItem('userId');
|
||||
const username = localStorage.getItem('username');
|
||||
commit('authUser', {
|
||||
username: username,
|
||||
token: token,
|
||||
userId: userId
|
||||
})
|
||||
},
|
||||
[types.USER_LOGOUT]: ({commit}) => {
|
||||
commit('clearAuthData');
|
||||
localStorage.removeItem('expirationDate');
|
||||
localStorage.removeItem('token');
|
||||
localStorage.removeItem('userId');
|
||||
localStorage.removeItem('username');
|
||||
localStorage.removeItem('isLoggedin');
|
||||
router.replace('/signin');
|
||||
},
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -17,6 +17,9 @@ export const USER_PASSWORD = 'password';
|
||||
export const USER_DATEOFBIRTH = 'dateofbirth';
|
||||
|
||||
export const USER_SIGNUP = 'signup';
|
||||
export const USER_SIGNIN = 'signin';
|
||||
export const USER_AUTOLOGIN = 'autologin';
|
||||
export const USER_LOGOUT = 'logout';
|
||||
export const USER_VREG = 'vreg';
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user