nuxt-auther 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. package/dist/module.cjs +5 -0
  2. package/dist/module.d.mts +3 -0
  3. package/dist/module.d.ts +3 -0
  4. package/dist/module.json +8 -0
  5. package/dist/module.mjs +1007 -0
  6. package/dist/runtime/composables.d.ts +2 -0
  7. package/dist/runtime/composables.mjs +2 -0
  8. package/dist/runtime/core/auth.d.ts +53 -0
  9. package/dist/runtime/core/auth.mjs +387 -0
  10. package/dist/runtime/core/index.d.ts +3 -0
  11. package/dist/runtime/core/index.mjs +3 -0
  12. package/dist/runtime/core/middleware.d.ts +2 -0
  13. package/dist/runtime/core/middleware.mjs +41 -0
  14. package/dist/runtime/core/storage.d.ts +41 -0
  15. package/dist/runtime/core/storage.mjs +277 -0
  16. package/dist/runtime/inc/configuration-document-request-error.d.ts +3 -0
  17. package/dist/runtime/inc/configuration-document-request-error.mjs +6 -0
  18. package/dist/runtime/inc/configuration-document.d.ts +26 -0
  19. package/dist/runtime/inc/configuration-document.mjs +94 -0
  20. package/dist/runtime/inc/default-properties.d.ts +123 -0
  21. package/dist/runtime/inc/default-properties.mjs +132 -0
  22. package/dist/runtime/inc/expired-auth-session-error.d.ts +3 -0
  23. package/dist/runtime/inc/expired-auth-session-error.mjs +6 -0
  24. package/dist/runtime/inc/id-token.d.ts +15 -0
  25. package/dist/runtime/inc/id-token.mjs +81 -0
  26. package/dist/runtime/inc/index.d.ts +10 -0
  27. package/dist/runtime/inc/index.mjs +10 -0
  28. package/dist/runtime/inc/refresh-controller.d.ts +9 -0
  29. package/dist/runtime/inc/refresh-controller.mjs +29 -0
  30. package/dist/runtime/inc/refresh-token.d.ts +14 -0
  31. package/dist/runtime/inc/refresh-token.mjs +75 -0
  32. package/dist/runtime/inc/request-handler.d.ts +17 -0
  33. package/dist/runtime/inc/request-handler.mjs +93 -0
  34. package/dist/runtime/inc/token-status.d.ts +12 -0
  35. package/dist/runtime/inc/token-status.mjs +39 -0
  36. package/dist/runtime/inc/token.d.ts +14 -0
  37. package/dist/runtime/inc/token.mjs +83 -0
  38. package/dist/runtime/index.d.ts +3 -0
  39. package/dist/runtime/index.mjs +3 -0
  40. package/dist/runtime/providers/auth0.d.ts +7 -0
  41. package/dist/runtime/providers/auth0.mjs +15 -0
  42. package/dist/runtime/providers/discord.d.ts +6 -0
  43. package/dist/runtime/providers/discord.mjs +18 -0
  44. package/dist/runtime/providers/facebook.d.ts +6 -0
  45. package/dist/runtime/providers/facebook.mjs +13 -0
  46. package/dist/runtime/providers/github.d.ts +6 -0
  47. package/dist/runtime/providers/github.mjs +15 -0
  48. package/dist/runtime/providers/google.d.ts +6 -0
  49. package/dist/runtime/providers/google.mjs +13 -0
  50. package/dist/runtime/providers/index.d.ts +8 -0
  51. package/dist/runtime/providers/index.mjs +8 -0
  52. package/dist/runtime/providers/laravel-jwt.d.ts +7 -0
  53. package/dist/runtime/providers/laravel-jwt.mjs +47 -0
  54. package/dist/runtime/providers/laravel-passport.d.ts +12 -0
  55. package/dist/runtime/providers/laravel-passport.mjs +69 -0
  56. package/dist/runtime/providers/laravel-sanctum.d.ts +6 -0
  57. package/dist/runtime/providers/laravel-sanctum.mjs +52 -0
  58. package/dist/runtime/schemes/auth0.d.ts +4 -0
  59. package/dist/runtime/schemes/auth0.mjs +13 -0
  60. package/dist/runtime/schemes/base.d.ts +8 -0
  61. package/dist/runtime/schemes/base.mjs +11 -0
  62. package/dist/runtime/schemes/cookie.d.ts +23 -0
  63. package/dist/runtime/schemes/cookie.mjs +111 -0
  64. package/dist/runtime/schemes/index.d.ts +8 -0
  65. package/dist/runtime/schemes/index.mjs +8 -0
  66. package/dist/runtime/schemes/laravel-jwt.d.ts +5 -0
  67. package/dist/runtime/schemes/laravel-jwt.mjs +6 -0
  68. package/dist/runtime/schemes/local.d.ts +38 -0
  69. package/dist/runtime/schemes/local.mjs +160 -0
  70. package/dist/runtime/schemes/oauth2.d.ts +61 -0
  71. package/dist/runtime/schemes/oauth2.mjs +374 -0
  72. package/dist/runtime/schemes/openIDConnect.d.ts +24 -0
  73. package/dist/runtime/schemes/openIDConnect.mjs +190 -0
  74. package/dist/runtime/schemes/refresh.d.ts +26 -0
  75. package/dist/runtime/schemes/refresh.mjs +141 -0
  76. package/dist/runtime/token-nitro.d.ts +2 -0
  77. package/dist/runtime/token-nitro.mjs +9 -0
  78. package/dist/types/index.d.ts +42 -0
  79. package/dist/types/openIDConnectConfigurationDocument.d.ts +31 -0
  80. package/dist/types/options.d.ts +125 -0
  81. package/dist/types/provider.d.ts +21 -0
  82. package/dist/types/request.d.ts +8 -0
  83. package/dist/types/router.d.ts +7 -0
  84. package/dist/types/scheme.d.ts +108 -0
  85. package/dist/types/store.d.ts +30 -0
  86. package/dist/types/strategy.d.ts +16 -0
  87. package/dist/types/utils.d.ts +5 -0
  88. package/dist/utils/index.d.ts +28 -0
  89. package/dist/utils/index.mjs +123 -0
  90. package/dist/utils/provider.d.ts +13 -0
  91. package/dist/utils/provider.mjs +360 -0
  92. package/package.json +5 -3
