Selaa lähdekoodia

:recycle: small tweaks | comments | etc

neo
toj 2 vuotta sitten
vanhempi
commit
8ebff56377

+ 9
- 11
frontend/src/router/guards.js Näytä tiedosto

@@ -15,11 +15,10 @@ async function log(to) {
15 15
 }
16 16
 
17 17
 const loginIfToken = async () => {
18
-    const sessionData = await authenticator.verifySessionCookie()
18
+    const sessionData = await authenticator.checkSessionValid()
19 19
     if (
20 20
         sessionData?.profileId &&
21
-        sessionData?.sessionToken &&
22
-        !currentProfile.isLoggedIn
21
+        sessionData?.sessionToken
23 22
     ) {
24 23
         await currentProfile.login(
25 24
             sessionData.profileId,
@@ -30,22 +29,21 @@ const loginIfToken = async () => {
30 29
 }
31 30
 
32 31
 const checkLoginStatus = async (destination, nextCb) => {
33
-    await loginIfToken()
32
+    if(!currentProfile.isLoggedIn) {
33
+        await loginIfToken()
34
+    }
34 35
     log(destination)
35 36
     if (DEV_MODE) {
36 37
         nextCb()
37 38
     } else if (
38
-        destination.meta.requiresCompleteProfile &&
39
-        !currentProfile.isLoggedIn &&
40
-        !currentProfile.isComplete
39
+        !currentProfile.isLoggedIn
41 40
     ) {
42
-        nextCb('/onboarding')
41
+        nextCb('/login')
43 42
     } else if (
44 43
         destination.meta.requiresCompleteProfile &&
45
-        destination.meta.requiresAuth &&
46
-        !currentProfile.isLoggedIn
44
+        destination.meta.requiresAuth
47 45
     ) {
48
-        nextCb('/login')
46
+        nextCb('/onboarding')
49 47
     } else {
50 48
         nextCb()
51 49
     }

+ 7
- 5
frontend/src/services/auth.service.js Näytä tiedosto

@@ -1,9 +1,10 @@
1 1
 import { db } from '../utils/db.js'
2 2
 
3 3
 class Authenticator {
4
-    async sendEmail(answered) {
5
-        return await db.post('/user/send-email/', answered)
4
+    async sendEmail(credentials) {
5
+        return await db.post('/user/send-email/', credentials)
6 6
     }
7
+    /** Check for session has not expired; Confirm session from email. */
7 8
     async verifySession(hashedToken) {
8 9
         let verification
9 10
         try {
@@ -17,7 +18,8 @@ class Authenticator {
17 18
     async createToken(req) {
18 19
         return await db.post('/user/token', req, true)
19 20
     }
20
-    async validateSession() {
21
+    /** Check for session existence in backend */
22
+    async #validateSession() {
21 23
         const hashedSessionToken = this.grabStoredSessionToken()
22 24
         let validation
23 25
         try {
@@ -51,8 +53,8 @@ class Authenticator {
51 53
             )
52 54
         return cookies[cookieKey]
53 55
     }
54
-    async verifySessionCookie() {
55
-        const validatedToken = await this.validateSession()
56
+    async checkSessionValid() {
57
+        const validatedToken = await this.#validateSession()
56 58
         if (validatedToken.error)
57 59
             return console.error('ERROR :=>', validatedToken.error)
58 60
         return validatedToken

+ 4
- 7
frontend/src/views/LoginView.vue Näytä tiedosto

@@ -39,14 +39,11 @@ export default {
39 39
     }),
40 40
     methods: {
41 41
         async login() {
42
-            const loginCredentials = {
43
-                user_email: this.form.email,
44
-                password: this.form.password,
45
-            }
46 42
             const credentials =
47
-                await authenticator.authenticateLoginCredentials(
48
-                    loginCredentials,
49
-                )
43
+                await authenticator.authenticateLoginCredentials({
44
+                    user_email: this.form.email,
45
+                    password: this.form.password,
46
+                })
50 47
             // emailSentSuccessfully: emailSent.wasSuccessfull,
51 48
             const sessionInfo = await authenticator.sendEmail({
52 49
                 ...credentials.answered,

+ 2
- 2
frontend/src/views/OnboardingView.vue Näytä tiedosto

@@ -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.verifySessionCookie()
63
+            const sessionData = await authenticator.checkSessionValid()
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.verifySessionCookie()
116
+                    await authenticator.checkSessionValid()
117 117
                 } catch (err) {
118 118
                     this.currentStep = 0
119 119
                     this.goToStep(this.currentStep)

+ 5
- 14
frontend/src/views/VerifyView.vue Näytä tiedosto

@@ -13,12 +13,11 @@ export default {
13 13
         hash = this.$route.params.hashedToken
14 14
         try {
15 15
             if (!hash) throw new Error('URL contains no hash!')
16
-            const verifiedFromUrlHash = await this.verifyActiveSession(hash)
17
-            console.log('verifiedFromUrlHash :>> ', verifiedFromUrlHash)
18
-            const sessionData = await authenticator.verifySessionCookie()
19
-            if (!sessionData)
20
-                throw new Error(`Could not verify session from cookie.`)
16
+            const sessionData = await authenticator.verifySession(hash)
17
+            if (!sessionData.hashesMatch)
18
+                throw new Error('Hash is not in activeSessions!')
21 19
 
20
+            await authenticator.checkSessionValid()
22 21
             currentProfile.login(
23 22
                 sessionData.profileId,
24 23
                 this.$waveui.notify,
@@ -28,15 +27,7 @@ export default {
28 27
             console.error(err)
29 28
         }
30 29
         this.$router.push('/')
31
-    },
32
-    methods: {
33
-        async verifyActiveSession(hashedToken) {
34
-            const sessionData = await authenticator.verifySession(hashedToken)
35
-            if (!sessionData.hashesMatch)
36
-                throw new Error('Hash is not in activeSessions!')
37
-            return sessionData
38
-        },
39
-    },
30
+    }
40 31
 }
41 32
 </script>
42 33
 

Loading…
Peruuta
Tallenna