|
| 1 | +import { CookieRef } from "nuxt/dist/app/composables"; |
| 2 | +import { PublicApi } from "~~/lib/api/public"; |
| 3 | +import { UserOut } from "~~/lib/api/types/data-contracts"; |
| 4 | +import { UserClient } from "~~/lib/api/user"; |
| 5 | + |
| 6 | +export interface IAuthContext { |
| 7 | + self?: UserOut; |
| 8 | + get token(): string | null; |
| 9 | + get expiresAt(): string | null; |
| 10 | + get attachmentToken(): string | null; |
| 11 | + |
| 12 | + /** |
| 13 | + * The current user object for the session. This is undefined if the session is not authorized. |
| 14 | + */ |
| 15 | + user?: UserOut; |
| 16 | + |
| 17 | + /** |
| 18 | + * Returns true if the session is expired. |
| 19 | + */ |
| 20 | + isExpired(): boolean; |
| 21 | + |
| 22 | + /** |
| 23 | + * Returns true if the session is authorized. |
| 24 | + */ |
| 25 | + isAuthorized(): boolean; |
| 26 | + |
| 27 | + /** |
| 28 | + * Invalidates the session by removing the token and the expiresAt. |
| 29 | + */ |
| 30 | + invalidateSession(): void; |
| 31 | + |
| 32 | + /** |
| 33 | + * Logs out the user and calls the invalidateSession method. |
| 34 | + */ |
| 35 | + logout(api: UserClient): ReturnType<UserClient["user"]["logout"]>; |
| 36 | + |
| 37 | + /** |
| 38 | + * Logs in the user and sets the authorization context via cookies |
| 39 | + */ |
| 40 | + login(api: PublicApi, email: string, password: string): ReturnType<PublicApi["login"]>; |
| 41 | +} |
| 42 | + |
| 43 | +class AuthContext implements IAuthContext { |
| 44 | + user?: UserOut; |
| 45 | + private _token: CookieRef<string | null>; |
| 46 | + private _expiresAt: CookieRef<string | null>; |
| 47 | + private _attachmentToken: CookieRef<string | null>; |
| 48 | + |
| 49 | + get token() { |
| 50 | + return this._token.value; |
| 51 | + } |
| 52 | + |
| 53 | + get expiresAt() { |
| 54 | + return this._expiresAt.value; |
| 55 | + } |
| 56 | + |
| 57 | + get attachmentToken() { |
| 58 | + return this._attachmentToken.value; |
| 59 | + } |
| 60 | + |
| 61 | + constructor( |
| 62 | + token: CookieRef<string | null>, |
| 63 | + expiresAt: CookieRef<string | null>, |
| 64 | + attachmentToken: CookieRef<string | null> |
| 65 | + ) { |
| 66 | + this._token = token; |
| 67 | + this._expiresAt = expiresAt; |
| 68 | + this._attachmentToken = attachmentToken; |
| 69 | + } |
| 70 | + |
| 71 | + isExpired() { |
| 72 | + const expiresAt = this.expiresAt; |
| 73 | + if (expiresAt === null) { |
| 74 | + return true; |
| 75 | + } |
| 76 | + |
| 77 | + const expiresAtDate = new Date(expiresAt); |
| 78 | + const now = new Date(); |
| 79 | + |
| 80 | + return now.getTime() > expiresAtDate.getTime(); |
| 81 | + } |
| 82 | + |
| 83 | + isAuthorized() { |
| 84 | + return this._token.value !== null && !this.isExpired(); |
| 85 | + } |
| 86 | + |
| 87 | + invalidateSession() { |
| 88 | + this.user = undefined; |
| 89 | + this._token.value = null; |
| 90 | + this._expiresAt.value = null; |
| 91 | + this._attachmentToken.value = null; |
| 92 | + } |
| 93 | + |
| 94 | + async login(api: PublicApi, email: string, password: string) { |
| 95 | + const r = await api.login(email, password); |
| 96 | + |
| 97 | + if (!r.error) { |
| 98 | + this._token.value = r.data.token; |
| 99 | + this._expiresAt.value = r.data.expiresAt as string; |
| 100 | + this._attachmentToken.value = r.data.attachmentToken; |
| 101 | + |
| 102 | + console.log({ |
| 103 | + token: this._token.value, |
| 104 | + expiresAt: this._expiresAt.value, |
| 105 | + attachmentToken: this._attachmentToken.value, |
| 106 | + }); |
| 107 | + } |
| 108 | + |
| 109 | + return r; |
| 110 | + } |
| 111 | + |
| 112 | + async logout(api: UserClient) { |
| 113 | + const r = await api.user.logout(); |
| 114 | + |
| 115 | + if (!r.error) { |
| 116 | + this.invalidateSession(); |
| 117 | + } |
| 118 | + |
| 119 | + return r; |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +export function useAuthContext(): IAuthContext { |
| 124 | + const tokenCookie = useCookie("hb.auth.token"); |
| 125 | + const expiresAtCookie = useCookie("hb.auth.expires_at"); |
| 126 | + const attachmentTokenCookie = useCookie("hb.auth.attachment_token"); |
| 127 | + |
| 128 | + return new AuthContext(tokenCookie, expiresAtCookie, attachmentTokenCookie); |
| 129 | +} |
0 commit comments