Переглянути джерело

:recycle: Cleaned up cookie/auth logic

brian_dev_incorp_login
tomit4 2 роки тому
джерело
коміт
feeb298c66

+ 0
- 1
frontend/src/components/onboarding/Auth.vue Переглянути файл

47
             const accessToken = await this.getAccessToken({
47
             const accessToken = await this.getAccessToken({
48
                 ...this.answered,
48
                 ...this.answered,
49
             })
49
             })
50
-            console.log('accessToken :=>', accessToken)
51
             const sessionInfo = await authenticator.sendAuthEmail({
50
             const sessionInfo = await authenticator.sendAuthEmail({
52
                 ...this.answered,
51
                 ...this.answered,
53
                 accessToken: accessToken,
52
                 accessToken: accessToken,

+ 1
- 26
frontend/src/router/guards.js Переглянути файл

13
     }
13
     }
14
 }
14
 }
15
 
15
 
16
-// TODO: move to utils/index.js and import to this file, OnboardingView.vue and VerifyView.vue
17
-const grabStoredCookie = cookieKey => {
18
-    const cookies = document.cookie.split('; ').reduce((prev, current) => {
19
-        const [name, ...value] = current.split('=')
20
-        prev[name] = value.join('=')
21
-        return prev
22
-    }, {})
23
-    const cookieVal = cookieKey in cookies ? cookies[`${cookieKey}`] : undefined
24
-    return cookieVal
25
-}
26
-
27
-const verifySession = async () => {
28
-    const hashedAccessToken = grabStoredCookie('siimee_access')
29
-    if (!hashedAccessToken)
30
-        return console.warn('WARNING :=> accessToken is not defined')
31
-    const validatedToken = await authenticator.validateSession(
32
-        hashedAccessToken,
33
-    )
34
-    if (validatedToken.error) {
35
-        console.error('ERROR :=>', validatedToken.error)
36
-    } else {
37
-        return validatedToken
38
-    }
39
-}
40
-
41
 const loginIfToken = async () => {
16
 const loginIfToken = async () => {
42
-    const sessionData = await verifySession()
17
+    const sessionData = await authenticator.verifySessionCookie('siimee_access')
43
     if (
18
     if (
44
         sessionData?.profileId &&
19
         sessionData?.profileId &&
45
         sessionData?.accessToken &&
20
         sessionData?.accessToken &&

+ 23
- 0
frontend/src/services/auth.service.js Переглянути файл

22
     async removeSession(hashedAccessToken) {
22
     async removeSession(hashedAccessToken) {
23
         return await db.post('/user/removesession', hashedAccessToken, true)
23
         return await db.post('/user/removesession', hashedAccessToken, true)
24
     }
24
     }
25
+    grabStoredCookie(cookieKey) {
26
+        const cookies = document.cookie.split('; ').reduce((prev, current) => {
27
+            const [name, ...value] = current.split('=')
28
+            prev[name] = value.join('=')
29
+            return prev
30
+        }, {})
31
+        const cookieVal =
32
+            cookieKey in cookies ? cookies[`${cookieKey}`] : undefined
33
+        return cookieVal
34
+    }
35
+    async verifySessionCookie(sessionCookie) {
36
+        const hashedAccessToken = this.grabStoredCookie(sessionCookie)
37
+        if (!hashedAccessToken)
38
+            return console.warn('WARNING :=> accessToken is not defined')
39
+        const validatedToken = await authenticator.validateSession(
40
+            hashedAccessToken,
41
+        )
42
+        if (validatedToken.error) {
43
+            console.error('ERROR :=>', validatedToken.error)
44
+        } else {
45
+            return validatedToken
46
+        }
47
+    }
25
 }
48
 }
26
 const authenticator = new Authenticator()
49
 const authenticator = new Authenticator()
27
 
50
 

+ 0
- 26
frontend/src/utils/index.js Переглянути файл

130
     return surveyResponses
130
     return surveyResponses
131
 }
131
 }
132
 
132
 
