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