UNPKG

1.26 kBJavaScriptView Raw
1import { Cookies } from 'react-cookie';
2export class AuthHandler {
3 constructor({ cookies }) {
4 this.authToken = null;
5 this.cookies = new Cookies();
6 this.setAuthToken = (newToken) => {
7 if (!newToken)
8 return;
9 this.authToken = newToken;
10 const expires = new Date();
11 expires.setMonth(new Date().getMonth() + 1);
12 this.cookies.set('authToken', newToken, {
13 expires,
14 path: '/',
15 sameSite: true
16 });
17 };
18 this.loadAuthToken = () => {
19 const cookieAuthToken = this.cookies.get('authToken');
20 if (cookieAuthToken) {
21 this.authToken = cookieAuthToken;
22 }
23 };
24 this.clearAuthToken = () => {
25 this.authToken = null;
26 if (this.cookies.get('authToken')) {
27 this.cookies.remove('authToken', { path: '/' });
28 }
29 };
30 this.getAuthHeaders = () => {
31 return {
32 authorization: this.authToken ? `Bearer ${this.authToken}` : ''
33 };
34 };
35 this.cookies = cookies;
36 this.loadAuthToken();
37 }
38}
39//# sourceMappingURL=AuthHandler.js.map
\No newline at end of file