2018-11-15 19:48:37 +01:00
|
|
|
import Api from '@api'
|
2018-12-06 20:07:51 +01:00
|
|
|
import { ISignupOptions, ISigninOptions, IUserState } from 'model'
|
2019-02-12 12:06:01 +01:00
|
|
|
import { ILinkReg, IResult, IIdToken, IToken } from 'model/other'
|
2018-11-17 21:07:07 +01:00
|
|
|
import { storeBuilder } from './Store/Store'
|
2018-11-17 20:32:28 +01:00
|
|
|
import router from '@router'
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2018-11-17 20:32:28 +01:00
|
|
|
import { serv_constants } from '../Modules/serv_constants'
|
|
|
|
|
import { rescodes } from '../Modules/rescodes'
|
2019-02-01 04:10:31 +01:00
|
|
|
import { GlobalStore, UserStore, Todos } from '@store'
|
2019-02-03 14:40:20 +01:00
|
|
|
import globalroutines from './../../globalroutines/index'
|
2018-11-15 19:48:37 +01:00
|
|
|
|
2019-02-06 18:47:54 +01:00
|
|
|
import translate from './../../globalroutines/util'
|
|
|
|
|
|
2018-11-17 20:32:28 +01:00
|
|
|
const bcrypt = require('bcryptjs')
|
2018-11-02 22:15:48 +01:00
|
|
|
|
2018-11-17 20:32:28 +01:00
|
|
|
// State
|
|
|
|
|
const state: IUserState = {
|
2019-01-14 22:40:30 +01:00
|
|
|
userId: '',
|
2019-02-06 18:47:54 +01:00
|
|
|
email: '',
|
2018-11-15 19:48:37 +01:00
|
|
|
username: '',
|
|
|
|
|
idapp: process.env.APP_ID,
|
|
|
|
|
password: '',
|
|
|
|
|
lang: '',
|
|
|
|
|
repeatPassword: '',
|
|
|
|
|
tokens: [],
|
2019-02-08 17:10:25 +01:00
|
|
|
verified_email: false,
|
2019-02-06 18:47:54 +01:00
|
|
|
categorySel: 'personal',
|
|
|
|
|
servercode: 0,
|
2019-02-08 17:10:25 +01:00
|
|
|
x_auth_token: ''
|
2018-11-15 19:48:37 +01:00
|
|
|
}
|
2018-11-02 20:10:45 +01:00
|
|
|
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2018-11-15 19:48:37 +01:00
|
|
|
const b = storeBuilder.module<IUserState>('UserModule', state)
|
|
|
|
|
const stateGetter = b.state()
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2018-11-15 19:48:37 +01:00
|
|
|
namespace Getters {
|
2018-11-26 00:53:05 +01:00
|
|
|
|
2018-12-22 23:30:02 +01:00
|
|
|
const lang = b.read(state => {
|
2018-11-15 19:48:37 +01:00
|
|
|
if (state.lang !== '') {
|
|
|
|
|
return state.lang
|
|
|
|
|
} else {
|
|
|
|
|
return process.env.LANG_DEFAULT
|
|
|
|
|
}
|
2018-12-22 23:30:02 +01:00
|
|
|
}, 'lang')
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2019-02-12 12:06:01 +01:00
|
|
|
// const tok = b.read(state => {
|
|
|
|
|
// if (state.tokens) {
|
|
|
|
|
// if (typeof state.tokens[0] !== 'undefined') {
|
|
|
|
|
// return state.tokens[0].token
|
|
|
|
|
// } else {
|
|
|
|
|
// return ''
|
|
|
|
|
// }
|
|
|
|
|
// } else {
|
|
|
|
|
// return ''
|
|
|
|
|
// }
|
|
|
|
|
// }, 'tok')
|
2018-11-15 19:48:37 +01:00
|
|
|
|
2019-02-06 18:47:54 +01:00
|
|
|
const isServerError = b.read(state => {
|
|
|
|
|
return (state.servercode === rescodes.ERR_SERVERFETCH)
|
|
|
|
|
}, 'isServerError')
|
|
|
|
|
|
|
|
|
|
const getServerCode = b.read(state => {
|
|
|
|
|
return state.servercode
|
|
|
|
|
}, 'getServerCode')
|
|
|
|
|
|
2018-11-15 19:48:37 +01:00
|
|
|
export const getters = {
|
|
|
|
|
get lang() {
|
|
|
|
|
return lang()
|
|
|
|
|
},
|
2019-02-12 12:06:01 +01:00
|
|
|
// get tok() {
|
|
|
|
|
// return tok()
|
|
|
|
|
// },
|
2019-02-06 18:47:54 +01:00
|
|
|
get isServerError() {
|
|
|
|
|
return isServerError()
|
|
|
|
|
},
|
|
|
|
|
get getServerCode() {
|
|
|
|
|
return getServerCode()
|
2019-02-08 17:10:25 +01:00
|
|
|
}
|
2018-11-15 19:48:37 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-06 18:47:54 +01:00
|
|
|
|
2018-11-15 19:48:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Mutations {
|
|
|
|
|
function authUser(state, data: IUserState) {
|
|
|
|
|
state.userId = data.userId
|
2019-01-14 22:40:30 +01:00
|
|
|
state.username = data.username
|
2019-02-08 17:10:25 +01:00
|
|
|
state.verified_email = data.verified_email
|
2019-01-30 01:05:31 +01:00
|
|
|
state.category = data.categorySel
|
2019-02-12 12:06:01 +01:00
|
|
|
resetArrToken(state.tokens)
|
2019-02-14 18:38:23 +01:00
|
|
|
state.tokens.push({ access: 'auth', token: state.x_auth_token, date_login: new Date() })
|
2019-02-13 18:48:30 +01:00
|
|
|
// console.log('state.tokens', state.tokens)
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
2018-11-15 19:48:37 +01:00
|
|
|
function setpassword(state: IUserState, newstr: string) {
|
|
|
|
|
state.password = newstr
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
2018-11-15 19:48:37 +01:00
|
|
|
function setemail(state: IUserState, newstr: string) {
|
|
|
|
|
state.email = newstr
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
2018-11-15 19:48:37 +01:00
|
|
|
function setlang(state: IUserState, newstr: string) {
|
|
|
|
|
state.lang = newstr
|
2019-01-02 01:58:47 +01:00
|
|
|
localStorage.setItem('lang', state.lang)
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
2018-11-15 19:48:37 +01:00
|
|
|
function UpdatePwd(state: IUserState, data: IIdToken) {
|
2019-02-12 12:06:01 +01:00
|
|
|
state.x_auth_token = data.x_auth_token
|
2018-11-15 19:48:37 +01:00
|
|
|
if (!state.tokens) {
|
|
|
|
|
state.tokens = []
|
|
|
|
|
}
|
2019-02-14 18:38:23 +01:00
|
|
|
state.tokens.push({ access: 'auth', token: data.x_auth_token, data_login: new Date() })
|
2018-11-15 19:48:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setServerCode(state: IUserState, num: number) {
|
|
|
|
|
state.servercode = num
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-09 18:04:49 +01:00
|
|
|
function setResStatus(state: IUserState, status: number) {
|
|
|
|
|
state.resStatus = status
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-06 18:47:54 +01:00
|
|
|
function setAuth(state: IUserState, x_auth_token: string) {
|
2019-02-12 12:06:01 +01:00
|
|
|
|
2019-02-06 18:47:54 +01:00
|
|
|
state.x_auth_token = x_auth_token
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-12 12:06:01 +01:00
|
|
|
|
|
|
|
|
function resetArrToken(arrtokens) {
|
|
|
|
|
if (!arrtokens.tokens) {
|
|
|
|
|
arrtokens.tokens = []
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Take only the others access (from others Browser)
|
|
|
|
|
return arrtokens.filter((token: IToken) => {
|
2019-02-14 18:38:23 +01:00
|
|
|
return token.access !== 'auth'
|
2019-02-12 12:06:01 +01:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-15 19:48:37 +01:00
|
|
|
function clearAuthData(state: IUserState) {
|
2019-01-14 22:40:30 +01:00
|
|
|
state.userId = ''
|
2018-11-15 19:48:37 +01:00
|
|
|
state.username = ''
|
2019-02-12 12:06:01 +01:00
|
|
|
resetArrToken(state.tokens)
|
|
|
|
|
state.x_auth_token = ''
|
2019-02-08 17:10:25 +01:00
|
|
|
state.verified_email = false
|
2019-01-30 01:05:31 +01:00
|
|
|
state.categorySel = 'personal'
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-06 18:47:54 +01:00
|
|
|
|
|
|
|
|
function setErrorCatch(state: IUserState, err: number) {
|
|
|
|
|
if (state.servercode !== rescodes.ERR_SERVERFETCH) {
|
|
|
|
|
state.servercode = err
|
|
|
|
|
}
|
2019-02-11 02:58:53 +01:00
|
|
|
console.log('Err catch: (servercode:', err, ')')
|
2019-02-06 18:47:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-11-15 19:48:37 +01:00
|
|
|
export const mutations = {
|
|
|
|
|
authUser: b.commit(authUser),
|
|
|
|
|
setpassword: b.commit(setpassword),
|
|
|
|
|
setemail: b.commit(setemail),
|
|
|
|
|
setlang: b.commit(setlang),
|
|
|
|
|
UpdatePwd: b.commit(UpdatePwd),
|
|
|
|
|
setServerCode: b.commit(setServerCode),
|
2019-02-09 18:04:49 +01:00
|
|
|
setResStatus: b.commit(setResStatus),
|
2019-02-06 18:47:54 +01:00
|
|
|
setAuth: b.commit(setAuth),
|
|
|
|
|
clearAuthData: b.commit(clearAuthData),
|
|
|
|
|
setErrorCatch: b.commit(setErrorCatch),
|
|
|
|
|
getMsgError: b.commit(getMsgError)
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-06 18:47:54 +01:00
|
|
|
|
2018-11-15 19:48:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace Actions {
|
|
|
|
|
|
|
|
|
|
async function sendUserEdit(context, form: Object) {
|
|
|
|
|
try {
|
2019-02-06 18:47:54 +01:00
|
|
|
const { data } = await Api.postFormData('profile/edit', form)
|
2018-11-15 19:48:37 +01:00
|
|
|
console.log(data)
|
|
|
|
|
// return new ApiSuccess({data})
|
|
|
|
|
|
|
|
|
|
} catch {
|
|
|
|
|
// return new ApiError()
|
|
|
|
|
}
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-06 18:47:54 +01:00
|
|
|
async function resetpwd(context, paramquery: IUserState) {
|
2018-11-02 22:15:48 +01:00
|
|
|
let call = process.env.MONGODB_HOST + '/updatepwd'
|
|
|
|
|
console.log('CALL ' + call)
|
2018-11-02 20:10:45 +01:00
|
|
|
|
|
|
|
|
let usertosend = {
|
|
|
|
|
keyappid: process.env.PAO_APP_ID,
|
|
|
|
|
idapp: process.env.APP_ID,
|
|
|
|
|
email: paramquery.email,
|
|
|
|
|
password: paramquery.password,
|
|
|
|
|
tokenforgot: paramquery.tokenforgot
|
2018-11-02 22:15:48 +01:00
|
|
|
}
|
|
|
|
|
console.log(usertosend)
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2018-11-17 20:32:28 +01:00
|
|
|
Mutations.mutations.setServerCode(rescodes.CALLING)
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2019-02-12 12:06:01 +01:00
|
|
|
return await Api.SendReq(call, 'POST', usertosend, true)
|
2019-02-06 18:47:54 +01:00
|
|
|
.then(({ res, body }) => {
|
2018-11-02 22:15:48 +01:00
|
|
|
return { code: body.code, msg: body.msg }
|
2019-02-06 18:47:54 +01:00
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
UserStore.mutations.setErrorCatch(error)
|
2019-02-12 19:09:43 +01:00
|
|
|
return { code: UserStore.getters.getServerCode, msg: error }
|
2018-11-02 22:15:48 +01:00
|
|
|
})
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2019-02-06 18:47:54 +01:00
|
|
|
async function requestpwd(context, paramquery: IUserState) {
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2018-11-02 22:15:48 +01:00
|
|
|
let call = process.env.MONGODB_HOST + '/requestnewpwd'
|
|
|
|
|
console.log('CALL ' + call)
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2018-11-02 20:10:45 +01:00
|
|
|
let usertosend = {
|
|
|
|
|
keyappid: process.env.PAO_APP_ID,
|
|
|
|
|
idapp: process.env.APP_ID,
|
|
|
|
|
email: paramquery.email
|
2018-11-02 22:15:48 +01:00
|
|
|
}
|
|
|
|
|
console.log(usertosend)
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2018-11-17 20:32:28 +01:00
|
|
|
Mutations.mutations.setServerCode(rescodes.CALLING)
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2019-02-12 12:06:01 +01:00
|
|
|
return await Api.SendReq(call, 'POST', usertosend)
|
2019-02-06 18:47:54 +01:00
|
|
|
.then(({ res, body }) => {
|
2018-11-02 22:15:48 +01:00
|
|
|
return { code: body.code, msg: body.msg }
|
2019-02-06 18:47:54 +01:00
|
|
|
}).catch((error) => {
|
|
|
|
|
UserStore.mutations.setErrorCatch(error)
|
|
|
|
|
return UserStore.getters.getServerCode
|
2018-11-02 22:15:48 +01:00
|
|
|
})
|
2018-11-02 20:10:45 +01:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-06 18:47:54 +01:00
|
|
|
async function vreg(context, paramquery: ILinkReg) {
|
2018-11-02 22:15:48 +01:00
|
|
|
let call = process.env.MONGODB_HOST + '/vreg'
|
|
|
|
|
console.log('CALL ' + call)
|
2018-11-02 20:10:45 +01:00
|
|
|
|
|
|
|
|
let usertosend = {
|
|
|
|
|
keyappid: process.env.PAO_APP_ID,
|
|
|
|
|
idapp: process.env.APP_ID,
|
2019-01-02 01:58:47 +01:00
|
|
|
idlink: paramquery.idlink
|
2018-11-02 22:15:48 +01:00
|
|
|
}
|
|
|
|
|
console.log(usertosend)
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2018-11-17 20:32:28 +01:00
|
|
|
Mutations.mutations.setServerCode(rescodes.CALLING)
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2019-02-12 12:06:01 +01:00
|
|
|
return await Api.SendReq(call, 'POST', usertosend)
|
2019-02-06 18:47:54 +01:00
|
|
|
.then(({ res, body }) => {
|
2018-11-02 20:10:45 +01:00
|
|
|
// console.log("RITORNO 2 ");
|
2018-11-15 19:48:37 +01:00
|
|
|
// mutations.setServerCode(myres);
|
2018-11-02 20:10:45 +01:00
|
|
|
if (body.code === serv_constants.RIS_CODE_EMAIL_VERIFIED) {
|
2019-01-02 18:01:36 +01:00
|
|
|
console.log('VERIFICATO !!')
|
2019-02-09 18:04:49 +01:00
|
|
|
localStorage.setItem(rescodes.localStorage.verified_email, String(true))
|
2019-01-02 18:01:36 +01:00
|
|
|
} else {
|
|
|
|
|
console.log('Risultato di vreg: ', body.code)
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
2018-11-02 22:15:48 +01:00
|
|
|
return { code: body.code, msg: body.msg }
|
2019-02-06 18:47:54 +01:00
|
|
|
}).catch((error) => {
|
|
|
|
|
UserStore.mutations.setErrorCatch(error)
|
|
|
|
|
return UserStore.getters.getServerCode
|
2018-11-02 22:15:48 +01:00
|
|
|
})
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-06 18:47:54 +01:00
|
|
|
async function signup(context, authData: ISignupOptions) {
|
2018-11-02 22:15:48 +01:00
|
|
|
let call = process.env.MONGODB_HOST + '/users'
|
|
|
|
|
console.log('CALL ' + call)
|
2018-11-02 20:10:45 +01:00
|
|
|
|
|
|
|
|
// console.log("PASSW: " + authData.password);
|
|
|
|
|
|
2018-11-15 19:48:37 +01:00
|
|
|
let mylang = state.lang
|
2018-11-12 18:08:06 +01:00
|
|
|
console.log('MYLANG: ' + mylang)
|
|
|
|
|
|
2018-11-02 20:10:45 +01:00
|
|
|
return bcrypt.hash(authData.password, bcrypt.genSaltSync(12))
|
|
|
|
|
.then((hashedPassword: string) => {
|
2018-11-02 15:56:29 +01:00
|
|
|
let usertosend = {
|
2018-11-02 20:10:45 +01:00
|
|
|
keyappid: process.env.PAO_APP_ID,
|
2018-11-12 18:08:06 +01:00
|
|
|
lang: mylang,
|
2018-11-02 20:10:45 +01:00
|
|
|
email: authData.email,
|
|
|
|
|
password: String(hashedPassword),
|
|
|
|
|
username: authData.username,
|
|
|
|
|
idapp: process.env.APP_ID
|
2018-11-02 22:15:48 +01:00
|
|
|
}
|
2018-11-02 20:10:45 +01:00
|
|
|
|
2018-11-02 22:15:48 +01:00
|
|
|
console.log(usertosend)
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2018-11-02 22:15:48 +01:00
|
|
|
let myres: IResult
|
2018-11-02 20:10:45 +01:00
|
|
|
|
2018-11-17 20:32:28 +01:00
|
|
|
Mutations.mutations.setServerCode(rescodes.CALLING)
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2019-02-12 12:06:01 +01:00
|
|
|
return Api.SendReq(call, 'POST', usertosend)
|
|
|
|
|
.then(({ res, body }) => {
|
2018-11-02 22:15:48 +01:00
|
|
|
myres = res
|
2018-11-02 20:10:45 +01:00
|
|
|
|
2019-02-12 12:06:01 +01:00
|
|
|
const newuser = body
|
|
|
|
|
|
2019-02-13 18:48:30 +01:00
|
|
|
console.log('newuser', newuser)
|
2019-02-12 12:06:01 +01:00
|
|
|
|
2018-11-15 19:48:37 +01:00
|
|
|
Mutations.mutations.setServerCode(myres.status)
|
2018-11-02 20:10:45 +01:00
|
|
|
|
|
|
|
|
if (myres.status === 200) {
|
2019-02-12 12:06:01 +01:00
|
|
|
let userId = newuser._id
|
2018-11-02 22:15:48 +01:00
|
|
|
let username = authData.username
|
2018-11-02 20:10:45 +01:00
|
|
|
if (process.env.DEV) {
|
2018-11-02 22:15:48 +01:00
|
|
|
console.log('USERNAME = ' + username)
|
2019-01-14 22:40:30 +01:00
|
|
|
console.log('IDUSER= ' + userId)
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
2018-11-15 19:48:37 +01:00
|
|
|
Mutations.mutations.authUser({
|
2019-02-12 12:06:01 +01:00
|
|
|
userId,
|
|
|
|
|
username,
|
2019-02-08 17:10:25 +01:00
|
|
|
verified_email: false
|
2018-11-02 22:15:48 +01:00
|
|
|
})
|
2018-11-02 20:10:45 +01:00
|
|
|
|
2018-11-02 22:15:48 +01:00
|
|
|
const now = new Date()
|
2018-11-02 20:10:45 +01:00
|
|
|
// const expirationDate = new Date(now.getTime() + myres.data.expiresIn * 1000);
|
2019-01-02 18:01:36 +01:00
|
|
|
const expirationDate = new Date(now.getTime() * 1000)
|
2019-01-14 22:40:30 +01:00
|
|
|
localStorage.setItem(rescodes.localStorage.userId, userId)
|
2019-01-05 20:11:41 +01:00
|
|
|
localStorage.setItem(rescodes.localStorage.username, username)
|
2019-02-12 12:06:01 +01:00
|
|
|
localStorage.setItem(rescodes.localStorage.token, state.x_auth_token)
|
2019-01-05 20:11:41 +01:00
|
|
|
localStorage.setItem(rescodes.localStorage.expirationDate, expirationDate.toString())
|
2019-02-09 18:04:49 +01:00
|
|
|
localStorage.setItem(rescodes.localStorage.verified_email, String(false))
|
2019-02-07 00:53:10 +01:00
|
|
|
state.isLogged = true
|
2018-11-02 20:10:45 +01:00
|
|
|
// dispatch('storeUser', authData);
|
|
|
|
|
// dispatch('setLogoutTimer', myres.data.expiresIn);
|
|
|
|
|
|
2018-11-17 20:32:28 +01:00
|
|
|
return rescodes.OK
|
2018-11-02 20:10:45 +01:00
|
|
|
} else {
|
2019-02-08 17:10:25 +01:00
|
|
|
return rescodes.ERR_GENERICO
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
2019-02-06 18:47:54 +01:00
|
|
|
UserStore.mutations.setErrorCatch(error)
|
|
|
|
|
return UserStore.getters.getServerCode
|
2018-11-02 22:15:48 +01:00
|
|
|
})
|
|
|
|
|
})
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-06 18:47:54 +01:00
|
|
|
async function signin(context, authData: ISigninOptions) {
|
2018-11-02 22:15:48 +01:00
|
|
|
let call = process.env.MONGODB_HOST + '/users/login'
|
|
|
|
|
console.log('LOGIN ' + call)
|
2018-11-02 20:10:45 +01:00
|
|
|
|
2018-11-15 19:48:37 +01:00
|
|
|
console.log('MYLANG = ' + state.lang)
|
2018-11-02 20:10:45 +01:00
|
|
|
|
2019-02-13 18:48:30 +01:00
|
|
|
let sub = null
|
|
|
|
|
|
2019-02-14 18:38:23 +01:00
|
|
|
if ('serviceWorker' in navigator) {
|
|
|
|
|
sub = await navigator.serviceWorker.ready
|
|
|
|
|
.then(function (swreg) {
|
2019-02-14 20:08:22 +01:00
|
|
|
let sub = swreg.pushManager.getSubscription()
|
2019-02-14 18:38:23 +01:00
|
|
|
return sub
|
|
|
|
|
})
|
|
|
|
|
.catch(e => {
|
|
|
|
|
sub = null
|
|
|
|
|
})
|
|
|
|
|
}
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2019-02-13 18:48:30 +01:00
|
|
|
const options = {
|
|
|
|
|
title: translate('notification.title_subscribed'),
|
|
|
|
|
content: translate('notification.subscribed'),
|
|
|
|
|
openUrl: '/'
|
|
|
|
|
}
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2019-02-13 18:48:30 +01:00
|
|
|
const usertosend = {
|
|
|
|
|
username: authData.username,
|
|
|
|
|
password: authData.password,
|
|
|
|
|
idapp: process.env.APP_ID,
|
|
|
|
|
keyappid: process.env.PAO_APP_ID,
|
|
|
|
|
lang: state.lang,
|
|
|
|
|
subs: sub,
|
|
|
|
|
options
|
|
|
|
|
}
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2019-02-13 18:48:30 +01:00
|
|
|
console.log(usertosend)
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2019-02-13 18:48:30 +01:00
|
|
|
Mutations.mutations.setServerCode(rescodes.CALLING)
|
2019-02-12 12:06:01 +01:00
|
|
|
|
2019-02-13 18:48:30 +01:00
|
|
|
let myres: IResult
|
2019-02-12 12:06:01 +01:00
|
|
|
|
2019-02-13 18:48:30 +01:00
|
|
|
return Api.SendReq(call, 'POST', usertosend, true)
|
|
|
|
|
.then(({ res, body }) => {
|
|
|
|
|
myres = res
|
2018-11-02 20:10:45 +01:00
|
|
|
|
2019-02-13 18:48:30 +01:00
|
|
|
if (body.code === serv_constants.RIS_CODE_LOGIN_ERR) {
|
|
|
|
|
Mutations.mutations.setServerCode(body.code)
|
|
|
|
|
return { myres, body }
|
|
|
|
|
}
|
2019-02-12 12:06:01 +01:00
|
|
|
|
2019-02-13 18:48:30 +01:00
|
|
|
Mutations.mutations.setServerCode(myres.status)
|
2019-02-12 19:09:43 +01:00
|
|
|
|
2019-02-13 18:48:30 +01:00
|
|
|
if (myres.status !== 200) {
|
|
|
|
|
return Promise.reject(rescodes.ERR_GENERICO)
|
|
|
|
|
}
|
|
|
|
|
return { myres, body }
|
|
|
|
|
|
|
|
|
|
}).then(({ myres, body }) => {
|
|
|
|
|
|
|
|
|
|
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 (myres.status !== 200) {
|
|
|
|
|
if (process.env.DEV) {
|
|
|
|
|
console.log('CODE = ' + body.code)
|
|
|
|
|
}
|
|
|
|
|
return body.code
|
|
|
|
|
}
|
2019-02-12 19:09:43 +01:00
|
|
|
|
2019-02-13 18:48:30 +01:00
|
|
|
if (myres.status === 200) {
|
|
|
|
|
GlobalStore.mutations.SetwasAlreadySubOnDb(body.subsExistonDb)
|
|
|
|
|
|
|
|
|
|
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, 'IDUSER= ' + userId)
|
|
|
|
|
// console.log('state.x_auth_token= ' + state.x_auth_token)
|
2019-02-12 19:09:43 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-13 18:48:30 +01:00
|
|
|
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))
|
|
|
|
|
localStorage.setItem(rescodes.localStorage.wasAlreadySubOnDb, String(GlobalStore.state.wasAlreadySubOnDb))
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-12 19:09:43 +01:00
|
|
|
|
2019-02-13 18:48:30 +01:00
|
|
|
return rescodes.OK
|
2019-02-12 19:09:43 +01:00
|
|
|
|
2019-02-13 18:48:30 +01:00
|
|
|
}).then(code => {
|
|
|
|
|
if (code === rescodes.OK) {
|
|
|
|
|
return setGlobal(true)
|
|
|
|
|
.then(() => {
|
2019-02-12 19:09:43 +01:00
|
|
|
return code
|
2019-02-13 18:48:30 +01:00
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
return code
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
UserStore.mutations.setErrorCatch(error)
|
|
|
|
|
return UserStore.getters.getServerCode
|
2018-11-02 22:15:48 +01:00
|
|
|
})
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-06 18:47:54 +01:00
|
|
|
async function logout(context) {
|
2019-02-12 12:06:01 +01:00
|
|
|
console.log('logout')
|
|
|
|
|
|
|
|
|
|
localStorage.removeItem(rescodes.localStorage.expirationDate)
|
|
|
|
|
localStorage.removeItem(rescodes.localStorage.token)
|
|
|
|
|
localStorage.removeItem(rescodes.localStorage.userId)
|
|
|
|
|
localStorage.removeItem(rescodes.localStorage.username)
|
|
|
|
|
localStorage.removeItem(rescodes.localStorage.isLogged)
|
|
|
|
|
// localStorage.removeItem(rescodes.localStorage.leftDrawerOpen)
|
|
|
|
|
localStorage.removeItem(rescodes.localStorage.verified_email)
|
|
|
|
|
localStorage.removeItem(rescodes.localStorage.categorySel)
|
2019-02-13 18:48:30 +01:00
|
|
|
localStorage.removeItem(rescodes.localStorage.wasAlreadySubOnDb)
|
|
|
|
|
|
2019-02-12 12:06:01 +01:00
|
|
|
|
|
|
|
|
await GlobalStore.actions.clearDataAfterLogout()
|
2018-11-02 20:10:45 +01:00
|
|
|
|
2018-11-02 22:15:48 +01:00
|
|
|
let call = process.env.MONGODB_HOST + '/users/me/token'
|
|
|
|
|
console.log('CALL ' + call)
|
2018-11-02 20:10:45 +01:00
|
|
|
|
|
|
|
|
let usertosend = {
|
|
|
|
|
keyappid: process.env.PAO_APP_ID,
|
|
|
|
|
idapp: process.env.APP_ID
|
2018-11-02 22:15:48 +01:00
|
|
|
}
|
2018-11-02 20:10:45 +01:00
|
|
|
|
2018-11-02 22:15:48 +01:00
|
|
|
console.log(usertosend)
|
2019-02-12 12:06:01 +01:00
|
|
|
const riscall = await Api.SendReq(call, 'DELETE', usertosend)
|
2019-02-06 18:47:54 +01:00
|
|
|
.then(({ res, body }) => {
|
|
|
|
|
console.log(res)
|
|
|
|
|
}).then(() => {
|
|
|
|
|
Mutations.mutations.clearAuthData()
|
|
|
|
|
}).catch((error) => {
|
|
|
|
|
UserStore.mutations.setErrorCatch(error)
|
|
|
|
|
return UserStore.getters.getServerCode
|
|
|
|
|
})
|
2018-11-02 22:15:48 +01:00
|
|
|
|
2019-02-12 12:06:01 +01:00
|
|
|
return riscall
|
2019-02-09 18:04:49 +01:00
|
|
|
|
|
|
|
|
// this.$router.push('/signin')
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2019-02-12 12:06:01 +01:00
|
|
|
async function setGlobal(loggedWithNetwork: boolean) {
|
2019-02-07 00:53:10 +01:00
|
|
|
state.isLogged = true
|
2019-01-05 20:11:41 +01:00
|
|
|
GlobalStore.mutations.setleftDrawerOpen(localStorage.getItem(rescodes.localStorage.leftDrawerOpen) === 'true')
|
2019-01-30 01:05:31 +01:00
|
|
|
GlobalStore.mutations.setCategorySel(localStorage.getItem(rescodes.localStorage.categorySel))
|
2019-02-01 04:10:31 +01:00
|
|
|
|
2019-02-03 14:40:20 +01:00
|
|
|
|
2019-02-09 18:04:49 +01:00
|
|
|
await GlobalStore.actions.loadAfterLogin()
|
|
|
|
|
.then(() => {
|
|
|
|
|
Todos.actions.dbLoadTodo(true)
|
|
|
|
|
})
|
2019-01-02 18:01:36 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-03 14:40:20 +01:00
|
|
|
|
2019-02-09 18:04:49 +01:00
|
|
|
async function autologin_FromLocalStorage(context) {
|
2019-01-02 18:01:36 +01:00
|
|
|
try {
|
2019-02-13 18:48:30 +01:00
|
|
|
// console.log('*** autologin_FromLocalStorage ***')
|
2019-01-02 18:01:36 +01:00
|
|
|
// INIT
|
2019-02-09 18:04:49 +01:00
|
|
|
|
2019-01-02 18:01:36 +01:00
|
|
|
UserStore.mutations.setlang(process.env.LANG_DEFAULT)
|
2019-02-09 18:04:49 +01:00
|
|
|
// Estrai la Lang dal Localstorage
|
2019-01-02 18:01:36 +01:00
|
|
|
const lang = localStorage.getItem('lang')
|
|
|
|
|
if (lang) {
|
|
|
|
|
UserStore.mutations.setlang(lang)
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-05 20:11:41 +01:00
|
|
|
const token = localStorage.getItem(rescodes.localStorage.token)
|
2019-01-02 18:01:36 +01:00
|
|
|
if (!token) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2019-01-05 20:11:41 +01:00
|
|
|
const expirationDateStr = localStorage.getItem(rescodes.localStorage.expirationDate)
|
2019-01-02 18:01:36 +01:00
|
|
|
let expirationDate = new Date(String(expirationDateStr))
|
|
|
|
|
const now = new Date()
|
|
|
|
|
if (now >= expirationDate) {
|
2019-02-07 00:53:10 +01:00
|
|
|
console.log('!!! Login Expired')
|
2019-01-02 18:01:36 +01:00
|
|
|
return false
|
|
|
|
|
}
|
2019-01-14 22:40:30 +01:00
|
|
|
const userId = String(localStorage.getItem(rescodes.localStorage.userId))
|
2019-01-05 20:11:41 +01:00
|
|
|
const username = String(localStorage.getItem(rescodes.localStorage.username))
|
2019-02-09 18:04:49 +01:00
|
|
|
const verified_email = localStorage.getItem(rescodes.localStorage.verified_email) === 'true'
|
2019-01-02 18:01:36 +01:00
|
|
|
|
2019-02-13 18:48:30 +01:00
|
|
|
GlobalStore.state.wasAlreadySubOnDb = localStorage.getItem(rescodes.localStorage.wasAlreadySubOnDb) === 'true'
|
|
|
|
|
|
|
|
|
|
console.log('************* autologin userId', userId)
|
2019-01-02 18:01:36 +01:00
|
|
|
|
2019-02-12 12:06:01 +01:00
|
|
|
UserStore.mutations.setAuth(token)
|
|
|
|
|
|
2019-01-02 18:01:36 +01:00
|
|
|
Mutations.mutations.authUser({
|
|
|
|
|
userId: userId,
|
2019-01-14 22:40:30 +01:00
|
|
|
username: username,
|
2019-02-13 18:48:30 +01:00
|
|
|
verified_email: verified_email,
|
2019-01-02 18:01:36 +01:00
|
|
|
})
|
2019-02-01 04:10:31 +01:00
|
|
|
|
2019-02-12 12:06:01 +01:00
|
|
|
await setGlobal(false)
|
2019-02-01 04:10:31 +01:00
|
|
|
|
|
|
|
|
console.log('autologin userId STATE ', state.userId)
|
|
|
|
|
|
2019-01-02 18:01:36 +01:00
|
|
|
return true
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('ERR autologin ', e.message)
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-15 19:48:37 +01:00
|
|
|
|
|
|
|
|
export const actions = {
|
|
|
|
|
resetpwd: b.dispatch(resetpwd),
|
|
|
|
|
requestpwd: b.dispatch(requestpwd),
|
|
|
|
|
vreg: b.dispatch(vreg),
|
|
|
|
|
signup: b.dispatch(signup),
|
|
|
|
|
signin: b.dispatch(signin),
|
2019-01-02 18:01:36 +01:00
|
|
|
logout: b.dispatch(logout),
|
2019-02-09 18:04:49 +01:00
|
|
|
autologin_FromLocalStorage: b.dispatch(autologin_FromLocalStorage)
|
2018-11-15 19:48:37 +01:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Module
|
|
|
|
|
const UserModule = {
|
|
|
|
|
get state() {
|
|
|
|
|
return stateGetter()
|
|
|
|
|
},
|
|
|
|
|
getters: Getters.getters,
|
|
|
|
|
mutations: Mutations.mutations,
|
|
|
|
|
actions: Actions.actions
|
2018-11-02 15:56:29 +01:00
|
|
|
}
|
2018-11-02 23:22:16 +01:00
|
|
|
|
2018-11-15 19:48:37 +01:00
|
|
|
export default UserModule
|