@@ -0,0 +1,81 @@
1
+ import { jwtDecode } from "jwt-decode";
2
+ import { addTokenPrefix } from "../../utils";
3
+ import { TokenStatus } from "./token-status.mjs";
4
+ export class IdToken {
5
+ scheme;
6
+ $storage;
7
+ constructor(scheme, storage) {
8
+ this.scheme = scheme;
9
+ this.$storage = storage;
10
+ }
11
+ get() {
12
+ const key = this.scheme.options.idToken.prefix + this.scheme.name;
13
+ return this.$storage.getUniversal(key);
14
+ }
15
+ set(tokenValue) {
16
+ const idToken = addTokenPrefix(tokenValue, this.scheme.options.idToken.type);
17
+ this.#setToken(idToken);
18
+ this.#updateExpiration(idToken);
19
+ return idToken;
20
+ }
21
+ sync() {
22
+ const idToken = this.#syncToken();
23
+ this.#syncExpiration();
24
+ return idToken;
25
+ }
26
+ reset() {
27
+ this.#resetSSRToken();
28
+ this.#setToken(void 0);
29
+ this.#setExpiration(void 0);
30
+ }
31
+ status() {
32
+ return new TokenStatus(this.get(), this.#getExpiration(), this.scheme.options.idToken?.httpOnly);
33
+ }
34
+ #resetSSRToken() {
35
+ if (this.scheme.options.ssr && this.scheme.options.idToken?.httpOnly) {
36
+ const key = this.scheme.options.idToken.prefix + this.scheme.name;
37
+ this.scheme.$auth.request({ baseURL: "", url: "/_auth/reset", body: new URLSearchParams({ token: key }), method: "POST" });
38
+ }
39
+ }
40
+ #getExpiration() {
41
+ const key = this.scheme.options.idToken.expirationPrefix + this.scheme.name;
42
+ return this.$storage.getUniversal(key);
43
+ }
44
+ #setExpiration(expiration) {
45
+ const key = this.scheme.options.idToken.expirationPrefix + this.scheme.name;
46
+ return this.$storage.setUniversal(key, expiration);
47
+ }
48
+ #syncExpiration() {
49
+ const key = this.scheme.options.idToken.expirationPrefix + this.scheme.name;
50
+ return this.$storage.syncUniversal(key);
51
+ }
52
+ #updateExpiration(idToken) {
53
+ let idTokenExpiration;
54
+ const tokenIssuedAtMillis = Date.now();
55
+ const tokenTTLMillis = Number(this.scheme.options.idToken.maxAge) * 1e3;
56
+ const tokenExpiresAtMillis = tokenTTLMillis ? tokenIssuedAtMillis + tokenTTLMillis : 0;
57
+ try {
58
+ idTokenExpiration = jwtDecode(idToken).exp * 1e3 || tokenExpiresAtMillis;
59
+ } catch (error) {
60
+ idTokenExpiration = tokenExpiresAtMillis;
61
+ if (!(error && error.name === "InvalidTokenError")) {
62
+ throw error;
63
+ }
64
+ }
65
+ return this.#setExpiration(idTokenExpiration || false);
66
+ }
67
+ #setToken(idToken) {
68
+ const key = this.scheme.options.idToken.prefix + this.scheme.name;
69
+ return this.$storage.setUniversal(key, idToken);
70
+ }
71
+ #syncToken() {
72
+ const key = this.scheme.options.idToken.prefix + this.scheme.name;
73
+ return this.$storage.syncUniversal(key);
74
+ }
75
+ userInfo() {
76
+ const idToken = this.get();
77
+ if (typeof idToken === "string") {
78
+ return jwtDecode(idToken);
79
+ }
80
+ }
81
+ }
@@ -0,0 +1,10 @@
1
+ export * from './configuration-document-request-error';
2
+ export * from './configuration-document';
3
+ export * from './expired-auth-session-error';
4
+ export * from './default-properties';
5
+ export * from './refresh-controller';
6
+ export * from './refresh-token';
7
+ export * from './request-handler';
8
+ export * from './token-status';
9
+ export * from './token';
10
+ export * from './id-token';
@@ -0,0 +1,10 @@
1
+ export * from "./configuration-document-request-error.mjs";
2
+ export * from "./configuration-document.mjs";
3
+ export * from "./expired-auth-session-error.mjs";
4
+ export * from "./default-properties.mjs";
5
+ export * from "./refresh-controller.mjs";
6
+ export * from "./refresh-token.mjs";
7
+ export * from "./request-handler.mjs";
8
+ export * from "./token-status.mjs";
9
+ export * from "./token.mjs";
10
+ export * from "./id-token.mjs";
@@ -0,0 +1,9 @@
1
+ import type { RefreshableScheme, HTTPResponse } from '../../types';
2
+ import type { Auth } from '../core';
3
+ export declare class RefreshController {
4
+ #private;
5
+ scheme: RefreshableScheme;
6
+ $auth: Auth;
7
+ constructor(scheme: RefreshableScheme);
8
+ handleRefresh(): Promise<HTTPResponse<any> | void>;
9
+ }
@@ -0,0 +1,29 @@
1
+ export class RefreshController {
2
+ constructor(scheme) {
3
+ this.scheme = scheme;
4
+ this.$auth = scheme.$auth;
5
+ }
6
+ $auth;
7
+ #refreshPromise = null;
8
+ // Multiple requests will be queued until the first has completed token refresh.
9
+ handleRefresh() {
10
+ if (this.#refreshPromise) {
11
+ return this.#refreshPromise;
12
+ }
13
+ return this.#doRefresh();
14
+ }
15
+ // Returns a promise which is resolved when refresh is completed
16
+ // Call this function when you intercept a request with an expired token.
17
+ #doRefresh() {
18
+ this.#refreshPromise = new Promise((resolve, reject) => {
19
+ this.scheme.refreshTokens().then((response) => {
20
+ this.#refreshPromise = null;
21
+ resolve(response);
22
+ }).catch((error) => {
23
+ this.#refreshPromise = null;
24
+ reject(error);
25
+ });
26
+ });
27
+ return this.#refreshPromise;
28
+ }
29
+ }
@@ -0,0 +1,14 @@
1
+ import type { RefreshableScheme } from '../../types';
2
+ import type { Storage } from '../core';
3
+ import { TokenStatus } from './token-status';
4
+ export declare class RefreshToken {
5
+ #private;
6
+ scheme: RefreshableScheme;
7
+ $storage: Storage;
8
+ constructor(scheme: RefreshableScheme, storage: Storage);
9
+ get(): string | boolean;
10
+ set(tokenValue: string | boolean): string | boolean | void | null | undefined;
11
+ sync(): string | boolean | void | null | undefined;
12
+ reset(): void;
13
+ status(): TokenStatus;
14
+ }
@@ -0,0 +1,75 @@
1
+ import { addTokenPrefix } from "../../utils";
2
+ import { TokenStatus } from "./token-status.mjs";
3
+ import { jwtDecode } from "jwt-decode";
4
+ export class RefreshToken {
5
+ scheme;
6
+ $storage;
7
+ constructor(scheme, storage) {
8
+ this.scheme = scheme;
9
+ this.$storage = storage;
10
+ }
11
+ get() {
12
+ const key = this.scheme.options.refreshToken.prefix + this.scheme.name;
13
+ return this.$storage.getUniversal(key);
14
+ }
15
+ set(tokenValue) {
16
+ const refreshToken = addTokenPrefix(tokenValue, this.scheme.options.refreshToken.type);
17
+ this.#setToken(refreshToken);
18
+ this.#updateExpiration(refreshToken);
19
+ return refreshToken;
20
+ }
21
+ sync() {
22
+ const refreshToken = this.#syncToken();
23
+ this.#syncExpiration();
24
+ return refreshToken;
25
+ }
26
+ reset() {
27
+ this.#resetSSRToken();
28
+ this.#setToken(void 0);
29
+ this.#setExpiration(void 0);
30
+ }
31
+ status() {
32
+ return new TokenStatus(this.get(), this.#getExpiration(), this.scheme.options.refreshToken?.httpOnly);
33
+ }
34
+ #resetSSRToken() {
35
+ if (this.scheme.options.ssr && this.scheme.options.refreshToken?.httpOnly) {
36
+ const key = this.scheme.options.refreshToken.prefix + this.scheme.name;
37
+ this.scheme.$auth.request({ baseURL: "", url: "/_auth/reset", body: new URLSearchParams({ token: key }), method: "POST" });
38
+ }
39
+ }
40
+ #getExpiration() {
41
+ const key = this.scheme.options.refreshToken.expirationPrefix + this.scheme.name;
42
+ return this.$storage.getUniversal(key);
43
+ }
44
+ #setExpiration(expiration) {
45
+ const key = this.scheme.options.refreshToken.expirationPrefix + this.scheme.name;
46
+ return this.$storage.setUniversal(key, expiration);
47
+ }
48
+ #syncExpiration() {
49
+ const key = this.scheme.options.refreshToken.expirationPrefix + this.scheme.name;
50
+ return this.$storage.syncUniversal(key);
51
+ }
52
+ #updateExpiration(refreshToken) {
53
+ let refreshTokenExpiration;
54
+ const tokenIssuedAtMillis = Date.now();
55
+ const tokenTTLMillis = Number(this.scheme.options.refreshToken.maxAge) * 1e3;
56
+ const tokenExpiresAtMillis = tokenTTLMillis ? tokenIssuedAtMillis + tokenTTLMillis : 0;
57
+ try {
58
+ refreshTokenExpiration = jwtDecode(refreshToken).exp * 1e3 || tokenExpiresAtMillis;
59
+ } catch (error) {
60
+ refreshTokenExpiration = tokenExpiresAtMillis;
61
+ if (!(error && error.name === "InvalidTokenError")) {
62
+ throw error;
63
+ }
64
+ }
65
+ return this.#setExpiration(refreshTokenExpiration || false);
66
+ }
67
+ #setToken(refreshToken) {
68
+ const key = this.scheme.options.refreshToken.prefix + this.scheme.name;
69
+ return this.$storage.setUniversal(key, refreshToken);
70
+ }
71
+ #syncToken() {
72
+ const key = this.scheme.options.refreshToken.prefix + this.scheme.name;
73
+ return this.$storage.syncUniversal(key);
74
+ }
75
+ }
@@ -0,0 +1,17 @@
1
+ import type { TokenableScheme, RefreshableScheme } from '../../types';
2
+ import type { Auth } from '..';
3
+ import { FetchInstance } from '@refactorjs/ofetch';
4
+ export declare class RequestHandler {
5
+ #private;
6
+ scheme: TokenableScheme | RefreshableScheme;
7
+ auth: Auth;
8
+ http: FetchInstance;
9
+ requestInterceptor: number | null;
10
+ responseErrorInterceptor: number | null;
11
+ currentToken: string;
12
+ constructor(scheme: TokenableScheme | RefreshableScheme, http: FetchInstance, auth: Auth);
13
+ setHeader(token: string): void;
14
+ clearHeader(): void;
15
+ initializeRequestInterceptor(refreshEndpoint?: string | Request): void;
16
+ reset(): void;
17
+ }
@@ -0,0 +1,93 @@
1
+ import { ExpiredAuthSessionError } from "./expired-auth-session-error.mjs";
2
+ export class RequestHandler {
3
+ scheme;
4
+ auth;
5
+ http;
6
+ requestInterceptor;
7
+ responseErrorInterceptor;
8
+ currentToken;
9
+ constructor(scheme, http, auth) {
10
+ this.scheme = scheme;
11
+ this.http = http;
12
+ this.auth = auth;
13
+ this.requestInterceptor = null;
14
+ this.responseErrorInterceptor = null;
15
+ this.currentToken = this.auth.$storage?.memory?.[this.scheme.options.token?.prefix + this.scheme.options.name];
16
+ }
17
+ setHeader(token) {
18
+ if (this.scheme.options.token && this.scheme.options.token.global) {
19
+ this.http.setHeader(this.scheme.options.token.name, token);
20
+ }
21
+ }
22
+ clearHeader() {
23
+ if (this.scheme.options.token && this.scheme.options.token.global) {
24
+ this.http.setHeader(this.scheme.options.token.name, null);
25
+ }
26
+ }
27
+ initializeRequestInterceptor(refreshEndpoint) {
28
+ this.requestInterceptor = this.http.onRequest(
29
+ async (config) => {
30
+ if (this.scheme.options.token && this.currentToken) {
31
+ this.setHeader(this.currentToken);
32
+ }
33
+ if (this.scheme.options.token && !this.#needToken(config) || config.url === refreshEndpoint) {
34
+ return config;
35
+ }
36
+ const { valid, tokenExpired, refreshTokenExpired, isRefreshable } = this.scheme.check(true);
37
+ let isValid = valid;
38
+ if (refreshTokenExpired) {
39
+ this.scheme.reset?.();
40
+ throw new ExpiredAuthSessionError();
41
+ }
42
+ if (tokenExpired) {
43
+ if (!isRefreshable) {
44
+ this.scheme.reset?.();
45
+ throw new ExpiredAuthSessionError();
46
+ }
47
+ isValid = await this.scheme.refreshController.handleRefresh().then(() => true).catch(() => {
48
+ this.scheme.reset?.();
49
+ throw new ExpiredAuthSessionError();
50
+ });
51
+ }
52
+ const token = this.scheme.token;
53
+ if (!isValid) {
54
+ if (token && !token.get() && this.#requestHasAuthorizationHeader(config)) {
55
+ throw new ExpiredAuthSessionError();
56
+ }
57
+ return config;
58
+ }
59
+ return this.#getUpdatedRequestConfig(config, token ? token.get() : false);
60
+ }
61
+ );
62
+ this.responseErrorInterceptor = this.http.onResponseError((error) => {
63
+ if (typeof this.auth.options.resetOnResponseError === "function") {
64
+ this.auth.options.resetOnResponseError(error, this.auth, this.scheme);
65
+ } else if (this.auth.options.resetOnResponseError && error?.response?.status === 401) {
66
+ this.scheme.reset?.();
67
+ throw new ExpiredAuthSessionError();
68
+ }
69
+ });
70
+ }
71
+ reset() {
72
+ this.http.interceptors.request.eject(this.requestInterceptor);
73
+ this.http.interceptors.response.eject(this.responseErrorInterceptor);
74
+ this.requestInterceptor = null;
75
+ this.responseErrorInterceptor = null;
76
+ }
77
+ #needToken(config) {
78
+ const options = this.scheme.options;
79
+ return options.token.global || Object.values(options.endpoints).some((endpoint) => typeof endpoint === "object" ? endpoint.url === config.url : endpoint === config.url);
80
+ }
81
+ // ---------------------------------------------------------------
82
+ // Watch requests for token expiration
83
+ // Refresh tokens if token has expired
84
+ #getUpdatedRequestConfig(config, token) {
85
+ if (typeof token === "string") {
86
+ config.headers[this.scheme.options.token.name] = token;
87
+ }
88
+ return config;
89
+ }
90
+ #requestHasAuthorizationHeader(config) {
91
+ return !!config.headers[this.scheme.options.token.name];
92
+ }
93
+ }
@@ -0,0 +1,12 @@
1
+ export declare enum TokenStatusEnum {
2
+ UNKNOWN = "UNKNOWN",
3
+ VALID = "VALID",
4
+ EXPIRED = "EXPIRED"
5
+ }
6
+ export declare class TokenStatus {
7
+ #private;
8
+ constructor(token: string | boolean, tokenExpiresAt: number | false, httpOnly?: boolean);
9
+ unknown(): boolean;
10
+ valid(): boolean;
11
+ expired(): boolean;
12
+ }
@@ -0,0 +1,39 @@
1
+ export var TokenStatusEnum = /* @__PURE__ */ ((TokenStatusEnum2) => {
2
+ TokenStatusEnum2["UNKNOWN"] = "UNKNOWN";
3
+ TokenStatusEnum2["VALID"] = "VALID";
4
+ TokenStatusEnum2["EXPIRED"] = "EXPIRED";
5
+ return TokenStatusEnum2;
6
+ })(TokenStatusEnum || {});
7
+ export class TokenStatus {
8
+ #status;
9
+ #isHttpOnly;
10
+ constructor(token, tokenExpiresAt, httpOnly = false) {
11
+ this.#status = this.#calculate(token, tokenExpiresAt);
12
+ this.#isHttpOnly = httpOnly;
13
+ }
14
+ unknown() {
15
+ return "UNKNOWN" /* UNKNOWN */ === this.#status;
16
+ }
17
+ valid() {
18
+ return "VALID" /* VALID */ === this.#status;
19
+ }
20
+ expired() {
21
+ return "EXPIRED" /* EXPIRED */ === this.#status;
22
+ }
23
+ #calculate(token, tokenExpiresAt) {
24
+ const now = Date.now();
25
+ try {
26
+ if (this.#isHttpOnly && !tokenExpiresAt || (!this.#isHttpOnly && !token || !tokenExpiresAt)) {
27
+ return "UNKNOWN" /* UNKNOWN */;
28
+ }
29
+ } catch (error) {
30
+ return "UNKNOWN" /* UNKNOWN */;
31
+ }
32
+ const timeSlackMillis = 6e4;
33
+ tokenExpiresAt -= timeSlackMillis;
34
+ if (now < tokenExpiresAt) {
35
+ return "VALID" /* VALID */;
36
+ }
37
+ return "EXPIRED" /* EXPIRED */;
38
+ }
39
+ }
@@ -0,0 +1,14 @@
1
+ import type { TokenableScheme } from '../../types';
2
+ import type { Storage } from '../core';
3
+ import { TokenStatus } from './token-status';
4
+ export declare class Token {
5
+ #private;
6
+ scheme: TokenableScheme;
7
+ $storage: Storage;
8
+ constructor(scheme: TokenableScheme, storage: Storage);
9
+ get(): string | boolean;
10
+ set(tokenValue: string | boolean, expiresIn?: number | boolean): string | boolean | void | null | undefined;
11
+ sync(): string | boolean | void | null | undefined;
12
+ reset(): void;
13
+ status(): TokenStatus;
14
+ }
@@ -0,0 +1,83 @@
1
+ import { addTokenPrefix } from "../../utils";
2
+ import { TokenStatus } from "./token-status.mjs";
3
+ import { jwtDecode } from "jwt-decode";
4
+ export class Token {
5
+ scheme;
6
+ $storage;
7
+ constructor(scheme, storage) {
8
+ this.scheme = scheme;
9
+ this.$storage = storage;
10
+ }
11
+ get() {
12
+ const key = this.scheme.options.token.prefix + this.scheme.name;
13
+ return this.$storage.getUniversal(key);
14
+ }
15
+ set(tokenValue, expiresIn = false) {
16
+ const token = addTokenPrefix(tokenValue, this.scheme.options.token.type);
17
+ this.#setToken(token);
18
+ this.#updateExpiration(token, expiresIn);
19
+ if (typeof token === "string") {
20
+ this.scheme.requestHandler.setHeader(token);
21
+ }
22
+ return token;
23
+ }
24
+ sync() {
25
+ const token = this.#syncToken();
26
+ this.#syncExpiration();
27
+ if (typeof token === "string") {
28
+ this.scheme.requestHandler.setHeader(token);
29
+ }
30
+ return token;
31
+ }
32
+ reset() {
33
+ this.scheme.requestHandler.clearHeader();
34
+ this.#resetSSRToken();
35
+ this.#setToken(void 0);
36
+ this.#setExpiration(void 0);
37
+ }
38
+ status() {
39
+ return new TokenStatus(this.get(), this.#getExpiration(), this.scheme.options.token?.httpOnly);
40
+ }
41
+ #resetSSRToken() {
42
+ if (this.scheme.options.ssr && this.scheme.options.token?.httpOnly) {
43
+ const key = this.scheme.options.token.prefix + this.scheme.name;
44
+ this.scheme.$auth.request({ baseURL: "", url: "/_auth/reset", body: new URLSearchParams({ token: key }), method: "POST" });
45
+ }
46
+ }
47
+ #getExpiration() {
48
+ const key = this.scheme.options.token.expirationPrefix + this.scheme.name;
49
+ return this.$storage.getUniversal(key);
50
+ }
51
+ #setExpiration(expiration) {
52
+ const key = this.scheme.options.token.expirationPrefix + this.scheme.name;
53
+ return this.$storage.setUniversal(key, expiration);
54
+ }
55
+ #syncExpiration() {
56
+ const key = this.scheme.options.token.expirationPrefix + this.scheme.name;
57
+ return this.$storage.syncUniversal(key);
58
+ }
59
+ #updateExpiration(token, expiresIn) {
60
+ let tokenExpiration;
61
+ const tokenIssuedAtMillis = Date.now();
62
+ const maxAge = expiresIn ? expiresIn : this.scheme.options.token.maxAge;
63
+ const tokenTTLMillis = Number(maxAge) * 1e3;
64
+ const tokenExpiresAtMillis = tokenTTLMillis ? tokenIssuedAtMillis + tokenTTLMillis : 0;
65
+ try {
66
+ tokenExpiration = jwtDecode(token).exp * 1e3 || tokenExpiresAtMillis;
67
+ } catch (error) {
68
+ tokenExpiration = tokenExpiresAtMillis;
69
+ if (!(error && error.name === "InvalidTokenError")) {
70
+ throw error;
71
+ }
72
+ }
73
+ return this.#setExpiration(tokenExpiration || false);
74
+ }
75
+ #setToken(token) {
76
+ const key = this.scheme.options.token.prefix + this.scheme.name;
77
+ return this.$storage.setUniversal(key, token);
78
+ }
79
+ #syncToken() {
80
+ const key = this.scheme.options.token.prefix + this.scheme.name;
81
+ return this.$storage.syncUniversal(key);
82
+ }
83
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./core";
2
+ export * from "./inc";
3
+ export * from "./schemes";
@@ -0,0 +1,3 @@
1
+ export * from "./core/index.mjs";
2
+ export * from "./inc/index.mjs";
3
+ export * from "./schemes/index.mjs";
@@ -0,0 +1,7 @@
1
+ import type { ProviderOptions, ProviderPartialOptions } from '../../types';
2
+ import type { Oauth2SchemeOptions } from '..';
3
+ import type { Nuxt } from '@nuxt/schema';
4
+ export interface Auth0ProviderOptions extends ProviderOptions, Oauth2SchemeOptions {
5
+ domain: string;
6
+ }
7
+ export declare function auth0(nuxt: Nuxt, strategy: ProviderPartialOptions<Auth0ProviderOptions>): void;
@@ -0,0 +1,15 @@
1
+ import { assignDefaults } from "../../utils/provider";
2
+ import { OAUTH2DEFAULTS } from "../inc/index.mjs";
3
+ export function auth0(nuxt, strategy) {
4
+ const DEFAULTS = Object.assign(OAUTH2DEFAULTS, {
5
+ scheme: "auth0",
6
+ endpoints: {
7
+ authorization: `https://${strategy.domain}/authorize`,
8
+ userInfo: `https://${strategy.domain}/userinfo`,
9
+ token: `https://${strategy.domain}/oauth/token`,
10
+ logout: `https://${strategy.domain}/v2/logout`
11
+ },
12
+ scope: ["openid", "profile", "email"]
13
+ });
14
+ assignDefaults(strategy, DEFAULTS);
15
+ }
@@ -0,0 +1,6 @@
1
+ import type { ProviderOptions, ProviderPartialOptions } from '../../types';
2
+ import type { Oauth2SchemeOptions } from '..';
3
+ import type { Nuxt } from '@nuxt/schema';
4
+ export interface DiscordProviderOptions extends ProviderOptions, Oauth2SchemeOptions {
5
+ }
6
+ export declare function discord(nuxt: Nuxt, strategy: ProviderPartialOptions<DiscordProviderOptions>): void;
@@ -0,0 +1,18 @@
1
+ import { assignDefaults, addAuthorize } from "../../utils/provider";
2
+ import { OAUTH2DEFAULTS } from "../inc/index.mjs";
3
+ export function discord(nuxt, strategy) {
4
+ const DEFAULTS = Object.assign(OAUTH2DEFAULTS, {
5
+ scheme: "oauth2",
6
+ endpoints: {
7
+ authorization: "https://discord.com/api/oauth2/authorize",
8
+ token: "https://discord.com/api/oauth2/token",
9
+ userInfo: "https://discord.com/api/users/@me"
10
+ // logout: 'https://discord.com/api/oauth2/token/revoke' //TODO: add post method, because discord using the post method to logout
11
+ },
12
+ grantType: "authorization_code",
13
+ codeChallengeMethod: "S256",
14
+ scope: ["identify", "email"]
15
+ });
16
+ assignDefaults(strategy, DEFAULTS);
17
+ addAuthorize(nuxt, strategy, true);
18
+ }
@@ -0,0 +1,6 @@
1
+ import type { ProviderPartialOptions, ProviderOptions } from '../../types';
2
+ import type { Oauth2SchemeOptions } from '..';
3
+ import type { Nuxt } from '@nuxt/schema';
4
+ export interface FacebookProviderOptions extends ProviderOptions, Oauth2SchemeOptions {
5
+ }
6
+ export declare function facebook(nuxt: Nuxt, strategy: ProviderPartialOptions<FacebookProviderOptions>): void;
@@ -0,0 +1,13 @@
1
+ import { assignDefaults } from "../../utils/provider";
2
+ import { OAUTH2DEFAULTS } from "../inc/index.mjs";
3
+ export function facebook(nuxt, strategy) {
4
+ const DEFAULTS = Object.assign(OAUTH2DEFAULTS, {
5
+ scheme: "oauth2",
6
+ endpoints: {
7
+ authorization: "https://facebook.com/v2.12/dialog/oauth",
8
+ userInfo: "https://graph.facebook.com/v2.12/me?fields=about,name,picture{url},email"
9
+ },
10
+ scope: ["public_profile", "email"]
11
+ });
12
+ assignDefaults(strategy, DEFAULTS);
13
+ }
@@ -0,0 +1,6 @@
1
+ import type { ProviderOptions, ProviderPartialOptions } from '../../types';
2
+ import type { Oauth2SchemeOptions } from '..';
3
+ import type { Nuxt } from '@nuxt/schema';
4
+ export interface GithubProviderOptions extends ProviderOptions, Oauth2SchemeOptions {
5
+ }
6
+ export declare function github(nuxt: Nuxt, strategy: ProviderPartialOptions<GithubProviderOptions>): void;
@@ -0,0 +1,15 @@
1
+ import { assignDefaults, addAuthorize } from "../../utils/provider";
2
+ import { OAUTH2DEFAULTS } from "../inc/index.mjs";
3
+ export function github(nuxt, strategy) {
4
+ const DEFAULTS = Object.assign(OAUTH2DEFAULTS, {
5
+ scheme: "oauth2",
6
+ endpoints: {
7
+ authorization: "https://github.com/login/oauth/authorize",
8
+ token: "https://github.com/login/oauth/access_token",
9
+ userInfo: "https://api.github.com/user"
10
+ },
11
+ scope: ["user", "email"]
12
+ });
13
+ assignDefaults(strategy, DEFAULTS);
14
+ addAuthorize(nuxt, strategy);
15
+ }
@@ -0,0 +1,6 @@
1
+ import type { ProviderPartialOptions, ProviderOptions } from '../../types';
2
+ import type { Oauth2SchemeOptions } from '..';
3
+ import type { Nuxt } from '@nuxt/schema';
4
+ export interface GoogleProviderOptions extends ProviderOptions, Oauth2SchemeOptions {
5
+ }
6
+ export declare function google(nuxt: Nuxt, strategy: ProviderPartialOptions<GoogleProviderOptions>): void;
@@ -0,0 +1,13 @@
1
+ import { assignDefaults } from "../../utils/provider";
2
+ import { OAUTH2DEFAULTS } from "../inc/index.mjs";
3
+ export function google(nuxt, strategy) {
4
+ const DEFAULTS = Object.assign(OAUTH2DEFAULTS, {
5
+ scheme: "oauth2",
6
+ endpoints: {
7
+ authorization: "https://accounts.google.com/o/oauth2/v2/auth",
8
+ userInfo: "https://www.googleapis.com/oauth2/v3/userinfo"
9
+ },
10
+ scope: ["openid", "profile", "email"]
11
+ });
12
+ assignDefaults(strategy, DEFAULTS);
13
+ }
@@ -0,0 +1,8 @@
1
+ export * from './auth0';
2
+ export * from './discord';
3
+ export * from './facebook';
4
+ export * from './github';
5
+ export * from './google';
6
+ export * from './laravel-jwt';
7
+ export * from './laravel-passport';
8
+ export * from './laravel-sanctum';
@@ -0,0 +1,8 @@
1
+ export * from "./auth0.mjs";
2
+ export * from "./discord.mjs";
3
+ export * from "./facebook.mjs";
4
+ export * from "./github.mjs";
5
+ export * from "./google.mjs";
6
+ export * from "./laravel-jwt.mjs";
7
+ export * from "./laravel-passport.mjs";
8
+ export * from "./laravel-sanctum.mjs";
@@ -0,0 +1,7 @@
1
+ import type { ProviderPartialOptions, ProviderOptions } from '../../types';
2
+ import type { RefreshSchemeOptions } from '..';
3
+ import type { Nuxt } from '@nuxt/schema';
4
+ export interface LaravelJWTProviderOptions extends ProviderOptions, RefreshSchemeOptions {
5
+ url: string;
6
+ }
7
+ export declare function laravelJWT(nuxt: Nuxt, strategy: ProviderPartialOptions<LaravelJWTProviderOptions>): void;