|
|
@@ -71,10 +71,7 @@ export default {
|
|
71
|
71
|
accessToken = this.grabStoredCookie('siimee_access')
|
|
72
|
72
|
// TODO: More graceful way of throwing exceptions if sessionData is not defined??
|
|
73
|
73
|
try {
|
|
74
|
|
- const sessionData = await this.verifyBothTokens(
|
|
75
|
|
- sessionToken,
|
|
76
|
|
- accessToken,
|
|
77
|
|
- )
|
|
|
74
|
+ const sessionData = await this.verifyBothTokens()
|
|
78
|
75
|
await this.isEmailInRegistry(sessionData.payload.email)
|
|
79
|
76
|
// TODO: Validate All routes hit by these methods using tokens in headers
|
|
80
|
77
|
const userId = await this.grabUserIdByEmail(
|
|
|
@@ -110,7 +107,7 @@ export default {
|
|
110
|
107
|
cookieKey in cookies ? cookies[`${cookieKey}`] : undefined
|
|
111
|
108
|
return cookieVal
|
|
112
|
109
|
},
|
|
113
|
|
- async verifyBothTokens(sessionToken, accessToken) {
|
|
|
110
|
+ async verifyBothTokens() {
|
|
114
|
111
|
const sessionTokenIsValid = await this.verifySessionToken(
|
|
115
|
112
|
sessionToken,
|
|
116
|
113
|
)
|
|
|
@@ -122,22 +119,23 @@ export default {
|
|
122
|
119
|
console.warn(
|
|
123
|
120
|
'WARNING :=> Access Token Expired, but Session Token Is Still Valid, reissuing Access Token...',
|
|
124
|
121
|
)
|
|
125
|
|
- // NOTE: Whether to implement newSessionToken is unclear in notes,
|
|
126
|
|
- // but without this, user session will expire in 10 minutes no matter what...
|
|
127
|
|
- const newSessionToken =
|
|
128
|
|
- await this.authenticator.getSessionToken(
|
|
129
|
|
- sessionTokenIsValid.payload,
|
|
130
|
|
- )
|
|
|
122
|
+ // TODO: break out reissuing new tokens into separate _function
|
|
131
|
123
|
const newAccessToken = await this.authenticator.getAccessToken(
|
|
132
|
124
|
sessionTokenIsValid.payload,
|
|
133
|
125
|
)
|
|
134
|
|
- sessionToken = newSessionToken
|
|
135
|
|
- accessToken = newAccessToken
|
|
136
|
|
- document.cookie = `siimee_access=${newSessionToken}; max-age=600; path=/; secure`
|
|
137
|
|
- document.cookie = `siimee_access=${newAccessToken}; max-age=600; path=/; secure`
|
|
138
|
126
|
const newAccessTokenIsValid = await this.verifyAccessToken(
|
|
139
|
127
|
newAccessToken,
|
|
140
|
128
|
)
|
|
|
129
|
+ accessToken = newAccessToken
|
|
|
130
|
+ document.cookie = `siimee_access=${newAccessToken}; max-age=600; path=/; secure`
|
|
|
131
|
+ // NOTE: Resetting Session Token otherwise session
|
|
|
132
|
+ // token will always expire after 10 minutes...???
|
|
|
133
|
+ const newSessionToken =
|
|
|
134
|
+ await this.authenticator.getSessionToken(
|
|
|
135
|
+ sessionTokenIsValid.payload,
|
|
|
136
|
+ )
|
|
|
137
|
+ sessionToken = newSessionToken
|
|
|
138
|
+ document.cookie = `siimee_session=${newSessionToken}; max-age=600; path=/; secure`
|
|
141
|
139
|
return newAccessTokenIsValid
|
|
142
|
140
|
} else if (
|
|
143
|
141
|
accessTokenIsValid.status === 401 &&
|
|
|
@@ -184,20 +182,21 @@ export default {
|
|
184
|
182
|
},
|
|
185
|
183
|
async grabProfileIdByUserId(userId) {
|
|
186
|
184
|
const profilesFromUserId = await fetchProfilesByUserId(userId)
|
|
187
|
|
- if (profilesFromUserId.length === 1) {
|
|
|
185
|
+ if (
|
|
|
186
|
+ profilesFromUserId.length === 1 &&
|
|
|
187
|
+ profilesFromUserId.status !== 401
|
|
|
188
|
+ ) {
|
|
188
|
189
|
return profilesFromUserId[0].profile_id
|
|
189
|
|
- } else {
|
|
|
190
|
+ } else if (profilesFromUserId.length > 1) {
|
|
190
|
191
|
// TODO: Refactor once more is known on users with multiple profiles
|
|
191
|
|
- console.error(
|
|
192
|
|
- 'ERROR :=> Multiple Profiles for this User ID',
|
|
193
|
|
- profilesFromUserId,
|
|
194
|
|
- )
|
|
195
|
192
|
throw new Error('Multiple Profiles for this User ID')
|
|
|
193
|
+ } else {
|
|
|
194
|
+ throw new Error('No Profile for User ID found')
|
|
196
|
195
|
}
|
|
197
|
196
|
},
|
|
198
|
197
|
async grabProfileByProfileId(profileId) {
|
|
199
|
198
|
const profile = await fetchProfileByProfileId(profileId)
|
|
200
|
|
- if (!profile) {
|
|
|
199
|
+ if (!profile || profile.status === 401) {
|
|
201
|
200
|
throw new Error(`No Profile Found for profileId ${profileId}`)
|
|
202
|
201
|
} else {
|
|
203
|
202
|
return profile
|
|
|
@@ -206,7 +205,7 @@ export default {
|
|
206
|
205
|
async grabResponsesByProfileId(profileId) {
|
|
207
|
206
|
const responses = []
|
|
208
|
207
|
const profile = await this.grabProfileByProfileId(profileId)
|
|
209
|
|
- if (!profile.responses.length) {
|
|
|
208
|
+ if (!profile.responses.length || profile.responses.status === 401) {
|
|
210
|
209
|
throw new Error(`No Responses Found for profileId ${profileId}`)
|
|
211
|
210
|
} else {
|
|
212
|
211
|
profile.responses.forEach(response => {
|
|
|
@@ -234,25 +233,23 @@ export default {
|
|
234
|
233
|
response.response_key_id = payload.question.response_key_id
|
|
235
|
234
|
response.val = payload.input
|
|
236
|
235
|
this.responses.push(response)
|
|
237
|
|
-
|
|
238
|
|
- // TODO: Validate this route using tokens in headers
|
|
239
|
|
- // TODO: Set check via methods to see if tokens are still valid,
|
|
240
|
|
- // if BOTH tokens are NOT valid,
|
|
241
|
|
- // currentProfileId = null and this.currentStep = 0
|
|
242
|
|
- if (currentProfileId) {
|
|
243
|
|
- await surveyFactory.addNewSurveyAnswer(
|
|
244
|
|
- this.responses[this.responses.length - 1],
|
|
245
|
|
- currentProfileId,
|
|
246
|
|
- )
|
|
247
|
|
- try {
|
|
248
|
|
- this.verifyBothTokens(sessionToken, accessToken)
|
|
249
|
|
- } catch (err) {
|
|
250
|
|
- console.error('ERROR :=>', err)
|
|
251
|
|
- this.goToStep(0)
|
|
252
|
|
- }
|
|
253
|
|
- }
|
|
254
|
236
|
if (k === 'aspects') return
|
|
255
|
237
|
}
|
|
|
238
|
+ if (currentProfileId) {
|
|
|
239
|
+ // TODO: Still have to authenticate this route
|
|
|
240
|
+ await surveyFactory.addNewSurveyAnswer(
|
|
|
241
|
+ this.responses[this.responses.length - 1],
|
|
|
242
|
+ currentProfileId,
|
|
|
243
|
+ accessToken,
|
|
|
244
|
+ )
|
|
|
245
|
+ try {
|
|
|
246
|
+ await this.verifyBothTokens(sessionToken, accessToken)
|
|
|
247
|
+ } catch (err) {
|
|
|
248
|
+ this.currentStep = 0
|
|
|
249
|
+ this.goToStep(this.currentStep)
|
|
|
250
|
+ throw new Error(err)
|
|
|
251
|
+ }
|
|
|
252
|
+ }
|
|
256
|
253
|
if (this.currentStep > this.survey.steps.length) {
|
|
257
|
254
|
this.onSubmit(this.answered)
|
|
258
|
255
|
} else {
|