133
-const grabStoredCookie = cookieKey => {
134
-    const cookies = document.cookie.split('; ').reduce((prev, current) => {
135
-        const [name, ...value] = current.split('=')
136
-        prev[name] = value.join('=')
137
-        return prev
138
-    }, {})
139
-    const cookieVal = cookieKey in cookies ? cookies[`${cookieKey}`] : undefined
140
-    return cookieVal
141
-}
142
-
143
-const verifySession = async () => {
144
-    const hashedAccessToken = grabStoredCookie('siimee_access')
145
-    if (!hashedAccessToken)
146
-        return console.warn('WARNING :=> accessToken is not defined')
147
-    const validatedToken = await authenticator.validateSession(
148
-        hashedAccessToken,
149
-    )
150
-    if (validatedToken.error) {
151
-        console.error('ERROR :=>', validatedToken.error)
152
-    } else {
153
-        return validatedToken
154
-    }
155
-}
156
-
157
 export {
133
 export {
158
     validatorMapping,
134
     validatorMapping,
159
     surveyFactory,
135
     surveyFactory,
165
     randomMedia,
141
     randomMedia,
166
     randomName,
142
     randomName,
167
     randomEmail,
143
     randomEmail,
168
-    grabStoredCookie,
169
-    verifySession,
170
 }
144
 }

+ 2
- 13
frontend/src/views/HomeView.vue Переглянути файл

95
             )
95
             )
96
             this.fetchedCards.push(...newQueue) // update fetchedCards => recalculate cards
96
             this.fetchedCards.push(...newQueue) // update fetchedCards => recalculate cards
97
         },
97
         },
98
-        grabStoredCookie(cookieKey) {
99
-            const cookies = document.cookie
100
-                .split('; ')
101
-                .reduce((prev, current) => {
102
-                    const [name, ...value] = current.split('=')
103
-                    prev[name] = value.join('=')
104
-                    return prev
105
-                }, {})
106
-            const cookieVal =
107
-                cookieKey in cookies ? cookies[`${cookieKey}`] : undefined
108
-            return cookieVal
109
-        },
110
         async logout() {
98
         async logout() {
111
             if (currentProfile.isLoggedIn) {
99
             if (currentProfile.isLoggedIn) {
112
                 currentProfile.logout()
100
                 currentProfile.logout()
113
             }
101
             }
114
-            const hashedAccessToken = this.grabStoredCookie('siimee_access')
102
+            const hashedAccessToken =
103
+                authenticator.grabStoredCookie('siimee_access')
115
             const removedSession = await authenticator.removeSession(
104
             const removedSession = await authenticator.removeSession(
116
                 hashedAccessToken,
105
                 hashedAccessToken,
117
             )
106
             )

+ 4
- 28
frontend/src/views/OnboardingView.vue Переглянути файл

38
 import { surveyFactory } from '@/utils'
38
 import { surveyFactory } from '@/utils'
39
 import stepViews from '@/components/onboarding'
39
 import stepViews from '@/components/onboarding'
40
 import SurveyCompleteView from './SurveyCompleteView.vue'
40
 import SurveyCompleteView from './SurveyCompleteView.vue'
41
-let hashedAccessToken = null
42
 
41
 
43
 export default {
42
 export default {
44
     name: 'OnboardingView',
43
     name: 'OnboardingView',
56
     }),
55
     }),
