Procházet zdrojové kódy

:bug: Changes to address 'eternal cookies' bug

tags/0.0.3^2
tomit4 před 3 roky
rodič
revize
c9d70e6fda
1 změnil soubory, kde provedl 26 přidání a 24 odebrání
  1. 26
    24
      frontend/src/views/OnboardingView.vue

+ 26
- 24
frontend/src/views/OnboardingView.vue Zobrazit soubor

@@ -71,13 +71,16 @@ export default {
71 71
 
72 72
         if (document.cookie.length) {
73 73
             // TODO: Heavy Refactor needed, obvious code smells
74
-            // BUG: NEEDS BROWSER REFRESH TO SEE UPDATED COOKIES
74
+            // BUG: NEEDS BROWSER REFRESH AFTER VERIFYING EMAIL AND REDIRECT BACK TO ONBOARDING
75
+            // BUG: CURRENT IMPLEMENTATION HAS COOKIES THAT NEVER EXPIRE
75 76
             const siimeeAnswered = this.grabCookie('siimee_answered')
76 77
             const myCurrentStep = this.grabCookie('siimee_current_step')
77
-            console.log('myCurrentStep :=>', myCurrentStep)
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...
78 82
             if (siimeeAnswered) {
79 83
                 const siimeeAnswers = JSON.parse(siimeeAnswered)
80
-                this.sessionToken = this.grabCookie('siimee_session')
81 84
                 const sessionTokenIsValid =
82 85
                     await this.authenticator.validateJwt(this.sessionToken)
83 86
                 this.accessToken = this.grabCookie('siimee_access')
@@ -88,7 +91,6 @@ export default {
88 91
                         seeking: siimeeAnswers.seeking,
89 92
                     }
90 93
                     this.currentProfileId = siimeeAnswers.profile_id
91
-                    this.currentStep = 6
92 94
                     this.responses = [
93 95
                         { response_key_id: 8, val: siimeeAnswers.email },
94 96
                         { response_key_id: 7, val: siimeeAnswers.name },
@@ -102,21 +104,17 @@ export default {
102 104
                         this.responses,
103 105
                     )}; max-age=600 ; path=/onboarding ; secure`
104 106
                     document.cookie = 'siimee_answered='
107
+                    this.currentStep = 6
105 108
                     this.goToStep(this.currentStep)
106 109
                 }
107 110
             } else if (myCurrentStep) {
108
-                const myCurrentAnswers = this.grabCookie(
109
-                    'siimee_cache_answered',
110
-                )
111
-                const myCurrentResponses = this.grabCookie(
112
-                    'siimee_cache_responses',
113
-                )
114 111
                 this.answered = JSON.parse(myCurrentAnswers)
115 112
                 this.responses = JSON.parse(myCurrentResponses)
116 113
                 this.currentStep = myCurrentStep
117 114
                 this.goToStep(Number(myCurrentStep) + 1)
118 115
             } else {
119
-                this.goToStep(0)
116
+                this.currentStep = 0
117
+                this.goToStep(this.currentStep)
120 118
             }
121 119
         }
122 120
     },
@@ -124,16 +122,17 @@ export default {
124 122
         onSubmit() {
125 123
             console.log(JSON.stringify(this.answered))
126 124
         },
127
-        async goToStep(num) {
125
+        async goToStep(num, maxAge) {
126
+            maxAge = 600 // temp measure
128 127
             document.cookie = `siimee_current_step=${Number(
129 128
                 this.currentStep,
130
-            )}; max-age=600 ; path=/onboarding ; secure`
129
+            )}; max-age=${maxAge} ; path=/onboarding ; secure`
131 130
             document.cookie = `siimee_cache_answered=${JSON.stringify(
132 131
                 this.answered,
133
-            )}; max-age=600 ; path=/onboarding ; secure`
132
+            )}; max-age=${maxAge} ; path=/onboarding ; secure`
134 133
             document.cookie = `siimee_cache_responses=${JSON.stringify(
135 134
                 this.responses,
136
-            )}; max-age=600 ; path=/onboarding ; secure`
135
+            )}; max-age=${maxAge} ; path=/onboarding ; secure`
137 136
 
138 137
             if (num > 6) {
139 138
                 this.validateAccessToken()
@@ -145,7 +144,7 @@ export default {
145 144
             const validatedAccessToken = await this.authenticator.validateJwt(
146 145
                 this.accessToken,
147 146
             )
148
-            if (!validatedAccessToken.isValid) {
147
+            if (!validatedAccessToken || !validatedAccessToken.isValid) {
149 148
                 const sessionTokenIsValid = await this.validateSessionToken()
150 149
                 if (!sessionTokenIsValid) {
151 150
                     this.goToStep(0)
@@ -156,7 +155,7 @@ export default {
156 155
             const validatedSessionToken = await this.authenticator.validateJwt(
157 156
                 this.sessionToken,
158 157
             )
159
-            if (validatedSessionToken.isValid) {
158
+            if (!validatedSessionToken || validatedSessionToken.isValid) {
160 159
                 this.accessToken = await this.authenticator.generateJwt({
161 160
                     ...this.answered,
162 161
                     expiration: 60 * 3,
@@ -165,13 +164,16 @@ export default {
165 164
             } else return false
166 165
         },
167 166
         grabCookie(cookieKey) {
168
-            const cookieString = document.cookie
169
-            const cookies = cookieString.split('; ').reduce((prev, current) => {
170
-                const [name, ...value] = current.split('=')
171
-                prev[name] = value.join('=')
172
-                return prev
173
-            }, {})
174
-            return cookieKey in cookies ? cookies[`${cookieKey}`] : undefined
167
+            const cookies = document.cookie
168
+                .split('; ')
169
+                .reduce((prev, current) => {
170
+                    const [name, ...value] = current.split('=')
171
+                    prev[name] = value.join('=')
172
+                    return prev
173
+                }, {})
174
+            const cookieVal =
175
+                cookieKey in cookies ? cookies[`${cookieKey}`] : undefined
176
+            return cookieVal
175 177
         },
176 178
         async updateAnswers(payload) {
177 179
             if (payload) {

Načítá se…
Zrušit
Uložit