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,12 +185,12 @@ 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(),
@@ -204,19 +206,15 @@ export const actions = {
commit('setServerCode', Errori_MongoDb.CALLING); commit('setServerCode', Errori_MongoDb.CALLING);
var x_auth_token = null;
return sendRequest(call, "POST", usertosend) return sendRequest(call, "POST", usertosend)
.then((res) => { .then((res) => {
console.log("HEADERS:");
for (let header of res.headers) {
console.log(header);
}
x_auth_token = res.headers.get('x-auth');
myres = res; myres = res;
var x_auth_token = res.headers.get('x-auth');
if (x_auth_token) {
return res.json(); return res.json();
} else {
return {status: 400, code: Errori_MongoDb.ERR_GENERICO}
}
}) })
.then((body) => { .then((body) => {
if (process.env.DEV) { if (process.env.DEV) {
@@ -231,17 +229,17 @@ export const actions = {
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 = " + body.email); console.log("USERNAME = " + username);
console.log("IDUSER= " + iduser); console.log("IDUSER= " + iduser);
commit('authUser', email, 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);
@@ -270,7 +268,6 @@ export const actions = {
return Errori_MongoDb.ERR_GENERICO; return Errori_MongoDb.ERR_GENERICO;
}); });
}); });
});
}, },
[types.USER_SIGNIN]: ({commit}, authData) => { [types.USER_SIGNIN]: ({commit}, authData) => {
@@ -279,14 +276,11 @@ 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(),
@@ -303,7 +297,14 @@ export const actions = {
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();
if (x_auth_token || injson) {
return injson;
} else {
return {status: 400, code: Errori_MongoDb.ERR_GENERICO}
}
}) })
.then((body) => { .then((body) => {
if (process.env.DEV) { if (process.env.DEV) {
@@ -314,27 +315,25 @@ export const actions = {
} }
if (body.code === serv_constants.RIS_CODE_LOGIN_ERR) { if (body.code === serv_constants.RIS_CODE_LOGIN_ERR) {
commit('setServerCode', Errori_MongoDb.ERR_LOGIN_ERRATO); commit('setServerCode', body.code);
return Errori_MongoDb.ERR_LOGIN_ERRATO; return body.code;
} }
x_auth_token = body.token;
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);
@@ -363,8 +362,6 @@ export const actions = {
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}) => {