- cleaned some code.
- routing offline - pushNotification
This commit is contained in:
@@ -12,6 +12,23 @@ import { UserStore } from '@store'
|
||||
import globalroutines from './../../globalroutines/index'
|
||||
|
||||
const allTables = ['todos', 'sync_todos', 'sync_todos_patch', 'delete_todos', 'config', 'swmsg']
|
||||
const allTablesAfterLogin = ['todos', 'sync_todos', 'sync_todos_patch', 'delete_todos', 'config', 'swmsg']
|
||||
|
||||
async function getstateConnSaved() {
|
||||
const config = await globalroutines(null, 'readall', 'config', null)
|
||||
if (config.length > 1) {
|
||||
return config[1].stateconn
|
||||
} else {
|
||||
return 'online'
|
||||
}
|
||||
}
|
||||
|
||||
let stateConnDefault = 'online'
|
||||
|
||||
getstateConnSaved()
|
||||
.then(conn => {
|
||||
stateConnDefault = conn
|
||||
})
|
||||
|
||||
const state: IGlobalState = {
|
||||
conta: 0,
|
||||
@@ -21,16 +38,17 @@ const state: IGlobalState = {
|
||||
mobileMode: false,
|
||||
menuCollapse: true,
|
||||
leftDrawerOpen: true,
|
||||
stateConnection: 'online',
|
||||
stateConnection: stateConnDefault,
|
||||
category: 'personal',
|
||||
posts: [],
|
||||
listatodo: [
|
||||
{namecat: 'personal', description: 'personal'},
|
||||
{namecat: 'work', description: 'work'},
|
||||
{namecat: 'shopping', description: 'shopping'}
|
||||
]
|
||||
{ namecat: 'personal', description: 'personal' },
|
||||
{ namecat: 'work', description: 'work' },
|
||||
{ namecat: 'shopping', description: 'shopping' }
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
const b = storeBuilder.module<IGlobalState>('GlobalModule', state)
|
||||
|
||||
// Getters
|
||||
@@ -51,6 +69,10 @@ namespace Getters {
|
||||
|
||||
get category() {
|
||||
return category()
|
||||
},
|
||||
|
||||
get isOnline() {
|
||||
return state.stateConnection === 'online'
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -95,6 +117,10 @@ namespace Actions {
|
||||
return
|
||||
}
|
||||
|
||||
if (!('PushManager' in window)) {
|
||||
return
|
||||
}
|
||||
|
||||
console.log('createPushSubscription')
|
||||
|
||||
let reg
|
||||
@@ -123,7 +149,12 @@ namespace Actions {
|
||||
// console.log('newSub', newSub)
|
||||
if (newSub) {
|
||||
saveNewSubscriptionToServer(context, newSub)
|
||||
mystate.isSubscribed = true
|
||||
.then(ris => {
|
||||
mystate.isSubscribed = true
|
||||
})
|
||||
.catch(e => {
|
||||
console.log('Error during Subscription!', e)
|
||||
})
|
||||
}
|
||||
return null
|
||||
})
|
||||
@@ -147,7 +178,8 @@ namespace Actions {
|
||||
options: { ...options },
|
||||
subs: newSub,
|
||||
others: {
|
||||
userId: UserStore.state.userId
|
||||
userId: UserStore.state.userId,
|
||||
access: UserStore.state.tokens[0].access
|
||||
}
|
||||
}
|
||||
|
||||
@@ -163,6 +195,20 @@ namespace Actions {
|
||||
|
||||
}
|
||||
|
||||
async function deleteSubscriptionToServer(context) {
|
||||
console.log('DeleteSubscriptionToServer: ')
|
||||
|
||||
return await fetch(process.env.MONGODB_HOST + '/subscribe/del', {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json',
|
||||
'x-auth': UserStore.state.x_auth_token
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
function t(params) {
|
||||
let msg = params.split('.')
|
||||
let lang = UserStore.state.lang
|
||||
@@ -186,17 +232,48 @@ namespace Actions {
|
||||
|
||||
}
|
||||
|
||||
async function clearDataAfterLogout (context) {
|
||||
async function clearDataAfterLogout(context) {
|
||||
console.log('clearDataAfterLogout')
|
||||
|
||||
// Clear all data from the IndexedDB
|
||||
allTables.forEach(table => {
|
||||
await allTables.forEach(table => {
|
||||
globalroutines(null, 'clearalldata', table, null)
|
||||
})
|
||||
|
||||
// REMOVE ALL SUBSCRIPTION
|
||||
console.log('REMOVE ALL SUBSCRIPTION...')
|
||||
await navigator.serviceWorker.ready.then(function(reg) {
|
||||
console.log('... Ready')
|
||||
reg.pushManager.getSubscription().then(function(subscription) {
|
||||
console.log(' Found Subscription...')
|
||||
subscription.unsubscribe().then(function(successful) {
|
||||
// You've successfully unsubscribed
|
||||
console.log('You\'ve successfully unsubscribed')
|
||||
}).catch(function(e) {
|
||||
// Unsubscription failed
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
await deleteSubscriptionToServer(context)
|
||||
|
||||
}
|
||||
|
||||
async function loadAfterLogin (context) {
|
||||
actions.clearDataAfterLogout()
|
||||
async function clearDataAfterLoginOnlyIfActiveConnection(context) {
|
||||
|
||||
// if (Getters.getters.isOnline) {
|
||||
// console.log('clearDataAfterLoginOnlyIfActiveConnection')
|
||||
// // Clear all data from the IndexedDB
|
||||
// allTablesAfterLogin.forEach(table => {
|
||||
// globalroutines(null, 'clearalldata', table, null)
|
||||
// })
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
|
||||
async function loadAfterLogin(context) {
|
||||
actions.clearDataAfterLoginOnlyIfActiveConnection()
|
||||
}
|
||||
|
||||
|
||||
@@ -205,6 +282,7 @@ namespace Actions {
|
||||
createPushSubscription: b.dispatch(createPushSubscription),
|
||||
loadAfterLogin: b.dispatch(loadAfterLogin),
|
||||
clearDataAfterLogout: b.dispatch(clearDataAfterLogout),
|
||||
clearDataAfterLoginOnlyIfActiveConnection: b.dispatch(clearDataAfterLoginOnlyIfActiveConnection),
|
||||
prova: b.dispatch(prova)
|
||||
}
|
||||
|
||||
|
||||
@@ -5,8 +5,8 @@ import Api from '@api'
|
||||
import { rescodes } from './rescodes'
|
||||
import { GlobalStore, Todos, UserStore } from '@store'
|
||||
import globalroutines from './../../globalroutines/index'
|
||||
import { Mutation } from "vuex-module-decorators"
|
||||
import { serv_constants } from "@src/store/Modules/serv_constants"
|
||||
import { Mutation } from 'vuex-module-decorators'
|
||||
import { serv_constants } from '@src/store/Modules/serv_constants'
|
||||
|
||||
|
||||
const state: ITodosState = {
|
||||
@@ -74,7 +74,7 @@ namespace Actions {
|
||||
let something = false
|
||||
|
||||
if ('serviceWorker' in navigator) {
|
||||
// console.log(' -------- sendSwMsgIfAvailable')
|
||||
console.log(' -------- sendSwMsgIfAvailable')
|
||||
|
||||
let count = await checkPendingMsg(null)
|
||||
if (count > 0) {
|
||||
@@ -86,19 +86,19 @@ namespace Actions {
|
||||
// let recclone = [...arr_recmsg]
|
||||
if (arr_recmsg.length > 0) {
|
||||
|
||||
console.log(' TROVATI MSG PENDENTI ! ORA LI MANDO: ', arr_recmsg)
|
||||
// console.log(' TROVATI MSG PENDENTI ! ORA LI MANDO: ', arr_recmsg)
|
||||
|
||||
// console.log('---------------------- 2) navigator (2) .serviceWorker.ready')
|
||||
|
||||
something = true
|
||||
for (let rec of arr_recmsg) {
|
||||
console.log(' .... sw.sync.register ( ', rec._id)
|
||||
if ('SyncManager' in window) {
|
||||
sw.sync.register(rec._id)
|
||||
} else {
|
||||
// console.log(' .... sw.sync.register ( ', rec._id)
|
||||
// if ('SyncManager' in window) {
|
||||
// sw.sync.register(rec._id)
|
||||
// } else {
|
||||
// #Todo ++ Alternative to SyncManager
|
||||
Api.syncAlternative(rec._id)
|
||||
}
|
||||
// }
|
||||
}
|
||||
return something
|
||||
}
|
||||
@@ -115,12 +115,10 @@ namespace Actions {
|
||||
|
||||
await aspettansec(1000)
|
||||
|
||||
// console.log('waitAndcheckPendingMsg')
|
||||
|
||||
return await checkPendingMsg(context)
|
||||
.then(ris => {
|
||||
if (ris) {
|
||||
console.log('ris = ', ris)
|
||||
console.log('risPending = ', ris)
|
||||
const result = sendSwMsgIfAvailable()
|
||||
.then(something => {
|
||||
if (something) {
|
||||
@@ -136,14 +134,30 @@ namespace Actions {
|
||||
async function waitAndRefreshData(context) {
|
||||
await aspettansec(3000)
|
||||
|
||||
// console.log('waitAndRefreshData')
|
||||
|
||||
return await dbLoadTodo(context, false)
|
||||
}
|
||||
|
||||
async function checkPendingMsg(context) {
|
||||
// console.log('checkPendingMsg')
|
||||
|
||||
const config = await globalroutines(null, 'readall', 'config', null)
|
||||
// console.log('config', config)
|
||||
|
||||
try {
|
||||
if (config) {
|
||||
if (config[1].stateconn !== undefined) {
|
||||
// console.log('config.stateconn', config[1].stateconn)
|
||||
|
||||
if (config[1].stateconn !== GlobalStore.state.stateConnection) {
|
||||
GlobalStore.mutations.setStateConnection(config[1].stateconn)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
|
||||
}
|
||||
|
||||
return new Promise(function (resolve, reject) {
|
||||
|
||||
/*
|
||||
@@ -173,45 +187,49 @@ namespace Actions {
|
||||
async function dbLoadTodo(context, checkPending: boolean = false) {
|
||||
console.log('dbLoadTodo', checkPending)
|
||||
|
||||
const token = UserStore.state.idToken
|
||||
if (UserStore.state.userId === '')
|
||||
return false // Login not made
|
||||
|
||||
let call = process.env.MONGODB_HOST + '/todos/' + UserStore.state.userId
|
||||
|
||||
state.networkDataReceived = false
|
||||
|
||||
let ris = await Api.SendReq(call, UserStore.state.lang, token, 'GET', null)
|
||||
.then(({ resData, body }) => {
|
||||
let ris = await Api.SendReq(call, 'GET', null)
|
||||
.then(({ resData, body, status }) => {
|
||||
state.networkDataReceived = true
|
||||
|
||||
// console.log('******* UPDATE TODOS.STATE.TODOS !:', resData.todos)
|
||||
state.todos = [...body.todos]
|
||||
Todos.mutations.setTodos_changed()
|
||||
if (body.todos) {
|
||||
state.todos = [...body.todos]
|
||||
Todos.mutations.setTodos_changed()
|
||||
}
|
||||
|
||||
console.log('********** resData', resData, 'state.todos', state.todos, 'checkPending', checkPending)
|
||||
|
||||
// After Login will store into the indexedDb...
|
||||
|
||||
return rescodes.OK
|
||||
return { status }
|
||||
})
|
||||
.catch((error) => {
|
||||
.catch(error => {
|
||||
console.log('error=', error)
|
||||
UserStore.mutations.setErrorCatch(error)
|
||||
return UserStore.getters.getServerCode
|
||||
return { status }
|
||||
})
|
||||
|
||||
console.log('ris FUNZ: ', ris.code, 'status', ris.status)
|
||||
console.log('ris : ', ris)
|
||||
console.log('ris STATUS: ', ris.status)
|
||||
|
||||
if (!Todos.state.networkDataReceived) {
|
||||
|
||||
if (ris.status === serv_constants.RIS_CODE__HTTP_FORBIDDEN_INVALID_TOKEN) {
|
||||
consolelogpao('UNAUTHORIZING... TOKEN EXPIRED... !! ')
|
||||
} else {
|
||||
consolelogpao('NETWORK UNREACHABLE ! (Error in fetch)', UserStore.getters.getServerCode, ris.code)
|
||||
consolelogpao('NETWORK UNREACHABLE ! (Error in fetch)', UserStore.getters.getServerCode, ris.status)
|
||||
}
|
||||
// Read all data from IndexedDB Store into Memory
|
||||
await updateArrayInMemory(context)
|
||||
} else {
|
||||
if (ris.code === rescodes.OK && checkPending) {
|
||||
if (ris.status === rescodes.OK && checkPending) {
|
||||
waitAndcheckPendingMsg(context)
|
||||
}
|
||||
}
|
||||
@@ -273,14 +291,15 @@ namespace Actions {
|
||||
console.log('dbInsertSaveTodo', itemtodo, method)
|
||||
let call = process.env.MONGODB_HOST + '/todos'
|
||||
|
||||
if (UserStore.state.userId === '')
|
||||
return false // Login not made
|
||||
|
||||
if (method !== 'POST')
|
||||
call += '/' + itemtodo._id
|
||||
|
||||
const token = UserStore.state.idToken
|
||||
|
||||
console.log('TODO TO SAVE: ', itemtodo)
|
||||
|
||||
let res = await Api.SendReq(call, UserStore.state.lang, token, method, itemtodo)
|
||||
let res = await Api.SendReq(call, method, itemtodo)
|
||||
.then(({ res, newItem }) => {
|
||||
console.log('dbInsertSaveTodo RIS =', newItem)
|
||||
if (newItem) {
|
||||
@@ -301,9 +320,10 @@ namespace Actions {
|
||||
// console.log('dbDeleteTodo', item)
|
||||
let call = process.env.MONGODB_HOST + '/todos/' + item._id
|
||||
|
||||
const token = UserStore.state.idToken
|
||||
if (UserStore.state.userId === '')
|
||||
return false // Login not made
|
||||
|
||||
let res = await Api.SendReq(call, UserStore.state.lang, token, 'DELETE', item)
|
||||
let res = await Api.SendReq(call, 'DELETE', item)
|
||||
.then(function ({ res, itemris }) {
|
||||
|
||||
if (res.status === 200) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import Api from '@api'
|
||||
import { ISignupOptions, ISigninOptions, IUserState } from 'model'
|
||||
import { ILinkReg, IResult, IIdToken } from 'model/other'
|
||||
import { ILinkReg, IResult, IIdToken, IToken } from 'model/other'
|
||||
import { storeBuilder } from './Store/Store'
|
||||
import router from '@router'
|
||||
|
||||
@@ -22,7 +22,6 @@ const state: IUserState = {
|
||||
password: '',
|
||||
lang: '',
|
||||
repeatPassword: '',
|
||||
idToken: '',
|
||||
tokens: [],
|
||||
verified_email: false,
|
||||
categorySel: 'personal',
|
||||
@@ -44,17 +43,17 @@ namespace Getters {
|
||||
}
|
||||
}, 'lang')
|
||||
|
||||
const tok = b.read(state => {
|
||||
if (state.tokens) {
|
||||
if (typeof state.tokens[0] !== 'undefined') {
|
||||
return state.tokens[0].token
|
||||
} else {
|
||||
return ''
|
||||
}
|
||||
} else {
|
||||
return ''
|
||||
}
|
||||
}, 'tok')
|
||||
// const tok = b.read(state => {
|
||||
// if (state.tokens) {
|
||||
// if (typeof state.tokens[0] !== 'undefined') {
|
||||
// return state.tokens[0].token
|
||||
// } else {
|
||||
// return ''
|
||||
// }
|
||||
// } else {
|
||||
// return ''
|
||||
// }
|
||||
// }, 'tok')
|
||||
|
||||
const isServerError = b.read(state => {
|
||||
return (state.servercode === rescodes.ERR_SERVERFETCH)
|
||||
@@ -68,9 +67,9 @@ namespace Getters {
|
||||
get lang() {
|
||||
return lang()
|
||||
},
|
||||
get tok() {
|
||||
return tok()
|
||||
},
|
||||
// get tok() {
|
||||
// return tok()
|
||||
// },
|
||||
get isServerError() {
|
||||
return isServerError()
|
||||
},
|
||||
@@ -87,13 +86,11 @@ namespace Mutations {
|
||||
function authUser(state, data: IUserState) {
|
||||
state.userId = data.userId
|
||||
state.username = data.username
|
||||
state.idToken = data.idToken
|
||||
state.verified_email = data.verified_email
|
||||
state.category = data.categorySel
|
||||
// @ts-ignore
|
||||
state.tokens = [
|
||||
{ access: 'auth ' + navigator.userAgent, token: data.idToken, date_login: new Date() }
|
||||
]
|
||||
resetArrToken(state.tokens)
|
||||
state.tokens.push({ access: 'auth ' + navigator.userAgent, token: state.x_auth_token, date_login: new Date() })
|
||||
console.log('state.tokens', state.tokens)
|
||||
}
|
||||
|
||||
function setpassword(state: IUserState, newstr: string) {
|
||||
@@ -110,11 +107,11 @@ namespace Mutations {
|
||||
}
|
||||
|
||||
function UpdatePwd(state: IUserState, data: IIdToken) {
|
||||
state.idToken = data.idToken
|
||||
state.x_auth_token = data.x_auth_token
|
||||
if (!state.tokens) {
|
||||
state.tokens = []
|
||||
}
|
||||
state.tokens.push({ access: 'auth ' + navigator.userAgent, token: data.idToken, data_login: new Date() })
|
||||
state.tokens.push({ access: 'auth ' + navigator.userAgent, token: data.x_auth_token, data_login: new Date() })
|
||||
}
|
||||
|
||||
function setServerCode(state: IUserState, num: number) {
|
||||
@@ -126,14 +123,27 @@ namespace Mutations {
|
||||
}
|
||||
|
||||
function setAuth(state: IUserState, x_auth_token: string) {
|
||||
|
||||
state.x_auth_token = x_auth_token
|
||||
}
|
||||
|
||||
|
||||
function resetArrToken(arrtokens) {
|
||||
if (!arrtokens.tokens) {
|
||||
arrtokens.tokens = []
|
||||
}
|
||||
|
||||
// Take only the others access (from others Browser)
|
||||
return arrtokens.filter((token: IToken) => {
|
||||
return token.access !== 'auth ' + navigator.userAgent
|
||||
})
|
||||
}
|
||||
|
||||
function clearAuthData(state: IUserState) {
|
||||
state.userId = ''
|
||||
state.username = ''
|
||||
state.tokens = []
|
||||
state.idToken = ''
|
||||
resetArrToken(state.tokens)
|
||||
state.x_auth_token = ''
|
||||
state.verified_email = false
|
||||
state.categorySel = 'personal'
|
||||
}
|
||||
@@ -211,7 +221,7 @@ namespace Actions {
|
||||
|
||||
Mutations.mutations.setServerCode(rescodes.CALLING)
|
||||
|
||||
return await Api.SendReq(call, state.lang, Getters.getters.tok, 'POST', usertosend, true)
|
||||
return await Api.SendReq(call, 'POST', usertosend, true)
|
||||
.then(({ res, body }) => {
|
||||
return { code: body.code, msg: body.msg }
|
||||
})
|
||||
@@ -236,7 +246,7 @@ namespace Actions {
|
||||
|
||||
Mutations.mutations.setServerCode(rescodes.CALLING)
|
||||
|
||||
return await Api.SendReq(call, state.lang, Getters.getters.tok, 'POST', usertosend)
|
||||
return await Api.SendReq(call, 'POST', usertosend)
|
||||
.then(({ res, body }) => {
|
||||
return { code: body.code, msg: body.msg }
|
||||
}).catch((error) => {
|
||||
@@ -259,7 +269,7 @@ namespace Actions {
|
||||
|
||||
Mutations.mutations.setServerCode(rescodes.CALLING)
|
||||
|
||||
return await Api.SendReq(call, state.lang, Getters.getters.tok, 'POST', usertosend)
|
||||
return await Api.SendReq(call, 'POST', usertosend)
|
||||
.then(({ res, body }) => {
|
||||
// console.log("RITORNO 2 ");
|
||||
// mutations.setServerCode(myres);
|
||||
@@ -302,16 +312,18 @@ namespace Actions {
|
||||
|
||||
Mutations.mutations.setServerCode(rescodes.CALLING)
|
||||
|
||||
let x_auth_token: string = ''
|
||||
|
||||
return Api.SendReq(call, state.lang, Getters.getters.tok, 'POST', usertosend)
|
||||
.then(({ res, newuser }) => {
|
||||
return Api.SendReq(call, 'POST', usertosend)
|
||||
.then(({ res, body }) => {
|
||||
myres = res
|
||||
|
||||
const newuser = body
|
||||
|
||||
console.log('newuser', newuser, 'body', body)
|
||||
|
||||
Mutations.mutations.setServerCode(myres.status)
|
||||
|
||||
if (myres.status === 200) {
|
||||
let userId = newuser.userId
|
||||
let userId = newuser._id
|
||||
let username = authData.username
|
||||
if (process.env.DEV) {
|
||||
console.log('USERNAME = ' + username)
|
||||
@@ -319,9 +331,8 @@ namespace Actions {
|
||||
}
|
||||
|
||||
Mutations.mutations.authUser({
|
||||
userId: userId,
|
||||
username: username,
|
||||
idToken: x_auth_token,
|
||||
userId,
|
||||
username,
|
||||
verified_email: false
|
||||
})
|
||||
|
||||
@@ -330,7 +341,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.verified_email, String(false))
|
||||
state.isLogged = true
|
||||
@@ -355,104 +366,105 @@ namespace Actions {
|
||||
|
||||
console.log('MYLANG = ' + state.lang)
|
||||
|
||||
const usertosend = {
|
||||
username: authData.username,
|
||||
password: authData.password,
|
||||
idapp: process.env.APP_ID,
|
||||
keyappid: process.env.PAO_APP_ID,
|
||||
lang: state.lang
|
||||
}
|
||||
await navigator.serviceWorker.ready
|
||||
.then(function (swreg) {
|
||||
const sub = swreg.pushManager.getSubscription()
|
||||
return sub
|
||||
})
|
||||
.then((swreg) => {
|
||||
|
||||
console.log(usertosend)
|
||||
|
||||
let myres: IResult
|
||||
|
||||
Mutations.mutations.setServerCode(rescodes.CALLING)
|
||||
|
||||
return await Api.SendReq(call, state.lang, Getters.getters.tok, 'POST', usertosend, true)
|
||||
.then(({ res, body }) => {
|
||||
myres = res
|
||||
if (res.code === serv_constants.RIS_CODE_LOGIN_ERR) {
|
||||
Mutations.mutations.setServerCode(body.code)
|
||||
return body.code
|
||||
const options = {
|
||||
title: translate('notification.title_subscribed'),
|
||||
content: translate('notification.subscribed'),
|
||||
openUrl: '/'
|
||||
}
|
||||
|
||||
Mutations.mutations.setServerCode(myres.status)
|
||||
const usertosend = {
|
||||
username: authData.username,
|
||||
password: authData.password,
|
||||
idapp: process.env.APP_ID,
|
||||
keyappid: process.env.PAO_APP_ID,
|
||||
lang: state.lang,
|
||||
subs: swreg,
|
||||
options
|
||||
}
|
||||
|
||||
if (myres.status === 200) {
|
||||
let myuser: IUserState = body.usertosend
|
||||
if (myuser) {
|
||||
let userId = myuser.userId
|
||||
let username = authData.username
|
||||
let verified_email = myuser.verified_email
|
||||
if (process.env.DEV) {
|
||||
console.log('USERNAME = ' + username)
|
||||
console.log('IDUSER= ' + userId)
|
||||
console.log('state.x_auth_token= ' + state.x_auth_token)
|
||||
Mutations.mutations.authUser({
|
||||
userId,
|
||||
username,
|
||||
idToken: state.x_auth_token,
|
||||
verified_email
|
||||
})
|
||||
console.log(usertosend)
|
||||
|
||||
Mutations.mutations.setServerCode(rescodes.CALLING)
|
||||
|
||||
return usertosend
|
||||
|
||||
}).then((usertosend) => {
|
||||
let myres: IResult
|
||||
|
||||
return Api.SendReq(call, 'POST', usertosend, true)
|
||||
.then(({ res, body }) => {
|
||||
myres = res
|
||||
if (res.code === serv_constants.RIS_CODE_LOGIN_ERR) {
|
||||
Mutations.mutations.setServerCode(body.code)
|
||||
return body.code
|
||||
}
|
||||
|
||||
const now = new Date()
|
||||
// const expirationDate = new Date(now.getTime() + myres.data.expiresIn * 1000);
|
||||
const expirationDate = new Date(now.getTime() * 1000)
|
||||
localStorage.setItem(rescodes.localStorage.userId, userId)
|
||||
localStorage.setItem(rescodes.localStorage.username, username)
|
||||
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.verified_email, String(verified_email))
|
||||
Mutations.mutations.setServerCode(myres.status)
|
||||
|
||||
setGlobal()
|
||||
if (myres.status === 200) {
|
||||
let myuser: IUserState = body.usertosend
|
||||
if (myuser) {
|
||||
let userId = myuser.userId
|
||||
let username = authData.username
|
||||
let verified_email = myuser.verified_email
|
||||
if (process.env.DEV) {
|
||||
console.log('USERNAME = ' + username)
|
||||
console.log('IDUSER= ' + userId)
|
||||
console.log('state.x_auth_token= ' + state.x_auth_token)
|
||||
}
|
||||
|
||||
// dispatch('storeUser', authData);
|
||||
// dispatch('setLogoutTimer', myres.data.expiresIn);
|
||||
return rescodes.OK
|
||||
} else {
|
||||
return rescodes.ERR_GENERICO
|
||||
}
|
||||
} else if (myres.status === serv_constants.RIS_CODE__HTTP_FORBIDDEN_INVALID_TOKEN) {
|
||||
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) => {
|
||||
UserStore.mutations.setErrorCatch(error)
|
||||
return UserStore.getters.getServerCode
|
||||
Mutations.mutations.authUser({
|
||||
userId,
|
||||
username,
|
||||
verified_email
|
||||
})
|
||||
|
||||
const now = new Date()
|
||||
// const expirationDate = new Date(now.getTime() + myres.data.expiresIn * 1000);
|
||||
const expirationDate = new Date(now.getTime() * 1000)
|
||||
localStorage.setItem(rescodes.localStorage.userId, userId)
|
||||
localStorage.setItem(rescodes.localStorage.username, username)
|
||||
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.verified_email, String(verified_email))
|
||||
|
||||
setGlobal(true)
|
||||
|
||||
// dispatch('storeUser', authData);
|
||||
// dispatch('setLogoutTimer', myres.data.expiresIn);
|
||||
return rescodes.OK
|
||||
} else {
|
||||
return rescodes.ERR_GENERICO
|
||||
}
|
||||
} else if (myres.status === serv_constants.RIS_CODE__HTTP_FORBIDDEN_INVALID_TOKEN) {
|
||||
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) => {
|
||||
UserStore.mutations.setErrorCatch(error)
|
||||
return UserStore.getters.getServerCode
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async function logout(context) {
|
||||
|
||||
let call = process.env.MONGODB_HOST + '/users/me/token'
|
||||
console.log('CALL ' + call)
|
||||
|
||||
let usertosend = {
|
||||
keyappid: process.env.PAO_APP_ID,
|
||||
idapp: process.env.APP_ID
|
||||
}
|
||||
|
||||
console.log(usertosend)
|
||||
return await Api.SendReq(call, state.lang, Getters.getters.tok, 'DELETE', usertosend)
|
||||
.then(({ res, body }) => {
|
||||
console.log(res)
|
||||
}).then(() => {
|
||||
Mutations.mutations.clearAuthData()
|
||||
}).catch((error) => {
|
||||
UserStore.mutations.setErrorCatch(error)
|
||||
return UserStore.getters.getServerCode
|
||||
})
|
||||
console.log('logout')
|
||||
|
||||
localStorage.removeItem(rescodes.localStorage.expirationDate)
|
||||
localStorage.removeItem(rescodes.localStorage.token)
|
||||
@@ -463,12 +475,33 @@ namespace Actions {
|
||||
localStorage.removeItem(rescodes.localStorage.verified_email)
|
||||
localStorage.removeItem(rescodes.localStorage.categorySel)
|
||||
|
||||
GlobalStore.actions.clearDataAfterLogout()
|
||||
await GlobalStore.actions.clearDataAfterLogout()
|
||||
|
||||
let call = process.env.MONGODB_HOST + '/users/me/token'
|
||||
console.log('CALL ' + call)
|
||||
|
||||
let usertosend = {
|
||||
keyappid: process.env.PAO_APP_ID,
|
||||
idapp: process.env.APP_ID
|
||||
}
|
||||
|
||||
console.log(usertosend)
|
||||
const riscall = await Api.SendReq(call, 'DELETE', usertosend)
|
||||
.then(({ res, body }) => {
|
||||
console.log(res)
|
||||
}).then(() => {
|
||||
Mutations.mutations.clearAuthData()
|
||||
}).catch((error) => {
|
||||
UserStore.mutations.setErrorCatch(error)
|
||||
return UserStore.getters.getServerCode
|
||||
})
|
||||
|
||||
return riscall
|
||||
|
||||
// this.$router.push('/signin')
|
||||
}
|
||||
|
||||
async function setGlobal() {
|
||||
async function setGlobal(loggedWithNetwork: boolean) {
|
||||
state.isLogged = true
|
||||
GlobalStore.mutations.setleftDrawerOpen(localStorage.getItem(rescodes.localStorage.leftDrawerOpen) === 'true')
|
||||
GlobalStore.mutations.setCategorySel(localStorage.getItem(rescodes.localStorage.categorySel))
|
||||
@@ -511,14 +544,15 @@ namespace Actions {
|
||||
|
||||
console.log('autologin userId', userId)
|
||||
|
||||
UserStore.mutations.setAuth(token)
|
||||
|
||||
Mutations.mutations.authUser({
|
||||
userId: userId,
|
||||
username: username,
|
||||
idToken: token,
|
||||
verified_email: verified_email
|
||||
})
|
||||
|
||||
await setGlobal()
|
||||
await setGlobal(false)
|
||||
|
||||
console.log('autologin userId STATE ', state.userId)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user