|
|
@@ -1,10 +1,9 @@
|
|
1
|
1
|
import { db } from '../utils/db.js'
|
|
2
|
2
|
|
|
3
|
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
|
7
|
async verifySession(hashedToken) {
|
|
9
|
8
|
let verification
|
|
10
|
9
|
try {
|
|
|
@@ -12,53 +11,46 @@ class Authenticator {
|
|
12
|
11
|
} catch (error) {
|
|
13
|
12
|
console.error(error)
|
|
14
|
13
|
}
|
|
15
|
|
- console.log('verifiedSession :>> ', verification)
|
|
|
14
|
+ console.log('verification :>> ', verification)
|
|
16
|
15
|
return verification
|
|
17
|
16
|
}
|
|
18
|
17
|
async createToken(req) {
|
|
19
|
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
|
21
|
let validation
|
|
25
|
22
|
try {
|
|
26
|
23
|
validation = await db.post(
|
|
27
|
24
|
'/user/validate-session',
|
|
28
|
|
- hash,
|
|
|
25
|
+ hashedSessionToken,
|
|
29
|
26
|
true,
|
|
30
|
27
|
)
|
|
31
|
28
|
} catch (error) {
|
|
32
|
29
|
console.error(error)
|
|
33
|
30
|
}
|
|
34
|
|
- console.log('valid Session :>> ', validation)
|
|
35
|
31
|
return validation
|
|
36
|
32
|
}
|
|
37
|
33
|
async authenticateLoginCredentials(credentials) {
|
|
38
|
34
|
return await db.post('/user/login', credentials)
|
|
39
|
35
|
}
|
|
40
|
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
|
41
|
const cookies = document.cookie.split('; ').reduce((prev, current) => {
|
|
46
|
42
|
const [name, ...value] = current.split('=')
|
|
47
|
43
|
prev[name] = value.join('=')
|
|
48
|
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
|
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
|
56
|
const authenticator = new Authenticator()
|