Login OK!

This commit is contained in:
paolo
2018-10-26 14:50:50 +02:00
parent 1d42b184bb
commit 28eb3ff6a8
3 changed files with 162 additions and 168 deletions

View File

@@ -60,6 +60,7 @@
import * as types from '../../../store/mutation-types' import * as types from '../../../store/mutation-types'
import {Errori_MongoDb} from '../../../store/modules/user' import {Errori_MongoDb} from '../../../store/modules/user'
import {serv_constants} from "../../../store/modules/serv_constants";
import axios from 'axios'; import axios from 'axios';
@@ -116,7 +117,7 @@
//console.log("RIS = " + riscode); //console.log("RIS = " + riscode);
if (riscode === Errori_MongoDb.OK) { if (riscode === Errori_MongoDb.OK) {
this.$router.push('/'); this.$router.push('/');
}else if (riscode === Errori_MongoDb.ERR_LOGIN_ERRATO) { }else if (riscode === serv_constants.RIS_CODE_LOGIN_ERR) {
this.$q.notify(this.$t('login.errato')); this.$q.notify(this.$t('login.errato'));
this.$router.push('/signin'); this.$router.push('/signin');
} else { } else {
@@ -136,11 +137,6 @@
this.signin(this.form) this.signin(this.form)
.then((riscode) => { .then((riscode) => {
this.checkErrors(riscode); this.checkErrors(riscode);
if (riscode === Errori_MongoDb.OK){
this.$router.push('/');
}else{
this.$router.push('/signin');
}
}).catch(error => { }).catch(error => {
console.log("ERROR = " + error); console.log("ERROR = " + error);
}); });

View File

@@ -1,7 +1,8 @@
export const serv_constants = { export const serv_constants = {
RIS_CODE_EMAIL_ALREADY_VERIFIED: -20, RIS_CODE_EMAIL_ALREADY_VERIFIED: -5,
RIS_CODE_EMAIL_VERIFIED: 1, RIS_CODE_EMAIL_VERIFIED: 1,
RIS_CODE_LOGIN_ERR_GENERIC: -20,
RIS_CODE_LOGIN_ERR: -10, RIS_CODE_LOGIN_ERR: -10,
RIS_CODE_LOGIN_OK: 1, RIS_CODE_LOGIN_OK: 1,
}; };

View File

@@ -1,9 +1,8 @@
import Vue from 'vue' import Vue from 'vue'
import Vuex from 'vuex' import Vuex from 'vuex'
import md5 from 'js-md5';
const bcrypt = require('bcryptjs'); var bcrypt = require('bcryptjs');
Vue.use(Vuex); Vue.use(Vuex);
@@ -23,7 +22,6 @@ export const Errori_MongoDb = {
CALLING: 10, CALLING: 10,
OK: 20, OK: 20,
ERR_GENERICO: -1, ERR_GENERICO: -1,
ERR_LOGIN_ERRATO: -10,
DUPLICATE_EMAIL_ID: 11000, DUPLICATE_EMAIL_ID: 11000,
DUPLICATE_USERNAME_ID: 11100 DUPLICATE_USERNAME_ID: 11100
}; };
@@ -50,6 +48,7 @@ export const state = {
servercode: 0, servercode: 0,
idToken: 0, idToken: 0,
userId: 0, userId: 0,
username: '',
}; };
function sendRequest(url, method, mydata) { function sendRequest(url, method, mydata) {
@@ -71,12 +70,13 @@ function sendRequest(url, method, mydata) {
export const getters = { export const getters = {
getUser: state => state.user, getUser: state => state.user,
getUsername: state => state.user.username,
getPassword: state => state.user.password, getPassword: state => state.user.password,
getDateOfBirth: state => state.user.dateofbirth, getDateOfBirth: state => state.user.dateofbirth,
getUserServer: state => state.user.userServer, getUserServer: state => state.user.userServer,
getServerCode: state => state.servercode, getServerCode: state => state.servercode,
getLang: state => state.user.lang, getLang: state => state.user.lang,
getUsername: state => state.username,
}; };
@@ -84,9 +84,6 @@ export const mutations = {
[types.USER_REC]: (state, payload) => { [types.USER_REC]: (state, payload) => {
state.user = payload; state.user = payload;
}, },
[types.USER_USERNAME]: (state, payload) => {
state.user.username = payload;
},
[types.USER_PASSWORD]: (state, payload) => { [types.USER_PASSWORD]: (state, payload) => {
state.user.password = payload; state.user.password = payload;
}, },
@@ -100,10 +97,15 @@ export const mutations = {
state.user.dateOfBirth = payload; state.user.dateOfBirth = payload;
}, },
authUser(state, email, userid, mytoken) { [types.USER_USERNAME]: (state, payload) => {
state.user.tokens.push({access: "auth", token: mytoken}); state.username = payload;
state.idToken = mytoken; },
authUser(state, username, userid, mytoken) {
state.username = username;
state.userId = userid; state.userId = userid;
state.idToken = mytoken;
state.user.tokens.push({access: "auth", token: mytoken});
}, },
setUser(state, user) { setUser(state, user) {
state.userServer = user state.userServer = user
@@ -183,94 +185,89 @@ export const actions = {
var call = process.env.MONGODB_HOST + '/users'; var call = process.env.MONGODB_HOST + '/users';
console.log("CALL " + call); console.log("CALL " + call);
console.log("MYLANG = " + getlang()); console.log("PASSW: " + authData.password);
bcrypt.genSalt(10, (err, salt) => { bcrypt
return bcrypt .hash(authData.password, bcrypt.genSaltSync(12))
.hash(authData.password, salt) .then(hashedPassword => {
.then(hashedPassword => { console.log("NEW hashedPassword = " + hashedPassword);
let usertosend = { let usertosend = {
keyappid: process.env.PAO_APP_ID, keyappid: process.env.PAO_APP_ID,
lang: getlang(), lang: getlang(),
email: authData.email, email: authData.email,
password: hashedPassword, password: hashedPassword,
username: authData.username, username: authData.username,
idapp: process.env.APP_ID, idapp: process.env.APP_ID,
}; };
console.log(usertosend); console.log(usertosend);
var myres = null; var myres = null;
commit('setServerCode', Errori_MongoDb.CALLING); commit('setServerCode', Errori_MongoDb.CALLING);
var x_auth_token = null; return sendRequest(call, "POST", usertosend)
.then((res) => {
return sendRequest(call, "POST", usertosend) myres = res;
.then((res) => { var x_auth_token = res.headers.get('x-auth');
console.log("HEADERS:"); if (x_auth_token) {
for (let header of res.headers) {
console.log(header);
}
x_auth_token = res.headers.get('x-auth');
myres = res;
return res.json(); return res.json();
}) } else {
.then((body) => { return {status: 400, code: Errori_MongoDb.ERR_GENERICO}
}
})
.then((body) => {
if (process.env.DEV) {
console.log("RISULTATO ");
console.log("STATUS " + myres.status + " " + (myres.statusText));
console.log("BODY:");
console.log(body);
}
commit('setServerCode', myres);
commit('setUser', body);
if (myres.status === 200) {
var iduser = body._id;
var username = authData.username;
if (process.env.DEV) { if (process.env.DEV) {
console.log("RISULTATO "); console.log("USERNAME = " + username);
console.log("STATUS " + myres.status + " " + (myres.statusText)); console.log("IDUSER= " + iduser);
console.log("BODY:"); commit('authUser', username, iduser, x_auth_token);
console.log(body);
} }
commit('setServerCode', myres); const now = new Date();
commit('setUser', body); //const expirationDate = new Date(now.getTime() + myres.data.expiresIn * 1000);
const expirationDate = new Date(now.getTime() + 1000);
localStorage.setItem('username', username);
localStorage.setItem('token', x_auth_token);
localStorage.setItem('userId', iduser);
localStorage.setItem('expirationDate', expirationDate);
//dispatch('storeUser', authData);
//dispatch('setLogoutTimer', myres.data.expiresIn);
if (myres.status === 200) { return Errori_MongoDb.OK;
var iduser = body._id; } else if (myres.status === 404) {
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) { if (process.env.DEV) {
console.log("ERROREEEEEEEEE"); console.log("CODE = " + body.code);
console.log(error);
} }
commit('setServerCode', Errori_MongoDb.ERR_GENERICO); return body.code;
return Errori_MongoDb.ERR_GENERICO; } 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) => { [types.USER_SIGNIN]: ({commit}, authData) => {
@@ -279,92 +276,92 @@ export const actions = {
console.log("MYLANG = " + getlang()); console.log("MYLANG = " + getlang());
bcrypt.genSalt(10, (err, salt) => { console.log("PASSW: " + authData.password);
return bcrypt
.hash(authData.password, salt)
.then(hashedPassword => {
const usertosend = { const usertosend = {
username: authData.username, username: authData.username,
password: hashedPassword, password: authData.password,
idapp: process.env.APP_ID, idapp: process.env.APP_ID,
keyappid: process.env.PAO_APP_ID, keyappid: process.env.PAO_APP_ID,
lang: getlang(), lang: getlang(),
}; };
console.log(usertosend); console.log(usertosend);
var myres = null; var myres = null;
commit('setServerCode', Errori_MongoDb.CALLING); commit('setServerCode', Errori_MongoDb.CALLING);
var x_auth_token = null; var x_auth_token = null;
return sendRequest(call, "POST", usertosend) return sendRequest(call, "POST", usertosend)
.then((res) => { .then((res) => {
myres = res; myres = res;
return res.json(); x_auth_token = res.headers.get('x-auth');
}) var injson = 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) { if (x_auth_token || injson) {
commit('setServerCode', Errori_MongoDb.ERR_LOGIN_ERRATO); return injson;
return Errori_MongoDb.ERR_LOGIN_ERRATO; } else {
} return {status: 400, code: Errori_MongoDb.ERR_GENERICO}
}
})
.then((body) => {
if (process.env.DEV) {
console.log("RISULTATO ");
console.log("STATUS " + myres.status + " " + (myres.statusText));
console.log("BODY:");
console.log(body);
}
x_auth_token = body.token; if (body.code === serv_constants.RIS_CODE_LOGIN_ERR) {
commit('setServerCode', body.code);
return body.code;
}
commit('setServerCode', myres); commit('setServerCode', myres);
if (myres.status === 200) { if (myres.status === 200) {
var iduser = body._id; var iduser = body._id;
var email = body.email; var username = authData.username;
if (process.env.DEV) { if (process.env.DEV) {
console.log("EMAIL = " + email); console.log("USERNAME = " + username);
console.log("IDUSER= " + iduser); console.log("IDUSER= " + iduser);
commit('authUser', '', iduser, x_auth_token); commit('authUser', username, iduser, x_auth_token);
} }
const now = new Date(); const now = new Date();
//const expirationDate = new Date(now.getTime() + myres.data.expiresIn * 1000); //const expirationDate = new Date(now.getTime() + myres.data.expiresIn * 1000);
const expirationDate = new Date(now.getTime() + 1000); const expirationDate = new Date(now.getTime() + 1000);
localStorage.setItem('username', authData.username); localStorage.setItem('username', username);
localStorage.setItem('token', x_auth_token); localStorage.setItem('token', x_auth_token);
localStorage.setItem('userId', iduser); localStorage.setItem('userId', iduser);
localStorage.setItem('expirationDate', expirationDate); localStorage.setItem('expirationDate', expirationDate);
localStorage.setItem('isLoggedin', true); localStorage.setItem('isLoggedin', true);
//dispatch('storeUser', authData); //dispatch('storeUser', authData);
//dispatch('setLogoutTimer', myres.data.expiresIn); //dispatch('setLogoutTimer', myres.data.expiresIn);
return Errori_MongoDb.OK; return Errori_MongoDb.OK;
} else if (myres.status === 404) { } else if (myres.status === 404) {
if (process.env.DEV) { if (process.env.DEV) {
console.log("CODE = " + body.code); console.log("CODE = " + body.code);
} }
return body.code; return body.code;
} else { } else {
if (process.env.DEV) { if (process.env.DEV) {
console.log("CODE = " + body.code); console.log("CODE = " + body.code);
} }
return body.code; return body.code;
} }
}) })
.catch((error) => { .catch((error) => {
if (process.env.DEV) { if (process.env.DEV) {
console.log("ERROREEEEEEEEE"); console.log("ERROREEEEEEEEE");
console.log(error); console.log(error);
} }
commit('setServerCode', Errori_MongoDb.ERR_GENERICO); commit('setServerCode', Errori_MongoDb.ERR_GENERICO);
return Errori_MongoDb.ERR_GENERICO; return Errori_MongoDb.ERR_GENERICO;
}); });
});
});
}, },
[types.USER_AUTOLOGIN]: ({commit}) => { [types.USER_AUTOLOGIN]: ({commit}) => {
const token = localStorage.getItem('token'); const token = localStorage.getItem('token');
@@ -380,8 +377,8 @@ export const actions = {
const username = localStorage.getItem('username'); const username = localStorage.getItem('username');
commit('authUser', { commit('authUser', {
username: username, username: username,
userId: userId,
token: token, token: token,
userId: userId
}) })
}, },
[types.USER_LOGOUT]: ({commit}) => { [types.USER_LOGOUT]: ({commit}) => {