| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- import { db } from '../utils/db.js'
-
- class Authenticator {
- async sendEmail(answered) {
- return await db.post('/user/send-email/', answered)
- }
- /** Check for session has not expired; Confirm session from email. */
- async verifySession(hashedToken) {
- let verification
- try {
- verification = await db.get(`/user/verify/${hashedToken}`)
- } catch (error) {
- console.error(error)
- }
- console.log('verifiedSession :>> ', verification)
- return verification
- }
- async createToken(req) {
- return await db.post('/user/token', req, true)
- }
- /** Check if session still active in backend */
- async #isValidSession() {
- const hash = this.#getHashedToken()
- let validation
- try {
- validation = await db.post('/user/validate-session', hash, true)
- } catch (error) {
- console.error(error)
- }
- console.log('valid Session :>> ', validation)
- return validation
- }
- async authenticateLoginCredentials(credentials) {
- return await db.post('/user/login', credentials)
- }
- async removeSession() {
- const hash = this.#getHashedToken()
- return await db.post('/user/remove-session', hash, true)
- }
- #getHashedToken(cookieKey = 'siimee_session') {
- const cookies = document.cookie.split('; ').reduce((prev, current) => {
- const [name, ...value] = current.split('=')
- prev[name] = value.join('=')
- return prev
- }, {})
- if (!cookies[cookieKey])
- return console.warn(
- 'WARNING :=> accessToken is not defined; There was problem with session cookie you are not logged in.',
- )
- return cookies[cookieKey]
- }
- async checkSessionValid() {
- const validation = await this.#isValidSession()
- if (validation.error)
- return console.error('ERROR :=>', validation.error)
- return validation
- }
- }
- const authenticator = new Authenticator()
-
- export { authenticator, Authenticator }
|