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'
|
2018-11-17 20:32:28 +01:00
|
|
|
import { ILinkReg, IResult, IIdToken } 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
|
|
|
|
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: '',
|
2018-11-15 19:48:37 +01:00
|
|
|
email: '',
|
|
|
|
|
username: '',
|
|
|
|
|
idapp: process.env.APP_ID,
|
|
|
|
|
password: '',
|
|
|
|
|
lang: '',
|
|
|
|
|
repeatPassword: '',
|
|
|
|
|
idToken: '',
|
|
|
|
|
tokens: [],
|
2019-01-30 01:05:31 +01:00
|
|
|
verifiedEmail: false,
|
|
|
|
|
categorySel: 'personal'
|
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
|
|
|
|
2018-12-22 23:30:02 +01:00
|
|
|
const tok = b.read(state => {
|
2018-11-15 19:48:37 +01:00
|
|
|
if (state.tokens) {
|
|
|
|
|
if (typeof state.tokens[0] !== 'undefined') {
|
|
|
|
|
return state.tokens[0].token
|
2018-11-02 20:10:45 +01:00
|
|
|
} else {
|
2018-11-02 22:15:48 +01:00
|
|
|
return ''
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
2018-11-02 22:15:48 +01:00
|
|
|
return ''
|
2018-11-02 15:56:29 +01:00
|
|
|
}
|
2018-12-22 23:30:02 +01:00
|
|
|
}, 'tok')
|
2018-11-15 19:48:37 +01:00
|
|
|
|
|
|
|
|
export const getters = {
|
|
|
|
|
get lang() {
|
|
|
|
|
return lang()
|
|
|
|
|
},
|
|
|
|
|
get tok() {
|
|
|
|
|
return tok()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Mutations {
|
|
|
|
|
function authUser(state, data: IUserState) {
|
|
|
|
|
state.userId = data.userId
|
2019-01-14 22:40:30 +01:00
|
|
|
state.username = data.username
|
2018-11-15 19:48:37 +01:00
|
|
|
state.idToken = data.idToken
|
|
|
|
|
state.verifiedEmail = data.verifiedEmail
|
2019-01-30 01:05:31 +01:00
|
|
|
state.category = data.categorySel
|
2018-11-15 19:48:37 +01:00
|
|
|
// @ts-ignore
|
|
|
|
|
state.tokens = [
|
|
|
|
|
{ access: 'auth', token: data.idToken }
|
|
|
|
|
]
|
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) {
|
|
|
|
|
state.idToken = data.idToken
|
|
|
|
|
if (!state.tokens) {
|
|
|
|
|
state.tokens = []
|
|
|
|
|
}
|
|
|
|
|
state.tokens.push({ access: 'auth', token: data.idToken })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setServerCode(state: IUserState, num: number) {
|
|
|
|
|
state.servercode = num
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function clearAuthData(state: IUserState) {
|
2019-01-14 22:40:30 +01:00
|
|
|
state.userId = ''
|
2018-11-15 19:48:37 +01:00
|
|
|
state.username = ''
|
|
|
|
|
state.tokens = []
|
|
|
|
|
state.idToken = ''
|
|
|
|
|
state.verifiedEmail = false
|
2019-01-30 01:05:31 +01:00
|
|
|
state.categorySel = 'personal'
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
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-01-02 18:01:36 +01:00
|
|
|
clearAuthData: b.commit(clearAuthData)
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
2018-11-15 19:48:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace Actions {
|
|
|
|
|
|
|
|
|
|
async function sendUserEdit(context, form: Object) {
|
|
|
|
|
try {
|
|
|
|
|
const {data} = await Api.postFormData('profile/edit', form)
|
|
|
|
|
console.log(data)
|
|
|
|
|
// return new ApiSuccess({data})
|
|
|
|
|
|
|
|
|
|
} catch {
|
|
|
|
|
// return new ApiError()
|
|
|
|
|
}
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
2018-11-17 20:32:28 +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
|
|
|
|
2018-11-02 22:15:48 +01:00
|
|
|
let myres
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2018-11-02 22:15:48 +01:00
|
|
|
let x_auth_token: string = ''
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2019-02-01 04:10:31 +01:00
|
|
|
return await Api.SendReq(call, state.lang, Getters.getters.tok, 'POST', usertosend)
|
2018-11-02 20:10:45 +01:00
|
|
|
.then((res) => {
|
2018-11-02 22:15:48 +01:00
|
|
|
console.log(res)
|
|
|
|
|
myres = res
|
|
|
|
|
x_auth_token = String(res.headers.get('x-auth'))
|
2018-11-02 20:10:45 +01:00
|
|
|
if (myres.status === 200) {
|
2018-11-02 22:15:48 +01:00
|
|
|
return myres.json()
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
2018-11-17 20:32:28 +01:00
|
|
|
Mutations.mutations.setServerCode(rescodes.ERR_GENERICO)
|
|
|
|
|
return { code: rescodes.ERR_GENERICO, msg: 'Errore: ' + myres.status, resetpwd: true }
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2018-11-02 20:10:45 +01:00
|
|
|
})
|
|
|
|
|
.then((body) => {
|
2018-11-15 19:48:37 +01:00
|
|
|
Mutations.mutations.UpdatePwd({ idToken: x_auth_token })
|
2019-01-05 20:11:41 +01:00
|
|
|
localStorage.setItem(rescodes.localStorage.token, x_auth_token)
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2018-11-02 22:15:48 +01:00
|
|
|
return { code: body.code, msg: body.msg }
|
2018-11-02 20:10:45 +01:00
|
|
|
}).catch((err) => {
|
2018-11-02 22:15:48 +01:00
|
|
|
console.log('ERROR: ' + err)
|
2018-11-17 20:32:28 +01:00
|
|
|
Mutations.mutations.setServerCode(rescodes.ERR_GENERICO)
|
|
|
|
|
return { code: rescodes.ERR_GENERICO, msg: 'Errore' }
|
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
|
|
|
|
2018-11-17 20:32:28 +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
|
|
|
|
2018-11-02 22:15:48 +01:00
|
|
|
let myres
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2019-02-01 04:10:31 +01:00
|
|
|
return await Api.SendReq(call, state.lang, Getters.getters.tok, 'POST', usertosend)
|
2018-11-02 20:10:45 +01:00
|
|
|
.then((res) => {
|
2018-11-02 22:15:48 +01:00
|
|
|
console.log(res)
|
|
|
|
|
myres = res
|
2018-11-02 20:10:45 +01:00
|
|
|
if (myres.status === 200) {
|
2018-11-02 22:15:48 +01:00
|
|
|
return myres.json()
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
2018-11-17 20:32:28 +01:00
|
|
|
Mutations.mutations.setServerCode(rescodes.ERR_GENERICO)
|
|
|
|
|
return { code: rescodes.ERR_GENERICO, msg: 'Errore: ' + myres.status, resetpwd: true }
|
2018-11-02 20:10:45 +01:00
|
|
|
|
|
|
|
|
})
|
|
|
|
|
.then((body) => {
|
2018-11-02 22:15:48 +01:00
|
|
|
return { code: body.code, msg: body.msg }
|
2018-11-02 20:10:45 +01:00
|
|
|
}).catch((err) => {
|
2018-11-02 22:15:48 +01:00
|
|
|
console.log('ERROR: ' + err)
|
2018-11-17 20:32:28 +01:00
|
|
|
Mutations.mutations.setServerCode(rescodes.ERR_GENERICO)
|
|
|
|
|
return { code: rescodes.ERR_GENERICO, msg: 'Errore' }
|
2018-11-02 22:15:48 +01:00
|
|
|
})
|
2018-11-02 20:10:45 +01:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-17 20:32:28 +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
|
|
|
|
2018-11-02 22:15:48 +01:00
|
|
|
let myres
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2019-01-02 01:58:47 +01:00
|
|
|
return await Api.SendReq(call, state.lang, Getters.getters.tok, 'POST', usertosend)
|
2018-11-02 20:10:45 +01:00
|
|
|
.then((res) => {
|
2018-11-02 22:15:48 +01:00
|
|
|
console.log(res)
|
|
|
|
|
myres = res
|
2018-11-02 20:10:45 +01:00
|
|
|
if (myres.status === 200) {
|
2018-11-02 22:15:48 +01:00
|
|
|
return myres.json()
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
2018-11-17 20:32:28 +01:00
|
|
|
Mutations.mutations.setServerCode(rescodes.ERR_GENERICO)
|
|
|
|
|
return { code: rescodes.ERR_GENERICO, msg: 'Errore: ' + myres.status }
|
2018-11-02 20:10:45 +01:00
|
|
|
|
|
|
|
|
})
|
|
|
|
|
.then((body) => {
|
|
|
|
|
// 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-01-05 20:11:41 +01:00
|
|
|
localStorage.setItem(rescodes.localStorage.verifiedEmail, '1')
|
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 }
|
2018-11-02 20:10:45 +01:00
|
|
|
}).catch((err) => {
|
2018-11-02 22:15:48 +01:00
|
|
|
console.log('ERROR: ' + err)
|
2018-11-17 20:32:28 +01:00
|
|
|
Mutations.mutations.setServerCode(rescodes.ERR_GENERICO)
|
|
|
|
|
return { code: rescodes.ERR_GENERICO, msg: 'Errore' }
|
2018-11-02 22:15:48 +01:00
|
|
|
})
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
2018-11-17 20:32:28 +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
|
|
|
|
2018-11-02 22:15:48 +01:00
|
|
|
let x_auth_token: string = ''
|
2018-11-02 20:10:45 +01:00
|
|
|
|
2018-11-15 19:48:37 +01:00
|
|
|
return Api.SendReq(call, state.lang, Getters.getters.tok, 'POST', usertosend)
|
2018-11-02 20:10:45 +01:00
|
|
|
.then((res) => {
|
2018-11-02 22:15:48 +01:00
|
|
|
myres = res
|
|
|
|
|
x_auth_token = String(res.headers.get('x-auth'))
|
2018-11-02 20:10:45 +01:00
|
|
|
if (x_auth_token) {
|
2018-11-02 22:15:48 +01:00
|
|
|
return res.json()
|
2018-11-02 20:10:45 +01:00
|
|
|
} else {
|
2018-11-17 20:32:28 +01:00
|
|
|
return { status: 400, code: rescodes.ERR_GENERICO }
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.then((body) => {
|
|
|
|
|
if (process.env.DEV) {
|
2018-11-02 22:15:48 +01:00
|
|
|
console.log('RISULTATO ')
|
|
|
|
|
console.log('STATUS ' + myres.status + ' ' + (myres.statusText))
|
|
|
|
|
console.log('BODY:')
|
|
|
|
|
console.log(body)
|
2018-11-02 20:10:45 +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-01-14 22:40:30 +01:00
|
|
|
let userId = body.userId
|
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-01-14 22:40:30 +01:00
|
|
|
userId: userId,
|
2018-11-02 20:10:45 +01:00
|
|
|
username: username,
|
|
|
|
|
idToken: x_auth_token,
|
|
|
|
|
verifiedEmail: 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)
|
|
|
|
|
localStorage.setItem(rescodes.localStorage.token, x_auth_token)
|
|
|
|
|
localStorage.setItem(rescodes.localStorage.expirationDate, expirationDate.toString())
|
|
|
|
|
localStorage.setItem(rescodes.localStorage.verifiedEmail, '0')
|
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 if (myres.status === 404) {
|
|
|
|
|
if (process.env.DEV) {
|
2018-11-02 22:15:48 +01:00
|
|
|
console.log('CODE = ' + body.code)
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
2018-11-02 22:15:48 +01:00
|
|
|
return body.code
|
2018-11-02 20:10:45 +01:00
|
|
|
} else {
|
|
|
|
|
if (process.env.DEV) {
|
2018-11-02 22:15:48 +01:00
|
|
|
console.log('CODE = ' + body.code)
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
2018-11-02 22:15:48 +01:00
|
|
|
return body.code
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
if (process.env.DEV) {
|
2019-02-03 14:40:20 +01:00
|
|
|
console.log('signup ERROREEEEEEEEE')
|
2018-11-02 22:15:48 +01:00
|
|
|
console.log(error)
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
2018-11-17 20:32:28 +01:00
|
|
|
Mutations.mutations.setServerCode(rescodes.ERR_GENERICO)
|
|
|
|
|
return rescodes.ERR_GENERICO
|
2018-11-02 22:15:48 +01:00
|
|
|
})
|
|
|
|
|
})
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
2018-12-06 20:07:51 +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
|
|
|
|
|
|
|
|
const usertosend = {
|
|
|
|
|
username: authData.username,
|
|
|
|
|
password: authData.password,
|
|
|
|
|
idapp: process.env.APP_ID,
|
|
|
|
|
keyappid: process.env.PAO_APP_ID,
|
2018-11-15 19:48:37 +01:00
|
|
|
lang: state.lang
|
2018-11-02 22:15:48 +01:00
|
|
|
}
|
2018-11-02 15:56:29 +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 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
|
|
|
|
2018-11-02 22:15:48 +01:00
|
|
|
let x_auth_token: string = ''
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2019-02-01 04:10:31 +01:00
|
|
|
return await Api.SendReq(call, state.lang, Getters.getters.tok, 'POST', usertosend)
|
2018-11-02 20:10:45 +01:00
|
|
|
.then((res) => {
|
2018-11-02 22:15:48 +01:00
|
|
|
myres = res
|
|
|
|
|
x_auth_token = String(res.headers.get('x-auth'))
|
|
|
|
|
let injson = res.json()
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2018-11-02 20:10:45 +01:00
|
|
|
if (x_auth_token || injson) {
|
2018-11-02 22:15:48 +01:00
|
|
|
return injson
|
2018-11-02 20:10:45 +01:00
|
|
|
} else {
|
2018-11-17 20:32:28 +01:00
|
|
|
return { status: 400, code: rescodes.ERR_GENERICO }
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.then((body) => {
|
|
|
|
|
if (process.env.DEV) {
|
2018-11-02 22:15:48 +01:00
|
|
|
console.log('RISULTATO ')
|
|
|
|
|
console.log('STATUS ' + myres.status + ' ' + (myres.statusText))
|
|
|
|
|
console.log('BODY:')
|
|
|
|
|
console.log(body)
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2018-11-02 20:10:45 +01:00
|
|
|
if (body.code === serv_constants.RIS_CODE_LOGIN_ERR) {
|
2018-11-15 19:48:37 +01:00
|
|
|
Mutations.mutations.setServerCode(body.code)
|
2018-11-02 22:15:48 +01:00
|
|
|
return body.code
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
2018-11-02 15:56:29 +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-01-14 22:40:30 +01:00
|
|
|
let userId = body.userId
|
2018-11-02 22:15:48 +01:00
|
|
|
let username = authData.username
|
2019-01-02 18:01:36 +01:00
|
|
|
let verifiedEmail = body.verified_email === 'true' || body.verified_email === true
|
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-15 19:48:37 +01:00
|
|
|
Mutations.mutations.authUser({
|
2019-01-14 22:40:30 +01:00
|
|
|
userId: userId,
|
2018-11-02 20:10:45 +01:00
|
|
|
username: username,
|
|
|
|
|
idToken: x_auth_token,
|
|
|
|
|
verifiedEmail: verifiedEmail
|
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)
|
|
|
|
|
localStorage.setItem(rescodes.localStorage.token, 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())
|
2018-11-02 20:10:45 +01:00
|
|
|
|
2019-02-01 04:10:31 +01:00
|
|
|
setGlobal()
|
|
|
|
|
|
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 if (myres.status === 404) {
|
|
|
|
|
if (process.env.DEV) {
|
2018-11-02 22:15:48 +01:00
|
|
|
console.log('CODE = ' + body.code)
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
2018-11-02 22:15:48 +01:00
|
|
|
return body.code
|
2018-11-02 20:10:45 +01:00
|
|
|
} else {
|
|
|
|
|
if (process.env.DEV) {
|
2018-11-02 22:15:48 +01:00
|
|
|
console.log('CODE = ' + body.code)
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
2018-11-02 22:15:48 +01:00
|
|
|
return body.code
|
2018-11-02 15:56:29 +01:00
|
|
|
}
|
2018-11-02 20:10:45 +01:00
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
if (process.env.DEV) {
|
2019-02-03 14:40:20 +01:00
|
|
|
console.log('signin ERRORE', error)
|
2018-11-02 15:56:29 +01:00
|
|
|
}
|
2018-11-17 20:32:28 +01:00
|
|
|
Mutations.mutations.setServerCode(rescodes.ERR_GENERICO)
|
|
|
|
|
return rescodes.ERR_GENERICO
|
2018-11-02 22:15:48 +01:00
|
|
|
})
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
2018-11-17 20:32:28 +01:00
|
|
|
async function logout (context) {
|
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-01 04:10:31 +01:00
|
|
|
return await Api.SendReq(call, state.lang, Getters.getters.tok, 'DELETE', usertosend)
|
2018-11-02 20:10:45 +01:00
|
|
|
.then(
|
|
|
|
|
(res) => {
|
2018-11-02 22:15:48 +01:00
|
|
|
console.log(res)
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
).catch((err) => {
|
2018-11-02 22:15:48 +01:00
|
|
|
console.log('ERROR: ' + err)
|
2018-11-02 20:10:45 +01:00
|
|
|
}).then(() => {
|
2018-11-15 19:48:37 +01:00
|
|
|
Mutations.mutations.clearAuthData()
|
2018-11-02 22:15:48 +01:00
|
|
|
})
|
|
|
|
|
|
2019-01-05 20:11:41 +01:00
|
|
|
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.verifiedEmail)
|
2019-01-30 01:05:31 +01:00
|
|
|
localStorage.removeItem(rescodes.localStorage.categorySel)
|
2018-11-02 20:10:45 +01:00
|
|
|
|
2018-11-15 19:48:37 +01:00
|
|
|
router.push('/signin')
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2019-01-02 18:01:36 +01:00
|
|
|
function setGlobal() {
|
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-05 18:17:36 +01:00
|
|
|
GlobalStore.actions.loadAfterLogin()
|
|
|
|
|
|
2019-02-04 03:09:15 +01:00
|
|
|
Todos.actions.dbLoadTodo(true)
|
2019-02-03 14:40:20 +01:00
|
|
|
|
|
|
|
|
|
2019-01-02 18:01:36 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-03 14:40:20 +01:00
|
|
|
|
2019-01-02 18:01:36 +01:00
|
|
|
async function autologin (context) {
|
|
|
|
|
try {
|
2019-02-01 04:10:31 +01:00
|
|
|
console.log('*** Autologin ***')
|
2019-01-02 18:01:36 +01:00
|
|
|
// INIT
|
|
|
|
|
UserStore.mutations.setlang(process.env.LANG_DEFAULT)
|
|
|
|
|
// ++Todo: Estrai la Lang dal Localstorage
|
|
|
|
|
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) {
|
|
|
|
|
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))
|
|
|
|
|
const verifiedEmail = localStorage.getItem(rescodes.localStorage.verifiedEmail) === '1'
|
2019-01-02 18:01:36 +01:00
|
|
|
|
2019-02-01 04:10:31 +01:00
|
|
|
console.log('autologin userId', userId)
|
2019-01-02 18:01:36 +01:00
|
|
|
|
|
|
|
|
Mutations.mutations.authUser({
|
|
|
|
|
userId: userId,
|
2019-01-14 22:40:30 +01:00
|
|
|
username: username,
|
2019-01-02 18:01:36 +01:00
|
|
|
idToken: token,
|
|
|
|
|
verifiedEmail: verifiedEmail
|
|
|
|
|
})
|
2019-02-01 04:10:31 +01:00
|
|
|
|
|
|
|
|
setGlobal()
|
|
|
|
|
|
|
|
|
|
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),
|
|
|
|
|
autologin: b.dispatch(autologin)
|
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
|