選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

auth.service.js 2.0KB

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