Sfoglia il codice sorgente

:recycle: small tweaks | comments | etc

neo
toj 2 anni fa
parent
commit
8ebff56377

+ 9
- 11
frontend/src/router/guards.js Vedi File

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

+ 7
- 5
frontend/src/services/auth.service.js Vedi File

1
 import { db } from '../utils/db.js'
1
 import { db } from '../utils/db.js'
2
 
2
 
3
 class Authenticator {
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
     async verifySession(hashedToken) {
8
     async verifySession(hashedToken) {
8
         let verification
9
         let verification
9
         try {
10
         try {
17
     async createToken(req) {
18
     async createToken(req) {
18
         return await db.post('/user/token', req, true)
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
         const hashedSessionToken = this.grabStoredSessionToken()
23
         const hashedSessionToken = this.grabStoredSessionToken()
22
         let validation
24
         let validation
23
         try {
25
         try {
51
             )
53
             )
52
         return cookies[cookieKey]
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
         if (validatedToken.error)
58
         if (validatedToken.error)
57
             return console.error('ERROR :=>', validatedToken.error)
59
             return console.error('ERROR :=>', validatedToken.error)
58
         return validatedToken
60
         return validatedToken

+ 4
- 7
frontend/src/views/LoginView.vue Vedi File

39
     }),
39
     }),
40
     methods: {
40
     methods: {
41
         async login() {
41
         async login() {
42
-            const loginCredentials = {
43
-                user_email: this.form.email,
44
-                password: this.form.password,
45
-            }
46
             const credentials =
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
             // emailSentSuccessfully: emailSent.wasSuccessfull,
47
             // emailSentSuccessfully: emailSent.wasSuccessfull,
51
             const sessionInfo = await authenticator.sendEmail({
48
             const sessionInfo = await authenticator.sendEmail({
52
                 ...credentials.answered,
49
                 ...credentials.answered,

+ 2
- 2
frontend/src/views/OnboardingView.vue Vedi File

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

+ 5
- 14
frontend/src/views/VerifyView.vue Vedi File

13
         hash = this.$route.params.hashedToken
13
         hash = this.$route.params.hashedToken
14
         try {
14
         try {
15
             if (!hash) throw new Error('URL contains no hash!')
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
             currentProfile.login(
21
             currentProfile.login(
23
                 sessionData.profileId,
22
                 sessionData.profileId,
24
                 this.$waveui.notify,
23
                 this.$waveui.notify,
28
             console.error(err)
27
             console.error(err)
29
         }
28
         }
30
         this.$router.push('/')
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
 </script>
32
 </script>
42
 
33
 

Loading…
Annulla
Salva