- updated SendReq

- If Server Down the login msg error corrected.
This commit is contained in:
Paolo Arena
2019-02-06 18:47:54 +01:00
parent cb941568e2
commit cb62d46048
11 changed files with 278 additions and 190 deletions

View File

@@ -3,6 +3,8 @@ import { storeBuilder } from './Store/Store'
import Vue from 'vue'
import translate from './../../globalroutines/util'
import urlBase64ToUint8Array from '../../js/utility'
import messages from '../../statics/i18n'
@@ -124,14 +126,17 @@ namespace Actions {
console.log('context', context)
const options = {
title: t('notification.title_subscribed'),
content: t('notification.subscribed'),
title: translate('notification.title_subscribed'),
content: translate('notification.subscribed'),
openUrl: '/'
}
let myres = {
options: { ...options },
subs: newSub
subs: newSub,
others: {
userId: UserStore.state.userId
}
}
return fetch(process.env.MONGODB_HOST + '/subscribe', {
@@ -158,7 +163,6 @@ namespace Actions {
})
return ris
}
function prova(context) {

View File

@@ -170,13 +170,11 @@ namespace Actions {
state.networkDataReceived = false
let ris = await Api.SendReq(call, UserStore.state.lang, token, 'GET', null)
.then((res) => {
return res.json()
}).then((resData) => {
.then(({resData, body}) => {
state.networkDataReceived = true
// console.log('******* UPDATE TODOS.STATE.TODOS !:', resData.todos)
state.todos = [...resData.todos]
state.todos = [...body.todos]
Todos.state.todos_changed++
console.log('state.todos', state.todos, 'checkPending', checkPending)
@@ -186,12 +184,8 @@ namespace Actions {
return rescodes.OK
})
.catch((error) => {
if (process.env.DEV) {
// console.log('dbLoadTodo ERRORE', error)
}
// If error network connection, take the data from IndexedDb
return rescodes.ERR_GENERICO
UserStore.mutations.setErrorCatch(error)
return UserStore.getters.getServerCode
})
console.log('fine della funz...')
@@ -266,30 +260,22 @@ namespace Actions {
const token = UserStore.state.idToken
let res = await Api.SendReq(call, UserStore.state.lang, token, method, itemtodo)
.then(function (response) {
if (response)
return response.json()
else
return null
}).then(newItem => {
console.log('RESDATA =', newItem)
if (newItem) {
const newId = newItem._id
.then(({res, body}) => {
console.log('RESDATA =', body)
if (body.newItem) {
const newId = body.newItem._id
// if (method === 'PATCH') {
// newItem = newItem.todo
// }
// Update ID on local
UpdateNewIdFromDB(itemtodo, newItem, method)
UpdateNewIdFromDB(itemtodo, body.newItem, method)
}
})
.catch((error) => {
if (process.env.DEV) {
console.log('ERRORE FETCH', 'dbInsertSaveTodo', method)
console.log(error)
}
return rescodes.ERR_GENERICO
UserStore.mutations.setErrorCatch(error)
return UserStore.getters.getServerCode
})
return res
@@ -302,7 +288,7 @@ namespace Actions {
const token = UserStore.state.idToken
let res = await Api.SendReq(call, UserStore.state.lang, token, 'DELETE', item)
.then(function (res) {
.then(function ({res, body}) {
// Delete Item in to Array
state.todos.splice(state.todos.indexOf(item), 1)
@@ -310,10 +296,8 @@ namespace Actions {
return rescodes.OK
})
.catch((error) => {
if (process.env.DEV) {
console.log('ERRORE FETCH', 'dbDeleteTodo')
}
return rescodes.ERR_GENERICO
UserStore.mutations.setErrorCatch(error)
return UserStore.getters.getServerCode
})
return res

View File

@@ -9,12 +9,14 @@ import { rescodes } from '../Modules/rescodes'
import { GlobalStore, UserStore, Todos } from '@store'
import globalroutines from './../../globalroutines/index'
import translate from './../../globalroutines/util'
const bcrypt = require('bcryptjs')
// State
const state: IUserState = {
userId: '',
email: '',
email: '',
username: '',
idapp: process.env.APP_ID,
password: '',
@@ -23,7 +25,9 @@ const state: IUserState = {
idToken: '',
tokens: [],
verifiedEmail: false,
categorySel: 'personal'
categorySel: 'personal',
servercode: 0,
x_auth_token: ''
}
@@ -52,15 +56,31 @@ namespace Getters {
}
}, 'tok')
const isServerError = b.read(state => {
return (state.servercode === rescodes.ERR_SERVERFETCH)
}, 'isServerError')
const getServerCode = b.read(state => {
return state.servercode
}, 'getServerCode')
export const getters = {
get lang() {
return lang()
},
get tok() {
return tok()
},
get isServerError() {
return isServerError()
},
get getServerCode() {
return getServerCode()
}
}
}
@@ -102,6 +122,10 @@ namespace Mutations {
state.servercode = num
}
function setAuth(state: IUserState, x_auth_token: string) {
state.x_auth_token = x_auth_token
}
function clearAuthData(state: IUserState) {
state.userId = ''
state.username = ''
@@ -111,6 +135,33 @@ namespace Mutations {
state.categorySel = 'personal'
}
function setErrorCatch(state: IUserState, err: number) {
if (state.servercode !== rescodes.ERR_SERVERFETCH) {
state.servercode = err
}
}
function getMsgError(state: IUserState, err: number) {
let msgerrore = ''
if (err !== rescodes.OK) {
msgerrore = 'Error [' + state.servercode + ']: '
if (state.servercode === rescodes.ERR_SERVERFETCH) {
msgerrore = translate('fetch.errore_server')
} else {
msgerrore = translate('fetch.errore_generico')
}
if (process.env.DEV) {
console.log('ERROREEEEEEEEE: ', msgerrore, ' (', err, ')')
}
}
// return { code: state.servercode, msg: msgerrore }
return msgerrore
}
export const mutations = {
authUser: b.commit(authUser),
setpassword: b.commit(setpassword),
@@ -118,16 +169,20 @@ namespace Mutations {
setlang: b.commit(setlang),
UpdatePwd: b.commit(UpdatePwd),
setServerCode: b.commit(setServerCode),
clearAuthData: b.commit(clearAuthData)
setAuth: b.commit(setAuth),
clearAuthData: b.commit(clearAuthData),
setErrorCatch: b.commit(setErrorCatch),
getMsgError: b.commit(getMsgError)
}
}
namespace Actions {
async function sendUserEdit(context, form: Object) {
try {
const {data} = await Api.postFormData('profile/edit', form)
const { data } = await Api.postFormData('profile/edit', form)
console.log(data)
// return new ApiSuccess({data})
@@ -136,7 +191,7 @@ namespace Actions {
}
}
async function resetpwd (context, paramquery: IUserState) {
async function resetpwd(context, paramquery: IUserState) {
let call = process.env.MONGODB_HOST + '/updatepwd'
console.log('CALL ' + call)
@@ -151,36 +206,18 @@ namespace Actions {
Mutations.mutations.setServerCode(rescodes.CALLING)
let myres
let x_auth_token: string = ''
return await Api.SendReq(call, state.lang, Getters.getters.tok, 'POST', usertosend)
.then((res) => {
console.log(res)
myres = res
x_auth_token = String(res.headers.get('x-auth'))
if (myres.status === 200) {
return myres.json()
}
Mutations.mutations.setServerCode(rescodes.ERR_GENERICO)
return { code: rescodes.ERR_GENERICO, msg: 'Errore: ' + myres.status, resetpwd: true }
})
.then((body) => {
Mutations.mutations.UpdatePwd({ idToken: x_auth_token })
localStorage.setItem(rescodes.localStorage.token, x_auth_token)
.then(({ res, body }) => {
return { code: body.code, msg: body.msg }
}).catch((err) => {
console.log('ERROR: ' + err)
Mutations.mutations.setServerCode(rescodes.ERR_GENERICO)
return { code: rescodes.ERR_GENERICO, msg: 'Errore' }
})
.catch((error) => {
UserStore.mutations.setErrorCatch(error)
return UserStore.getters.getServerCode
})
}
async function requestpwd (context, paramquery: IUserState) {
async function requestpwd(context, paramquery: IUserState) {
let call = process.env.MONGODB_HOST + '/requestnewpwd'
console.log('CALL ' + call)
@@ -194,30 +231,17 @@ namespace Actions {
Mutations.mutations.setServerCode(rescodes.CALLING)
let myres
return await Api.SendReq(call, state.lang, Getters.getters.tok, 'POST', usertosend)
.then((res) => {
console.log(res)
myres = res
if (myres.status === 200) {
return myres.json()
}
Mutations.mutations.setServerCode(rescodes.ERR_GENERICO)
return { code: rescodes.ERR_GENERICO, msg: 'Errore: ' + myres.status, resetpwd: true }
})
.then((body) => {
.then(({ res, body }) => {
return { code: body.code, msg: body.msg }
}).catch((err) => {
console.log('ERROR: ' + err)
Mutations.mutations.setServerCode(rescodes.ERR_GENERICO)
return { code: rescodes.ERR_GENERICO, msg: 'Errore' }
}).catch((error) => {
UserStore.mutations.setErrorCatch(error)
return UserStore.getters.getServerCode
})
}
async function vreg (context, paramquery: ILinkReg) {
async function vreg(context, paramquery: ILinkReg) {
let call = process.env.MONGODB_HOST + '/vreg'
console.log('CALL ' + call)
@@ -230,20 +254,8 @@ namespace Actions {
Mutations.mutations.setServerCode(rescodes.CALLING)
let myres
return await Api.SendReq(call, state.lang, Getters.getters.tok, 'POST', usertosend)
.then((res) => {
console.log(res)
myres = res
if (myres.status === 200) {
return myres.json()
}
Mutations.mutations.setServerCode(rescodes.ERR_GENERICO)
return { code: rescodes.ERR_GENERICO, msg: 'Errore: ' + myres.status }
})
.then((body) => {
.then(({ res, body }) => {
// console.log("RITORNO 2 ");
// mutations.setServerCode(myres);
if (body.code === serv_constants.RIS_CODE_EMAIL_VERIFIED) {
@@ -253,14 +265,13 @@ namespace Actions {
console.log('Risultato di vreg: ', body.code)
}
return { code: body.code, msg: body.msg }
}).catch((err) => {
console.log('ERROR: ' + err)
Mutations.mutations.setServerCode(rescodes.ERR_GENERICO)
return { code: rescodes.ERR_GENERICO, msg: 'Errore' }
}).catch((error) => {
UserStore.mutations.setErrorCatch(error)
return UserStore.getters.getServerCode
})
}
async function signup (context, authData: ISignupOptions) {
async function signup(context, authData: ISignupOptions) {
let call = process.env.MONGODB_HOST + '/users'
console.log('CALL ' + call)
@@ -289,16 +300,8 @@ namespace Actions {
let x_auth_token: string = ''
return Api.SendReq(call, state.lang, Getters.getters.tok, 'POST', usertosend)
.then((res) => {
.then(({ res, body }) => {
myres = res
x_auth_token = String(res.headers.get('x-auth'))
if (x_auth_token) {
return res.json()
} else {
return { status: 400, code: rescodes.ERR_GENERICO }
}
})
.then((body) => {
if (process.env.DEV) {
console.log('RISULTATO ')
console.log('STATUS ' + myres.status + ' ' + (myres.statusText))
@@ -348,17 +351,13 @@ namespace Actions {
}
})
.catch((error) => {
if (process.env.DEV) {
console.log('signup ERROREEEEEEEEE')
console.log(error)
}
Mutations.mutations.setServerCode(rescodes.ERR_GENERICO)
return rescodes.ERR_GENERICO
UserStore.mutations.setErrorCatch(error)
return UserStore.getters.getServerCode
})
})
}
async function signin (context, authData: ISigninOptions) {
async function signin(context, authData: ISigninOptions) {
let call = process.env.MONGODB_HOST + '/users/login'
console.log('LOGIN ' + call)
@@ -378,24 +377,12 @@ namespace Actions {
Mutations.mutations.setServerCode(rescodes.CALLING)
let x_auth_token: string = ''
return await Api.SendReq(call, state.lang, Getters.getters.tok, 'POST', usertosend)
.then((res) => {
.then(({ res, body }) => {
myres = res
x_auth_token = String(res.headers.get('x-auth'))
let injson = res.json()
if (x_auth_token || injson) {
return injson
} else {
return { status: 400, code: rescodes.ERR_GENERICO }
}
})
.then((body) => {
if (process.env.DEV) {
console.log('RISULTATO ')
console.log('STATUS ' + myres.status + ' ' + (myres.statusText))
console.log('STATUS ' + res.status + ' ' + (res.statusText))
console.log('BODY:')
console.log(body)
}
@@ -417,7 +404,7 @@ namespace Actions {
Mutations.mutations.authUser({
userId: userId,
username: username,
idToken: x_auth_token,
idToken: state.x_auth_token,
verifiedEmail: verifiedEmail
})
}
@@ -427,7 +414,7 @@ namespace Actions {
const expirationDate = new Date(now.getTime() * 1000)
localStorage.setItem(rescodes.localStorage.userId, userId)
localStorage.setItem(rescodes.localStorage.username, username)
localStorage.setItem(rescodes.localStorage.token, x_auth_token)
localStorage.setItem(rescodes.localStorage.token, state.x_auth_token)
localStorage.setItem(rescodes.localStorage.expirationDate, expirationDate.toString())
localStorage.setItem(rescodes.localStorage.isLogged, String(true))
localStorage.setItem(rescodes.localStorage.verifiedEmail, Number(verifiedEmail).toString())
@@ -450,15 +437,12 @@ namespace Actions {
}
})
.catch((error) => {
if (process.env.DEV) {
console.log('signin ERRORE', error)
}
Mutations.mutations.setServerCode(rescodes.ERR_GENERICO)
return rescodes.ERR_GENERICO
UserStore.mutations.setErrorCatch(error)
return UserStore.getters.getServerCode
})
}
async function logout (context) {
async function logout(context) {
let call = process.env.MONGODB_HOST + '/users/me/token'
console.log('CALL ' + call)
@@ -470,15 +454,14 @@ namespace Actions {
console.log(usertosend)
return await Api.SendReq(call, state.lang, Getters.getters.tok, 'DELETE', usertosend)
.then(
(res) => {
console.log(res)
}
).catch((err) => {
console.log('ERROR: ' + err)
}).then(() => {
Mutations.mutations.clearAuthData()
})
.then(({ res, body }) => {
console.log(res)
}).then(() => {
Mutations.mutations.clearAuthData()
}).catch((error) => {
UserStore.mutations.setErrorCatch(error)
return UserStore.getters.getServerCode
})
localStorage.removeItem(rescodes.localStorage.expirationDate)
localStorage.removeItem(rescodes.localStorage.token)
@@ -504,7 +487,7 @@ namespace Actions {
}
async function autologin (context) {
async function autologin(context) {
try {
console.log('*** Autologin ***')
// INIT

View File

@@ -1,7 +1,10 @@
export const rescodes = {
EMPTY: 0,
CALLING: 10,
OK: 20,
ERR_GENERICO: -1,
ERR_SERVERFETCH: -2,
ERR_AUTHENTICATION: -5,
DUPLICATE_EMAIL_ID: 11000,
DUPLICATE_USERNAME_ID: 11100,