57
     async created() {
56
     async created() {
58
         this.survey = await surveyFactory.createSurvey()
57
         this.survey = await surveyFactory.createSurvey()
59
-        hashedAccessToken = this.grabStoredCookie('siimee_access')
60
         try {
58
         try {
61
-            const sessionData = await this.verifySession(hashedAccessToken)
59
+            const sessionData = await authenticator.verifySessionCookie(
60
+                'siimee_access',
61
+            )
62
             if (sessionData) {
62
             if (sessionData) {
63
                 await currentProfile.login(
63
                 await currentProfile.login(
64
                     sessionData.profileId,
64
                     sessionData.profileId,
83
         async goToStep(num) {
83
         async goToStep(num) {
84
             this.currentStep = num
84
             this.currentStep = num
85
         },
85
         },
86
-        grabStoredCookie(cookieKey) {
87
-            const cookies = document.cookie
88
-                .split('; ')
89
-                .reduce((prev, current) => {
90
-                    const [name, ...value] = current.split('=')
91
-                    prev[name] = value.join('=')
92
-                    return prev
93
-                }, {})
94
-            const cookieVal =
95
-                cookieKey in cookies ? cookies[`${cookieKey}`] : undefined
96
-            return cookieVal
97
-        },
98
-        async verifySession(hashedAccessToken) {
99
-            if (!hashedAccessToken)
100
-                return console.warn('WARNING :=> accessToken is not defined')
101
-            const validatedToken = await authenticator.validateSession(
102
-                hashedAccessToken,
103
-            )
104
-            if (validatedToken.error) {
105
-                throw new Error(validatedToken.error)
106
-            } else {
107
-                return validatedToken
108
-            }
109
-        },
110
         formatResponses(responses) {
86
         formatResponses(responses) {
111
             return responses.map(response => {
87
             return responses.map(response => {
112
                 return {
88
                 return {
140
                 )
116
                 )
141
                 currentProfile._profile.responses = this.responses
117
                 currentProfile._profile.responses = this.responses
142
                 try {
118
                 try {
143
-                    await this.verifySession(hashedAccessToken)
119
+                    await authenticator.verifySessionCookie('siimee_access')
144
                 } catch (err) {
120
                 } catch (err) {
145
                     this.currentStep = 0
121
                     this.currentStep = 0
146
                     this.goToStep(this.currentStep)
122
                     this.goToStep(this.currentStep)

+ 4
- 22
frontend/src/views/VerifyView.vue Переглянути файл

12
     name: 'VerifyView',
12
     name: 'VerifyView',
13
     async created() {
13
     async created() {
14
         hash = this.$route.params.hashedToken
14
         hash = this.$route.params.hashedToken
15
-        hashedAccessToken = this.grabCookie('siimee_access')
15
+        hashedAccessToken = authenticator.grabStoredCookie('siimee_access')
16
         try {
16
         try {
17
             this.isHashInUrl(hash)
17
             this.isHashInUrl(hash)
18
             await this.doesAccessTokenExist(hashedAccessToken)
18
             await this.doesAccessTokenExist(hashedAccessToken)
19
             await this.verifyActiveSession(hash)
19
             await this.verifyActiveSession(hash)
20
-            const sessionData = await this.isSessionTokenValid(hash)
20
+            const sessionData = await authenticator.verifySessionCookie(
21
+                'siimee_access',
22
+            )
21
             await currentProfile.login(
23
             await currentProfile.login(
22
                 sessionData.profileId,
24
                 sessionData.profileId,
23
                 this.$waveui.notify,
25
                 this.$waveui.notify,
29
         this.$router.push('/')
31
         this.$router.push('/')
30
     },
32
     },
31
     methods: {
33
     methods: {
32
-        grabCookie(cookieKey) {
33
-            const cookies = document.cookie
34
-                .split('; ')
35
-                .reduce((prev, current) => {
36
-                    const [name, ...value] = current.split('=')
37
-                    prev[name] = value.join('=')
38
-                    return prev
39
-                }, {})
40
-            return `${cookieKey}` in cookies
41
-                ? cookies[`${cookieKey}`]
42
-                : undefined
43
-        },
44
         isHashInUrl(hash) {
34
         isHashInUrl(hash) {
45
             if (!hash) throw new Error('URL contains no hash!')
35
             if (!hash) throw new Error('URL contains no hash!')
46
         },
36
         },
55
             if (!sessionData.hashesMatch)
45
             if (!sessionData.hashesMatch)
56
                 throw new Error('Hash is not in activeSessions!')
46
                 throw new Error('Hash is not in activeSessions!')
57
         },
47
         },
58
-        async isSessionTokenValid(hash) {
59
-            const sessionTokenIsValid = await authenticator.validateSession(
60
-                hash,
61
-            )
62
-            if (sessionTokenIsValid.error) {
63
-                throw new Error(sessionTokenIsValid.error)
64
-            } else return sessionTokenIsValid
65
-        },
66
     },
48
     },
67
 }
49
 }
68
 </script>
50
 </script>

Завантаження…
Відмінити
Зберегти