-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.ts
93 lines (73 loc) · 2.33 KB
/
auth.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import {
HttpInterface,
MakeRequestParams,
} from "devise-token-auth-vue/HttpInterface";
import { FetchResponse, FetchError } from "ofetch";
import { CookieStorageInterface } from "devise-token-auth-vue/CookieStorageInterface";
import { vueDeviseAuth, useAuthCreate } from "devise-token-auth-vue";
import { DeviseAuth } from "devise-token-auth-vue/DeviseAuth";
import { AuthHeaders } from "devise-token-auth-vue/types";
const baseURL = "https://example.net";
type HttpProxyParams = [];
class HttpProxy implements HttpInterface<HttpProxyParams, FetchResponse<any>> {
async makeRequest(
p: MakeRequestParams,
...params: HttpProxyParams
): Promise<any> {
console.log("make request: ", p, params);
const resp = await $fetch.raw(p.url, {
baseURL,
method: p.method,
body: p.body,
headers: p.reqHeaders,
});
console.log("resp: ", resp);
return resp._data;
}
getResponseHeaders(resp: FetchResponse<any>) {
return Object.fromEntries(resp.headers.entries());
}
checkRequestErrorIsUnauthorized(e: any): boolean {
if (e instanceof FetchError && e.status === 401) {
return true;
}
return false;
}
}
class CookieStorage implements CookieStorageInterface {
set(data: AuthHeaders | null): void {
console.log("set cookie", data);
const cookie = useCookie<AuthHeaders | null | undefined>("auth", {
sameSite: "strict",
secure: true,
});
cookie.value = data;
}
get(): AuthHeaders | null {
console.log("get cookie");
const cookie = useCookie<AuthHeaders | null | undefined>("auth", {
sameSite: "strict",
secure: true,
});
console.log(" get cookie");
return cookie.value ?? null;
}
}
// `useAuth` composable has to be declared here to return a concrete type.
//
// Typescript doesn't know if initializer ran, and, if declared in plugin,
// `useAuth` would have to return any as a result.
//
// In nuxt, this function declaration could be moved to `composables` folder
// to enable auto import.
export const useAuth = useAuthCreate<HttpProxyParams, FetchResponse<any>>();
export default defineNuxtPlugin((nuxtApp) => {
const authInstance = new DeviseAuth({
apiUrl: "/api/v1/auth",
http: new HttpProxy(),
cookie: new CookieStorage(),
});
nuxtApp.vueApp.use(vueDeviseAuth, {
authInstance,
});
});