2018-11-02 22:15:48 +01:00
|
|
|
import Vue from 'vue'
|
|
|
|
|
import Vuex from 'vuex'
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2018-11-02 22:15:48 +01:00
|
|
|
import { Module, VuexModule, Mutation, MutationAction, Action, getModule } from 'vuex-module-decorators'
|
2018-11-05 22:28:59 +01:00
|
|
|
import {Route} from 'vue-router'
|
|
|
|
|
import store from '@/store'
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2018-11-02 23:22:16 +01:00
|
|
|
const bcrypt = require('bcryptjs')
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2018-11-02 22:15:48 +01:00
|
|
|
import * as types from '@/store/mutation-types'
|
|
|
|
|
import { serv_constants } from '@/store/modules/serv_constants'
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2018-11-12 18:08:06 +01:00
|
|
|
import { ISignupOptions, IUserState } from '@/model'
|
2018-11-08 01:09:33 +01:00
|
|
|
import { ILinkReg, IResult, IIdToken } from '@/model/other'
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2018-11-02 22:15:48 +01:00
|
|
|
|
|
|
|
|
export const ErroriMongoDb = {
|
2018-11-02 20:10:45 +01:00
|
|
|
CALLING: 10,
|
|
|
|
|
OK: 20,
|
|
|
|
|
ERR_GENERICO: -1,
|
|
|
|
|
DUPLICATE_EMAIL_ID: 11000,
|
|
|
|
|
DUPLICATE_USERNAME_ID: 11100
|
2018-11-02 22:15:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vue.use(Vuex)
|
|
|
|
|
|
2018-11-02 23:22:16 +01:00
|
|
|
@Module({ dynamic: true, store, name: 'user' })
|
2018-11-03 01:23:39 +01:00
|
|
|
class User extends VuexModule implements IUserState { // Non occorrono i getters, basta questi qui:
|
2018-11-02 22:15:48 +01:00
|
|
|
_id: IUserState['_id'] = ''
|
|
|
|
|
email: IUserState['email'] = ''
|
|
|
|
|
username: IUserState['username'] = ''
|
|
|
|
|
idapp: IUserState['idapp'] = process.env.APP_ID
|
|
|
|
|
password: IUserState['password'] = ''
|
|
|
|
|
lang: IUserState['lang'] = ''
|
2018-11-05 22:28:59 +01:00
|
|
|
repeatPassword: IUserState['repeatPassword'] = ''
|
2018-11-02 22:15:48 +01:00
|
|
|
idToken: IUserState['idToken'] = ''
|
|
|
|
|
userId: IUserState['userId'] = 0
|
|
|
|
|
tokens: IUserState['tokens'] = []
|
|
|
|
|
verifiedEmail: IUserState['verifiedEmail'] = false
|
|
|
|
|
servercode: number = 0
|
2018-11-02 20:10:45 +01:00
|
|
|
|
2018-11-12 18:08:06 +01:00
|
|
|
getlang() {
|
2018-11-02 20:10:45 +01:00
|
|
|
if (this.lang !== '') {
|
2018-11-02 22:15:48 +01:00
|
|
|
return this.lang
|
2018-11-02 20:10:45 +01:00
|
|
|
} else {
|
2018-11-02 22:15:48 +01:00
|
|
|
return process.env.LANG_DEFAULT
|
2018-11-02 15:56:29 +01:00
|
|
|
}
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
2018-11-03 01:23:39 +01:00
|
|
|
sendRequest (url: string, method: string, mydata: any) {
|
2018-11-02 22:15:48 +01:00
|
|
|
console.log('LANG ' + this.getlang())
|
2018-11-03 01:23:39 +01:00
|
|
|
let mytok: string = this.getTok()
|
2018-11-02 20:10:45 +01:00
|
|
|
|
2018-11-02 22:15:48 +01:00
|
|
|
const authHeader = new Headers()
|
|
|
|
|
authHeader.append('content-type', 'application/json')
|
|
|
|
|
authHeader.append('x-auth', mytok)
|
|
|
|
|
authHeader.append('accept-language', this.getlang())
|
2018-11-02 20:10:45 +01:00
|
|
|
const configInit: RequestInit = {
|
|
|
|
|
method: method,
|
|
|
|
|
cache: 'no-cache',
|
|
|
|
|
body: JSON.stringify(mydata),
|
|
|
|
|
headers: authHeader
|
2018-11-02 22:15:48 +01:00
|
|
|
}
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2018-11-02 22:15:48 +01:00
|
|
|
const request: Promise<Response> = fetch(url, configInit)
|
|
|
|
|
return request
|
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-03 01:23:39 +01:00
|
|
|
getTok () {
|
2018-11-02 20:10:45 +01:00
|
|
|
if (this.tokens) {
|
|
|
|
|
if (typeof this.tokens[0] !== 'undefined') {
|
2018-11-02 22:15:48 +01:00
|
|
|
return this.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-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
2018-11-02 22:15:48 +01:00
|
|
|
@MutationAction({ mutate: [types.USER_PASSWORD] })
|
2018-11-03 01:23:39 +01:00
|
|
|
async setpassword (newstr: string) {
|
2018-11-02 22:15:48 +01:00
|
|
|
return { password: newstr }
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@MutationAction({ mutate: [types.USER_EMAIL] })
|
2018-11-03 01:23:39 +01:00
|
|
|
async setemail (newstr: string) {
|
2018-11-02 22:15:48 +01:00
|
|
|
return { email: newstr }
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@MutationAction({ mutate: [types.USER_LANG] })
|
2018-11-03 01:23:39 +01:00
|
|
|
async setlang (newstr: string) {
|
2018-11-02 22:15:48 +01:00
|
|
|
return { lang: newstr }
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Mutation
|
2018-11-03 01:23:39 +01:00
|
|
|
authUser (data: IUserState) {
|
2018-11-02 22:15:48 +01:00
|
|
|
this.username = data.username
|
|
|
|
|
this.userId = data.userId
|
|
|
|
|
this.idToken = data.idToken
|
|
|
|
|
this.verifiedEmail = data.verifiedEmail
|
2018-11-05 22:28:59 +01:00
|
|
|
// @ts-ignore
|
2018-11-02 20:10:45 +01:00
|
|
|
this.tokens = [
|
|
|
|
|
{ access: 'auth', token: data.idToken }
|
2018-11-02 22:15:48 +01:00
|
|
|
]
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Mutation
|
2018-11-03 01:23:39 +01:00
|
|
|
UpdatePwd (data: IIdToken) {
|
2018-11-02 22:15:48 +01:00
|
|
|
this.idToken = data.idToken
|
2018-11-02 20:10:45 +01:00
|
|
|
if (!this.tokens) {
|
2018-11-02 22:15:48 +01:00
|
|
|
this.tokens = []
|
2018-11-02 15:56:29 +01:00
|
|
|
}
|
2018-11-02 22:15:48 +01:00
|
|
|
this.tokens.push({ access: 'auth', token: data.idToken })
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Mutation
|
2018-11-03 01:23:39 +01:00
|
|
|
setServerCode (servercode: number) {
|
2018-11-02 22:15:48 +01:00
|
|
|
this.servercode = servercode
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Mutation
|
2018-11-03 01:23:39 +01:00
|
|
|
clearAuthData (): void {
|
2018-11-02 22:15:48 +01:00
|
|
|
this.username = ''
|
|
|
|
|
this.tokens = []
|
|
|
|
|
this.idToken = ''
|
|
|
|
|
this.userId = 0
|
|
|
|
|
this.verifiedEmail = false
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Action({ commit: types.USER_UPDATEPWD })
|
2018-11-03 01:23:39 +01:00
|
|
|
resetpwd (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-02 22:15:48 +01:00
|
|
|
this.setServerCode(ErroriMongoDb.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
|
|
|
|
2018-11-02 20:10:45 +01:00
|
|
|
return this.sendRequest(call, 'POST', usertosend)
|
|
|
|
|
.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-02 22:15:48 +01:00
|
|
|
this.setServerCode(ErroriMongoDb.ERR_GENERICO)
|
|
|
|
|
return { code: ErroriMongoDb.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-02 22:15:48 +01:00
|
|
|
this.UpdatePwd({ idToken: x_auth_token })
|
|
|
|
|
localStorage.setItem('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)
|
|
|
|
|
this.setServerCode(ErroriMongoDb.ERR_GENERICO)
|
|
|
|
|
return { code: ErroriMongoDb.ERR_GENERICO, msg: 'Errore' }
|
|
|
|
|
})
|
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-02 20:10:45 +01:00
|
|
|
@Action({ commit: types.USER_REQUESTRESETPWD })
|
2018-11-03 01:23:39 +01:00
|
|
|
requestpwd (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-02 22:15:48 +01:00
|
|
|
this.setServerCode(ErroriMongoDb.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 20:10:45 +01:00
|
|
|
return this.sendRequest(call, 'POST', usertosend)
|
|
|
|
|
.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-02 22:15:48 +01:00
|
|
|
this.setServerCode(ErroriMongoDb.ERR_GENERICO)
|
|
|
|
|
return { code: ErroriMongoDb.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)
|
|
|
|
|
this.setServerCode(ErroriMongoDb.ERR_GENERICO)
|
|
|
|
|
return { code: ErroriMongoDb.ERR_GENERICO, msg: 'Errore' }
|
|
|
|
|
})
|
2018-11-02 20:10:45 +01:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Action({ commit: types.USER_VREG })
|
2018-11-03 01:23:39 +01:00
|
|
|
vreg (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,
|
|
|
|
|
idLink: paramquery.idLink
|
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
|
|
|
this.setServerCode(ErroriMongoDb.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 20:10:45 +01:00
|
|
|
return this.sendRequest(call, 'POST', usertosend)
|
|
|
|
|
.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-02 22:15:48 +01:00
|
|
|
this.setServerCode(ErroriMongoDb.ERR_GENERICO)
|
|
|
|
|
return { code: ErroriMongoDb.ERR_GENERICO, msg: 'Errore: ' + myres.status }
|
2018-11-02 20:10:45 +01:00
|
|
|
|
|
|
|
|
})
|
|
|
|
|
.then((body) => {
|
|
|
|
|
// console.log("RITORNO 2 ");
|
|
|
|
|
// this.setServerCode(myres);
|
|
|
|
|
if (body.code === serv_constants.RIS_CODE_EMAIL_VERIFIED) {
|
2018-11-02 22:15:48 +01:00
|
|
|
localStorage.setItem('verificato', '1')
|
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)
|
|
|
|
|
this.setServerCode(ErroriMongoDb.ERR_GENERICO)
|
|
|
|
|
return { code: ErroriMongoDb.ERR_GENERICO, msg: 'Errore' }
|
|
|
|
|
})
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
2018-11-12 18:08:06 +01:00
|
|
|
@Action({ commit: types.USER_SIGNUP })
|
|
|
|
|
signup (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-12 18:08:06 +01:00
|
|
|
let mylang = this.getlang()
|
|
|
|
|
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-02 22:15:48 +01:00
|
|
|
this.setServerCode(ErroriMongoDb.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
|
|
|
|
|
|
|
|
return this.sendRequest(call, 'POST', usertosend)
|
|
|
|
|
.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-02 22:15:48 +01:00
|
|
|
return { status: 400, code: ErroriMongoDb.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 22:15:48 +01:00
|
|
|
this.setServerCode(myres.status)
|
2018-11-02 20:10:45 +01:00
|
|
|
|
|
|
|
|
if (myres.status === 200) {
|
2018-11-02 22:15:48 +01:00
|
|
|
let iduser = body._id
|
|
|
|
|
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)
|
|
|
|
|
console.log('IDUSER= ' + iduser)
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.authUser({
|
|
|
|
|
username: username,
|
|
|
|
|
userId: iduser,
|
|
|
|
|
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);
|
2018-11-02 22:15:48 +01:00
|
|
|
const expirationDate = new Date(now.getTime() + 1000)
|
|
|
|
|
localStorage.setItem('username', username)
|
|
|
|
|
localStorage.setItem('token', x_auth_token)
|
|
|
|
|
localStorage.setItem('userId', iduser)
|
|
|
|
|
localStorage.setItem('expirationDate', expirationDate.toString())
|
|
|
|
|
localStorage.setItem('verificato', '0')
|
2018-11-02 20:10:45 +01:00
|
|
|
// dispatch('storeUser', authData);
|
|
|
|
|
// dispatch('setLogoutTimer', myres.data.expiresIn);
|
|
|
|
|
|
2018-11-02 22:15:48 +01:00
|
|
|
return ErroriMongoDb.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) {
|
2018-11-02 22:15:48 +01:00
|
|
|
console.log('ERROREEEEEEEEE')
|
|
|
|
|
console.log(error)
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
2018-11-02 22:15:48 +01:00
|
|
|
this.setServerCode(ErroriMongoDb.ERR_GENERICO)
|
|
|
|
|
return ErroriMongoDb.ERR_GENERICO
|
|
|
|
|
})
|
|
|
|
|
})
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Action({ commit: types.USER_SIGNIN })
|
2018-11-12 18:08:06 +01:00
|
|
|
signin (authData: ISignupOptions) {
|
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-02 22:15:48 +01:00
|
|
|
console.log('MYLANG = ' + this.getlang())
|
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,
|
|
|
|
|
lang: this.getlang()
|
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-02 22:15:48 +01:00
|
|
|
this.setServerCode(ErroriMongoDb.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
|
|
|
|
2018-11-02 20:10:45 +01:00
|
|
|
return this.sendRequest(call, 'POST', usertosend)
|
|
|
|
|
.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-02 22:15:48 +01:00
|
|
|
return { status: 400, code: ErroriMongoDb.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-02 22:15:48 +01:00
|
|
|
this.setServerCode(body.code)
|
|
|
|
|
return body.code
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
2018-11-02 15:56:29 +01:00
|
|
|
|
2018-11-02 22:15:48 +01:00
|
|
|
this.setServerCode(myres.status)
|
2018-11-02 20:10:45 +01:00
|
|
|
|
|
|
|
|
if (myres.status === 200) {
|
2018-11-02 22:15:48 +01:00
|
|
|
let iduser = body._id
|
|
|
|
|
let username = authData.username
|
|
|
|
|
let verifiedEmail = body.verifiedEmail === 'true' || body.verifiedEmail === 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)
|
|
|
|
|
console.log('IDUSER= ' + iduser)
|
2018-11-02 20:10:45 +01:00
|
|
|
this.authUser({
|
|
|
|
|
username: username,
|
|
|
|
|
userId: iduser,
|
|
|
|
|
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);
|
2018-11-02 22:15:48 +01:00
|
|
|
const expirationDate = new Date(now.getTime() + 1000)
|
|
|
|
|
localStorage.setItem('username', username)
|
|
|
|
|
localStorage.setItem('token', x_auth_token)
|
|
|
|
|
localStorage.setItem('userId', iduser)
|
|
|
|
|
localStorage.setItem('expirationDate', expirationDate.toString())
|
|
|
|
|
localStorage.setItem('isLoggedin', String(true))
|
|
|
|
|
localStorage.setItem('verificato', String(verifiedEmail))
|
2018-11-02 20:10:45 +01:00
|
|
|
|
|
|
|
|
// dispatch('storeUser', authData);
|
|
|
|
|
// dispatch('setLogoutTimer', myres.data.expiresIn);
|
2018-11-02 22:15:48 +01:00
|
|
|
return ErroriMongoDb.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) {
|
2018-11-02 22:15:48 +01:00
|
|
|
console.log('ERROREEEEEEEEE')
|
|
|
|
|
console.log(error)
|
2018-11-02 15:56:29 +01:00
|
|
|
}
|
2018-11-02 22:15:48 +01:00
|
|
|
this.setServerCode(ErroriMongoDb.ERR_GENERICO)
|
|
|
|
|
return ErroriMongoDb.ERR_GENERICO
|
|
|
|
|
})
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
2018-11-06 00:21:22 +01:00
|
|
|
@Mutation
|
2018-11-03 01:23:39 +01:00
|
|
|
autologin () {
|
2018-11-02 22:15:48 +01:00
|
|
|
const token = localStorage.getItem('token')
|
2018-11-02 20:10:45 +01:00
|
|
|
if (!token) {
|
2018-11-02 22:15:48 +01:00
|
|
|
return
|
2018-11-02 15:56:29 +01:00
|
|
|
}
|
2018-11-02 22:15:48 +01:00
|
|
|
const expirationDateStr = localStorage.getItem('expirationDate')
|
|
|
|
|
let expirationDate = new Date(String(expirationDateStr))
|
|
|
|
|
const now = new Date()
|
2018-11-02 20:10:45 +01:00
|
|
|
if (now >= expirationDate) {
|
2018-11-02 22:15:48 +01:00
|
|
|
return
|
2018-11-02 15:56:29 +01:00
|
|
|
}
|
2018-11-02 22:15:48 +01:00
|
|
|
const userId = Number(localStorage.getItem('userId'))
|
|
|
|
|
const username = String(localStorage.getItem('username'))
|
|
|
|
|
const verifiedEmail = localStorage.getItem('verificato') === '1'
|
2018-11-02 20:10:45 +01:00
|
|
|
this.authUser({
|
|
|
|
|
username: username,
|
|
|
|
|
userId: userId,
|
|
|
|
|
idToken: token,
|
|
|
|
|
verifiedEmail: verifiedEmail
|
2018-11-02 22:15:48 +01:00
|
|
|
})
|
2018-11-02 20:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Action({ commit: types.USER_LOGOUT })
|
2018-11-03 01:23:39 +01:00
|
|
|
logout () {
|
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)
|
2018-11-02 20:10:45 +01:00
|
|
|
this.sendRequest(call, 'DELETE', usertosend)
|
|
|
|
|
.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-02 22:15:48 +01:00
|
|
|
this.clearAuthData()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
localStorage.removeItem('expirationDate')
|
|
|
|
|
localStorage.removeItem('token')
|
|
|
|
|
localStorage.removeItem('userId')
|
|
|
|
|
localStorage.removeItem('username')
|
|
|
|
|
localStorage.removeItem('isLoggedin')
|
|
|
|
|
localStorage.removeItem('verifiedEmail')
|
2018-11-02 20:10:45 +01:00
|
|
|
|
|
|
|
|
// router.replace('/signin')
|
|
|
|
|
}
|
2018-11-02 15:56:29 +01:00
|
|
|
|
|
|
|
|
}
|
2018-11-02 23:22:16 +01:00
|
|
|
|
|
|
|
|
export const UserModule = getModule(User.prototype)
|