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

:recycle: trim down auth service

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

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

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

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

@@ -19,13 +19,13 @@ class Authenticator {
19 19
         return await db.post('/user/token', req, true)
20 20
     }
21 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 24
         let validation
25 25
         try {
26 26
             validation = await db.post('/user/validate-session', hash, true)
27 27
         } catch (error) {
28
-            console.error(error)
28
+            console.error(`Invalid session: ${error}`)
29 29
         }
30 30
         console.log('valid Session :>> ', validation)
31 31
         return validation
@@ -34,10 +34,10 @@ class Authenticator {
34 34
         return await db.post('/user/login', credentials)
35 35
     }
36 36
     async removeSession() {
37
-        const hash = this.#getHashedToken()
37
+        const hash = this.getHashedToken()
38 38
         return await db.post('/user/remove-session', hash, true)
39 39
     }
40
-    #getHashedToken(cookieKey = 'siimee_session') {
40
+    getHashedToken(cookieKey = 'siimee_session') {
41 41
         const cookies = document.cookie.split('; ').reduce((prev, current) => {
42 42
             const [name, ...value] = current.split('=')
43 43
             prev[name] = value.join('=')
@@ -49,12 +49,6 @@ class Authenticator {
49 49
             )
50 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 53
 const authenticator = new Authenticator()
60 54
 

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

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

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