|
|
@@ -35,6 +35,11 @@ main.view--onboarding
|
|
35
|
35
|
|
|
36
|
36
|
<script>
|
|
37
|
37
|
import { Authenticator } from '../services/auth.service.js'
|
|
|
38
|
+import { fetchUserByEmail } from '../services/user.service.js'
|
|
|
39
|
+import {
|
|
|
40
|
+ fetchProfilesByUserId,
|
|
|
41
|
+ fetchProfileByProfileId,
|
|
|
42
|
+} from '../services/profile.service.js'
|
|
38
|
43
|
import { surveyFactory } from '@/utils'
|
|
39
|
44
|
import { currentProfile } from '../services/'
|
|
40
|
45
|
import stepViews from '@/components/onboarding'
|
|
|
@@ -54,11 +59,8 @@ export default {
|
|
54
|
59
|
responses: [],
|
|
55
|
60
|
currentStep: 0,
|
|
56
|
61
|
survey: null,
|
|
57
|
|
- currentProfileId: null,
|
|
58
|
62
|
invalidResponse: false,
|
|
59
|
63
|
authenticator: {},
|
|
60
|
|
- sessionToken: '',
|
|
61
|
|
- accessToken: '',
|
|
62
|
64
|
}),
|
|
63
|
65
|
computed: {
|
|
64
|
66
|
cP() {
|
|
|
@@ -68,53 +70,38 @@ export default {
|
|
68
|
70
|
async created() {
|
|
69
|
71
|
this.survey = await surveyFactory.createSurvey()
|
|
70
|
72
|
this.authenticator = new Authenticator()
|
|
71
|
|
-
|
|
72
|
73
|
if (document.cookie.length) {
|
|
73
|
|
- // TODO: Heavy Refactor needed, obvious code smells
|
|
74
|
|
- // BUG: NEEDS BROWSER REFRESH AFTER VERIFYING EMAIL AND REDIRECT BACK TO ONBOARDING
|
|
75
|
|
- // BUG: CURRENT IMPLEMENTATION HAS COOKIES THAT NEVER EXPIRE
|
|
76
|
|
- const siimeeAnswered = this.grabCookie('siimee_answered')
|
|
77
|
|
- const myCurrentStep = this.grabCookie('siimee_current_step')
|
|
78
|
|
- const myCurrentAnswers = this.grabCookie('siimee_cache_answered')
|
|
79
|
|
- const myCurrentResponses = this.grabCookie('siimee_cache_responses')
|
|
80
|
|
- this.sessionToken = this.grabCookie('siimee_session') || ''
|
|
81
|
|
- // TODO: START REFACTOR HERE...
|
|
82
|
|
- if (siimeeAnswered) {
|
|
83
|
|
- const siimeeAnswers = JSON.parse(siimeeAnswered)
|
|
84
|
|
- const sessionTokenIsValid =
|
|
85
|
|
- await this.authenticator.validateJwt(this.sessionToken)
|
|
86
|
|
- this.accessToken = this.grabCookie('siimee_access')
|
|
87
|
|
- if (sessionTokenIsValid.isValid) {
|
|
88
|
|
- this.answered = {
|
|
89
|
|
- name: siimeeAnswers.name,
|
|
90
|
|
- email: siimeeAnswers.email,
|
|
91
|
|
- seeking: siimeeAnswers.seeking,
|
|
|
74
|
+ const sessionToken = this.grabCookie('siimee_session_onboarding')
|
|
|
75
|
+ if (sessionToken) {
|
|
|
76
|
+ const sessionData = await this.authenticator.validateJwt(
|
|
|
77
|
+ sessionToken,
|
|
|
78
|
+ )
|
|
|
79
|
+ // NOTE: Left off here, INCOMPLETE, no ACCESS TOKEN yet, crazy amount of logic here...
|
|
|
80
|
+ if (sessionToken.isValid) {
|
|
|
81
|
+ const userEmail = sessionData.payload.email
|
|
|
82
|
+ const emailIsInCache =
|
|
|
83
|
+ await this.authenticator.checkEmailCache(userEmail)
|
|
|
84
|
+ if (emailIsInCache) {
|
|
|
85
|
+ const user = await fetchUserByEmail(userEmail)
|
|
|
86
|
+ const userId = user.user_id
|
|
|
87
|
+ const profilesFromUserId = await fetchProfilesByUserId(
|
|
|
88
|
+ userId,
|
|
|
89
|
+ )
|
|
|
90
|
+ let profileId
|
|
|
91
|
+ if (profilesFromUserId.length === 1) {
|
|
|
92
|
+ profileId = profilesFromUserId[0].profile_id
|
|
|
93
|
+ }
|
|
|
94
|
+ const profile = await fetchProfileByProfileId(profileId)
|
|
|
95
|
+ profile.responses.forEach(response => {
|
|
|
96
|
+ this.responses.push({
|
|
|
97
|
+ response_key_id: response.response_key_id,
|
|
|
98
|
+ val: response.val,
|
|
|
99
|
+ })
|
|
|
100
|
+ })
|
|
|
101
|
+ this.currentStep = 6
|
|
|
102
|
+ this.goToStep(this.currentStep)
|
|
92
|
103
|
}
|
|
93
|
|
- this.currentProfileId = siimeeAnswers.profile_id
|
|
94
|
|
- this.responses = [
|
|
95
|
|
- { response_key_id: 8, val: siimeeAnswers.email },
|
|
96
|
|
- { response_key_id: 7, val: siimeeAnswers.name },
|
|
97
|
|
- { response_key_id: 11, val: siimeeAnswers.seeking },
|
|
98
|
|
- ]
|
|
99
|
|
- document.cookie = `siimee_current_step=${this.currentStep}; max-age=600 ; path=/onboarding ; secure`
|
|
100
|
|
- document.cookie = `siimee_cache_answered=${JSON.stringify(
|
|
101
|
|
- this.answered,
|
|
102
|
|
- )}; max-age=600 ; path=/onboarding ; secure`
|
|
103
|
|
- document.cookie = `siimee_cache_responses=${JSON.stringify(
|
|
104
|
|
- this.responses,
|
|
105
|
|
- )}; max-age=600 ; path=/onboarding ; secure`
|
|
106
|
|
- document.cookie = 'siimee_answered='
|
|
107
|
|
- this.currentStep = 6
|
|
108
|
|
- this.goToStep(this.currentStep)
|
|
109
|
104
|
}
|
|
110
|
|
- } else if (myCurrentStep) {
|
|
111
|
|
- this.answered = JSON.parse(myCurrentAnswers)
|
|
112
|
|
- this.responses = JSON.parse(myCurrentResponses)
|
|
113
|
|
- this.currentStep = myCurrentStep
|
|
114
|
|
- this.goToStep(Number(myCurrentStep) + 1)
|
|
115
|
|
- } else {
|
|
116
|
|
- this.currentStep = 0
|
|
117
|
|
- this.goToStep(this.currentStep)
|
|
118
|
105
|
}
|
|
119
|
106
|
}
|
|
120
|
107
|
},
|
|
|
@@ -122,47 +109,9 @@ export default {
|
|
122
|
109
|
onSubmit() {
|
|
123
|
110
|
console.log(JSON.stringify(this.answered))
|
|
124
|
111
|
},
|
|
125
|
|
- async goToStep(num, maxAge) {
|
|
126
|
|
- maxAge = 600 // temp measure
|
|
127
|
|
- document.cookie = `siimee_current_step=${Number(
|
|
128
|
|
- this.currentStep,
|
|
129
|
|
- )}; max-age=${maxAge} ; path=/onboarding ; secure`
|
|
130
|
|
- document.cookie = `siimee_cache_answered=${JSON.stringify(
|
|
131
|
|
- this.answered,
|
|
132
|
|
- )}; max-age=${maxAge} ; path=/onboarding ; secure`
|
|
133
|
|
- document.cookie = `siimee_cache_responses=${JSON.stringify(
|
|
134
|
|
- this.responses,
|
|
135
|
|
- )}; max-age=${maxAge} ; path=/onboarding ; secure`
|
|
136
|
|
-
|
|
137
|
|
- if (num > 6) {
|
|
138
|
|
- this.validateAccessToken()
|
|
139
|
|
- }
|
|
|
112
|
+ async goToStep(num) {
|
|
140
|
113
|
this.currentStep = num
|
|
141
|
114
|
},
|
|
142
|
|
- // TODO: Refactor to use cookie's max-age attribute instead of network call for jwt auth
|
|
143
|
|
- async validateAccessToken() {
|
|
144
|
|
- const validatedAccessToken = await this.authenticator.validateJwt(
|
|
145
|
|
- this.accessToken,
|
|
146
|
|
- )
|
|
147
|
|
- if (!validatedAccessToken || !validatedAccessToken.isValid) {
|
|
148
|
|
- const sessionTokenIsValid = await this.validateSessionToken()
|
|
149
|
|
- if (!sessionTokenIsValid) {
|
|
150
|
|
- this.goToStep(0)
|
|
151
|
|
- }
|
|
152
|
|
- }
|
|
153
|
|
- },
|
|
154
|
|
- async validateSessionToken() {
|
|
155
|
|
- const validatedSessionToken = await this.authenticator.validateJwt(
|
|
156
|
|
- this.sessionToken,
|
|
157
|
|
- )
|
|
158
|
|
- if (!validatedSessionToken || validatedSessionToken.isValid) {
|
|
159
|
|
- this.accessToken = await this.authenticator.generateJwt({
|
|
160
|
|
- ...this.answered,
|
|
161
|
|
- expiration: 60 * 3,
|
|
162
|
|
- })
|
|
163
|
|
- return true
|
|
164
|
|
- } else return false
|
|
165
|
|
- },
|
|
166
|
115
|
grabCookie(cookieKey) {
|
|
167
|
116
|
const cookies = document.cookie
|
|
168
|
117
|
.split('; ')
|
|
|
@@ -194,6 +143,8 @@ export default {
|
|
194
|
143
|
response.response_key_id = payload.question.response_key_id
|
|
195
|
144
|
response.val = payload.input
|
|
196
|
145
|
this.responses.push(response)
|
|
|
146
|
+ console.log('this.answered :=>', this.answered)
|
|
|
147
|
+ console.log('this.responses :=>', this.responses)
|
|
197
|
148
|
|
|
198
|
149
|
// sends latest survey response to db
|
|
199
|
150
|
if (this.currentProfileId) {
|