Przeglądaj źródła

:recycle: return error correctly from validate session endpoint

brian_auth_fix
j 2 lat temu
rodzic
commit
ac82b11a29

+ 1
- 1
backend/lib/routes/user/validate-session.js Wyświetl plik

88
                 return {
88
                 return {
89
                     ok: false,
89
                     ok: false,
90
                     handler: pluginConfig.handlerType,
90
                     handler: pluginConfig.handlerType,
91
-                    data: { error: err.message },
91
+                    data: { error: err },
92
                 }
92
                 }
93
             }
93
             }
94
         },
94
         },

+ 14
- 22
frontend/src/services/auth.service.js Wyświetl plik

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(credentials) {
5
-        return await db.post('/user/send-email/', credentials)
4
+    async sendEmail(answered) {
5
+        return await db.post('/user/send-email/', answered)
6
     }
6
     }
7
-    /** Check for session has not expired; Confirm session from email. */
8
     async verifySession(hashedToken) {
7
     async verifySession(hashedToken) {
9
         let verification
8
         let verification
10
         try {
9
         try {
12
         } catch (error) {
11
         } catch (error) {
13
             console.error(error)
12
             console.error(error)
14
         }
13
         }
15
-        console.log('verifiedSession :>> ', verification)
14
+        console.log('verification :>> ', verification)
16
         return verification
15
         return verification
17
     }
16
     }
18
     async createToken(req) {
17
     async createToken(req) {
19
         return await db.post('/user/token', req, true)
18
         return await db.post('/user/token', req, true)
20
     }
19
     }
21
-    /** Check if session still active in backend */
22
-    async #isValidSession() {
23
-        const hash = this.#getHashedToken()
20
+    async validateSession(hashedSessionToken) {
24
         let validation
21
         let validation
25
         try {
22
         try {
26
             validation = await db.post(
23
             validation = await db.post(
27
                 '/user/validate-session',
24
                 '/user/validate-session',
28
-                hash,
25
+                hashedSessionToken,
29
                 true,
26
                 true,
30
             )
27
             )
31
         } catch (error) {
28
         } catch (error) {
32
             console.error(error)
29
             console.error(error)
33
         }
30
         }
34
-        console.log('valid Session :>> ', validation)
35
         return validation
31
         return validation
36
     }
32
     }
37
     async authenticateLoginCredentials(credentials) {
33
     async authenticateLoginCredentials(credentials) {
38
         return await db.post('/user/login', credentials)
34
         return await db.post('/user/login', credentials)
39
     }
35
     }
40
     async removeSession() {
36
     async removeSession() {
41
-        const hash = this.#getHashedToken()
42
-        return await db.post('/user/remove-session', hash, true)
37
+        const hashedSessionToken = this.grabStoredSessionToken('siimee_session')
38
+        return await db.post('/user/remove-session', hashedSessionToken, true)
43
     }
39
     }
44
-    #getHashedToken(cookieKey = 'siimee_session') {
40
+    grabStoredSessionToken(cookieKey) {
45
         const cookies = document.cookie.split('; ').reduce((prev, current) => {
41
         const cookies = document.cookie.split('; ').reduce((prev, current) => {
46
             const [name, ...value] = current.split('=')
42
             const [name, ...value] = current.split('=')
47
             prev[name] = value.join('=')
43
             prev[name] = value.join('=')
48
             return prev
44
             return prev
49
         }, {})
45
         }, {})
50
-        console.log('cookies :>> ', cookies);
51
-        if (!cookies[cookieKey])
52
-            return console.warn(
53
-                'WARNING :=> accessToken is not defined; There was problem with session cookie you are not logged in.',
54
-            )
55
         return cookies[cookieKey]
46
         return cookies[cookieKey]
56
     }
47
     }
57
-    async checkSessionValid() {
58
-        const validation = await this.#isValidSession()
59
-        if (validation.error)
60
-            console.error('ERROR :=>', validation.error)
61
-        return validation
48
+    async verifySessionCookie(sessionCookieKey = 'siimee_session') {
49
+        const hashedAccessToken = this.grabStoredSessionToken(sessionCookieKey)
50
+        const validatedToken = await this.validateSession(hashedAccessToken)
51
+        if (validatedToken.error)
52
+            return console.error('ERROR :=>', validatedToken.error)
53
+        return validatedToken
62
     }
54
     }
63
 }
55
 }
64
 const authenticator = new Authenticator()
56
 const authenticator = new Authenticator()

Ładowanie…
Anuluj
Zapisz