Преглед изворни кода

:recycle: trim down auth service

brian_auth_fix
j пре 2 година
родитељ
комит
d7da98db0a

+ 2
- 5
frontend/src/router/guards.js Прегледај датотеку

15
 }
15
 }
16
 
16
 
17
 const loginIfToken = async () => {
17
 const loginIfToken = async () => {
18
-    const sessionData = await authenticator.checkSessionValid()
18
+    const sessionData = await authenticator.isValidSession()
19
     if (
19
     if (
20
         sessionData?.profileId &&
20
         sessionData?.profileId &&
21
         sessionData?.sessionToken &&
21
         sessionData?.sessionToken &&
40
         !currentProfile.isComplete
40
         !currentProfile.isComplete
41
     ) {
41
     ) {
42
         nextCb('/onboarding')
42
         nextCb('/onboarding')
43
-    } else if (
44
-        destination.meta.requiresAuth &&
45
-        !currentProfile.isLoggedIn
46
-    ) {
43
+    } else if (destination.meta.requiresAuth && !currentProfile.isLoggedIn) {
47
         nextCb('/login')
44
         nextCb('/login')
48
     } else {
45
     } else {
49
         nextCb()
46
         nextCb()

+ 5
- 11
frontend/src/services/auth.service.js Прегледај датотеку

19
         return await db.post('/user/token', req, true)
19
         return await db.post('/user/token', req, true)
20
     }
20
     }
21
     /** Check if session still active in backend */
21
     /** Check if session still active in backend */
22
-    async #isValidSession() {
23
-        const hash = this.#getHashedToken()
22
+    async isValidSession() {
23
+        const hash = this.getHashedToken()
24
         let validation
24
         let validation
25
         try {
25
         try {
26
             validation = await db.post('/user/validate-session', hash, true)
26
             validation = await db.post('/user/validate-session', hash, true)
27
         } catch (error) {
27
         } catch (error) {
28
-            console.error(error)
28
+            console.error(`Invalid session: ${error}`)
29
         }
29
         }
30
         console.log('valid Session :>> ', validation)
30
         console.log('valid Session :>> ', validation)
31
         return validation
31
         return validation
34
         return await db.post('/user/login', credentials)
34
         return await db.post('/user/login', credentials)
35
     }
35
     }
36
     async removeSession() {
36
     async removeSession() {
37
-        const hash = this.#getHashedToken()
37
+        const hash = this.getHashedToken()
38
         return await db.post('/user/remove-session', hash, true)
38
         return await db.post('/user/remove-session', hash, true)
39
     }
39
     }
40
-    #getHashedToken(cookieKey = 'siimee_session') {
40
+    getHashedToken(cookieKey = 'siimee_session') {
41
         const cookies = document.cookie.split('; ').reduce((prev, current) => {
41
         const cookies = document.cookie.split('; ').reduce((prev, current) => {
42
             const [name, ...value] = current.split('=')
42
             const [name, ...value] = current.split('=')
43
             prev[name] = value.join('=')
43
             prev[name] = value.join('=')
49
             )
49
             )
50
         return cookies[cookieKey]
50
         return cookies[cookieKey]
51
     }
51
     }
52
-    async checkSessionValid() {
53
-        const validation = await this.#isValidSession()
54
-        if (validation.error)
55
-            return console.error('ERROR :=>', validation.error)
56
-        return validation
57
-    }
58
 }
52
 }
59
 const authenticator = new Authenticator()
53
 const authenticator = new Authenticator()
60
 
54
 

+ 2
- 2
frontend/src/views/OnboardingView.vue Прегледај датотеку

60
         // TODO: Troubleshoot bug where not all responses are returned at SurveyCompleteView
60
         // TODO: Troubleshoot bug where not all responses are returned at SurveyCompleteView
61
         this.survey = await surveyFactory.createSurvey()
61
         this.survey = await surveyFactory.createSurvey()
62
         try {
62
         try {
63
-            const sessionData = await authenticator.checkSessionValid()
63
+            const sessionData = await authenticator.isValidSession()
64
             if (sessionData) {
64
             if (sessionData) {
65
                 this.responses = this.formatResponses(
65
                 this.responses = this.formatResponses(
66
                     currentProfile._profile.responses,
66
                     currentProfile._profile.responses,
113
                 )
113
                 )
114
                 currentProfile._profile.responses = this.responses
114
                 currentProfile._profile.responses = this.responses
115
                 try {
115
                 try {
116
-                    await authenticator.checkSessionValid()
116
+                    await authenticator.isValidSession()
117
                 } catch (err) {
117
                 } catch (err) {
118
                     this.currentStep = 0
118
                     this.currentStep = 0
119
                     this.goToStep(this.currentStep)
119
                     this.goToStep(this.currentStep)

Loading…
Откажи
Сачувај