Skip to content

Commit d3318fa

Browse files
authored
v3.1.7 (#107)
* v3.1.7 * Update package.json * v3.1.7
1 parent ea5d9b8 commit d3318fa

19 files changed

+132
-90
lines changed

package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nuxt-alt/auth",
3-
"version": "3.1.4",
3+
"version": "3.1.7",
44
"description": "An alternative module to @nuxtjs/auth",
55
"homepage": "https://github.com/nuxt-alt/auth",
66
"author": "Denoder",
@@ -37,24 +37,24 @@
3737
},
3838
"dependencies": {
3939
"@nuxt-alt/http": "latest",
40-
"@nuxt/kit": "^3.9.1",
40+
"@nuxt/kit": "^3.12.2",
4141
"@refactorjs/serialize": "latest",
42-
"cookie-es": "^1.0.0",
42+
"cookie-es": "^1.1.0",
4343
"defu": "^6.1.3",
4444
"jwt-decode": "^4.0.0",
4545
"ohash": "^1.1.3",
46-
"pathe": "^1.1.1",
46+
"pathe": "^1.1.2",
4747
"pinia": "^2.1.7",
4848
"requrl": "^3.0.2"
4949
},
5050
"devDependencies": {
51-
"@nuxt-alt/proxy": "^2.4.8",
52-
"@nuxt/schema": "^3.9.1",
51+
"@nuxt-alt/proxy": "^2.5.8",
52+
"@nuxt/schema": "^3.12.2",
5353
"@nuxtjs/i18n": "next",
5454
"@types/node": "^20",
55-
"jiti": "^1.21.0",
56-
"nuxt": "^3.9.1",
57-
"typescript": "^5.3.3",
55+
"jiti": "^1.21.6",
56+
"nuxt": "^3.9.3",
57+
"typescript": "5.3.3",
5858
"unbuild": "^2.0.0"
5959
},
6060
"repository": {

src/plugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import type { ModuleOptions, Strategy, ImportOptions } from './types';
1+
import type { ModuleOptions, StrategyOptions, ImportOptions } from './types';
22
import { serialize } from '@refactorjs/serialize';
33

44
export const getAuthPlugin = (options: {
55
options: ModuleOptions
66
schemeImports: ImportOptions[]
7-
strategies: Strategy[]
7+
strategies: StrategyOptions[]
88
strategyScheme: Record<string, ImportOptions>
99
}): string => {
1010
return `import { Auth, ExpiredAuthSessionError } from '#auth/runtime'

src/resolve.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
import type { Strategy, ModuleOptions, ProviderNames, SchemeNames, ImportOptions } from './types';
1+
import type { StrategyOptions, ModuleOptions, SchemeNames, ImportOptions } from './types';
22
import type { Nuxt } from '@nuxt/schema';
3-
import { OAUTH2DEFAULTS, LOCALDEFAULTS, ProviderAliases, BuiltinSchemes } from './runtime/inc/default-properties';
4-
import { addAuthorize, addLocalAuthorize, assignAbsoluteEndpoints, assignDefaults } from './utils/provider';
3+
import { OAUTH2DEFAULTS, LOCALDEFAULTS, ProviderAliases, BuiltinSchemes, LocalSchemes, OAuth2Schemes } from './runtime/inc/default-properties';
4+
import { addAuthorize, addLocalAuthorize, assignAbsoluteEndpoints, assignDefaults, } from './utils/provider';
5+
import { hasOwn } from './utils';
56
import * as AUTH_PROVIDERS from './runtime/providers';
67
import { resolvePath } from '@nuxt/kit';
78
import { existsSync } from 'fs';
89
import { hash } from 'ohash';
910

1011
export async function resolveStrategies(nuxt: Nuxt, options: ModuleOptions) {
11-
const strategies: Strategy[] = [];
12+
const strategies: StrategyOptions[] = [];
1213
const strategyScheme = {} as Record<string, ImportOptions>;
1314

1415
for (const name of Object.keys(options.strategies!)) {
15-
if (!options.strategies![name] || (options.strategies as Strategy)[name].enabled === false) {
16+
if (!options.strategies?.[name] || options.strategies?.[name].enabled === false) {
1617
continue;
1718
}
1819

1920
// Clone strategy
20-
const strategy = Object.assign({}, options.strategies![name]) as Strategy;
21+
const strategy = Object.assign({}, options.strategies![name]);
2122

2223
// Default name
2324
if (!strategy.name) {
@@ -26,14 +27,18 @@ export async function resolveStrategies(nuxt: Nuxt, options: ModuleOptions) {
2627

2728
// Default provider (same as name)
2829
if (!strategy.provider) {
29-
strategy.provider = strategy.name as ProviderNames;
30+
strategy.provider = strategy.name;
3031
}
3132

3233
// Determine if SSR is enabled
33-
strategy.ssr = nuxt.options.ssr
34+
if (hasOwn(strategy, 'ssr')) {
35+
strategy.ssr = strategy.ssr;
36+
} else {
37+
strategy.ssr = nuxt.options.ssr;
38+
}
3439

3540
// Try to resolve provider
36-
const provider = await resolveProvider(strategy.provider, nuxt, strategy);
41+
const provider = await resolveProvider(strategy.provider as string, nuxt, strategy);
3742

3843
delete strategy.provider;
3944

@@ -50,7 +55,7 @@ export async function resolveStrategies(nuxt: Nuxt, options: ModuleOptions) {
5055
// Resolve and keep scheme needed for strategy
5156
const schemeImport = await resolveScheme(strategy.scheme);
5257
delete strategy.scheme;
53-
strategyScheme[strategy.name] = schemeImport as ImportOptions;
58+
strategyScheme[strategy.name] = schemeImport!;
5459

5560
// Add strategy to array
5661
strategies.push(strategy);
@@ -90,7 +95,7 @@ export async function resolveScheme(scheme: string) {
9095
}
9196
}
9297

93-
export async function resolveProvider(provider: string | ((...args: any[]) => any), nuxt: Nuxt, strategy: Strategy) {
98+
export async function resolveProvider(provider: string | ((nuxt: Nuxt, strategy: StrategyOptions, ...args: any[]) => void), nuxt: Nuxt, strategy: StrategyOptions) {
9499
provider = (ProviderAliases[provider as keyof typeof ProviderAliases] || provider);
95100

96101
if (AUTH_PROVIDERS[provider as keyof typeof AUTH_PROVIDERS]) {
@@ -104,20 +109,20 @@ export async function resolveProvider(provider: string | ((...args: any[]) => an
104109

105110
// return an empty function as it doesn't use a provider
106111
if (typeof provider === 'string') {
107-
return (nuxt: Nuxt, strategy: Strategy) => {
108-
if (['oauth2', 'openIDConnect', 'auth0'].includes(strategy.scheme!) && strategy.ssr) {
109-
assignDefaults(strategy as any, OAUTH2DEFAULTS)
110-
addAuthorize(nuxt, strategy as any, true)
112+
return (nuxt: Nuxt, strategy: StrategyOptions) => {
113+
if (OAuth2Schemes.includes(strategy.scheme!) && strategy.ssr) {
114+
assignDefaults(strategy, OAUTH2DEFAULTS as typeof strategy)
115+
addAuthorize(nuxt, strategy, true)
111116
}
112117

113-
if (['refresh', 'local', 'cookie'].includes(strategy.scheme!) && strategy.ssr) {
114-
assignDefaults(strategy as any, LOCALDEFAULTS)
118+
if (LocalSchemes.includes(strategy.scheme!) && strategy.ssr) {
119+
assignDefaults(strategy, LOCALDEFAULTS as typeof strategy)
115120

116121
if (strategy.url) {
117-
assignAbsoluteEndpoints(strategy as any);
122+
assignAbsoluteEndpoints(strategy);
118123
}
119124

120-
addLocalAuthorize(nuxt, strategy as any)
125+
addLocalAuthorize(nuxt, strategy)
121126
}
122127
}
123128
}

src/runtime/core/storage.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
import type { ModuleOptions, AuthStore, AuthState, StoreMethod, StoreIncludeOptions } from '../../types';
22
import type { NuxtApp } from '#app';
3-
import type { Pinia, StoreDefinition } from 'pinia';
3+
import { type Pinia, type StoreDefinition, defineStore } from 'pinia';
44
import { isUnset, isSet, decodeValue, encodeValue, setH3Cookie } from '../../utils';
55
import { parse, serialize, type CookieSerializeOptions } from 'cookie-es';
6-
import { useState } from '#imports';
76
import { watch, type Ref } from 'vue';
7+
import { useState } from '#imports';
88

9+
/**
10+
* @class Storage
11+
* @classdesc Storage class for stores and cookies
12+
* @param { NuxtApp } ctx - Nuxt app context
13+
* @param { ModuleOptions } options - Module options
14+
*/
915
export class Storage {
1016
ctx: NuxtApp;
1117
options: ModuleOptions;
1218
#PiniaStore!: StoreDefinition;
1319
#initPiniaStore!: AuthStore;
1420
#initStore!: Ref<AuthState>;
1521
state: AuthState;
16-
memory!: Ref<AuthState>;
22+
#internal!: Ref<AuthState>;
23+
memory!: AuthState;
1724
#piniaEnabled: boolean = false;
1825

1926
constructor(ctx: NuxtApp, options: ModuleOptions) {
@@ -106,22 +113,22 @@ export class Storage {
106113
this.#piniaEnabled = this.options.stores.pinia!?.enabled! && !!pinia;
107114

108115
if (this.#piniaEnabled) {
109-
const { defineStore } = await import('pinia')
110-
this.#PiniaStore = defineStore(this.options.stores.pinia?.namespace!, {
116+
this.#PiniaStore = defineStore(this.options.stores.pinia?.namespace as string, {
111117
state: (): AuthState => ({ ...this.options.initialState })
112118
});
113119

114120
this.#initPiniaStore = this.#PiniaStore(pinia)
115121
this.state = this.#initPiniaStore;
116122
} else {
117-
this.#initStore = useState<AuthState>(this.options.stores.state?.namespace, () => ({
123+
this.#initStore = useState<AuthState>(this.options.stores.state?.namespace as string, () => ({
118124
...this.options.initialState
119125
}))
120126

121127
this.state = this.#initStore.value
122128
}
123129

124-
this.memory = useState<AuthState>('auth-internal', () => ({}))
130+
this.#internal = useState<AuthState>('auth-internal', () => ({}))
131+
this.memory = this.#internal.value
125132
}
126133

127134
get pinia() {
@@ -134,7 +141,7 @@ export class Storage {
134141

135142
setState(key: string, value: any) {
136143
if (key.startsWith('_')) {
137-
this.memory.value[key] = value;
144+
this.memory[key] = value;
138145
}
139146
else if (this.#piniaEnabled) {
140147
this.#initPiniaStore.$patch({ [key]: value });
@@ -150,7 +157,7 @@ export class Storage {
150157
if (!key.startsWith('_')) {
151158
return this.state[key];
152159
} else {
153-
return this.memory.value[key];
160+
return this.memory[key];
154161
}
155162
}
156163

@@ -214,7 +221,7 @@ export class Storage {
214221
isLocalStorageEnabled(): boolean {
215222
const isNotServer = !process.server;
216223
const isConfigEnabled = this.options.stores.local?.enabled;
217-
const localTest = "test";
224+
const localTest = 'test';
218225

219226
if (isNotServer && isConfigEnabled) {
220227
try {
@@ -278,7 +285,7 @@ export class Storage {
278285
const isNotServer = !process.server;
279286
// @ts-ignore
280287
const isConfigEnabled = this.options.stores!.session?.enabled;
281-
const testKey = "test";
288+
const testKey = 'test';
282289

283290
if (isNotServer && isConfigEnabled) {
284291
try {

src/runtime/inc/default-properties.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,22 @@ export const ProviderAliases = {
116116
export const BuiltinSchemes = {
117117
local: 'LocalScheme',
118118
cookie: 'CookieScheme',
119-
oauth2: 'Oauth2Scheme',
120-
openIDConnect: 'OpenIDConnectScheme',
121119
refresh: 'RefreshScheme',
122120
laravelJWT: 'LaravelJWTScheme',
121+
oauth2: 'Oauth2Scheme',
122+
openIDConnect: 'OpenIDConnectScheme',
123123
auth0: 'Auth0Scheme',
124-
};
124+
};
125+
126+
export const LocalSchemes = [
127+
'local',
128+
'cookie',
129+
'refresh',
130+
'laravelJWT',
131+
]
132+
133+
export const OAuth2Schemes = [
134+
'oauth',
135+
'openIDConnect',
136+
'auth0',
137+
]

src/runtime/inc/id-token.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,13 @@ export class IdToken {
3636
}
3737

3838
reset() {
39-
this.scheme.requestHandler!.clearHeader();
4039
this.#resetSSRToken();
4140
this.#setToken(undefined);
4241
this.#setExpiration(undefined);
4342
}
4443

4544
status(): TokenStatus {
46-
return new TokenStatus(this.get(), this.#getExpiration());
45+
return new TokenStatus(this.get(), this.#getExpiration(), this.scheme.options.idToken?.httpOnly);
4746
}
4847

4948
#resetSSRToken(): void {

src/runtime/inc/refresh-token.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ export class RefreshToken {
3636
}
3737

3838
reset(): void {
39-
this.#resetSSRToken()
39+
this.#resetSSRToken();
4040
this.#setToken(undefined);
4141
this.#setExpiration(undefined);
4242
}
4343

4444
status(): TokenStatus {
45-
return new TokenStatus(this.get(), this.#getExpiration(), this.scheme.options.refreshToken.httpOnly);
45+
return new TokenStatus(this.get(), this.#getExpiration(), this.scheme.options.refreshToken?.httpOnly);
4646
}
4747

4848
#resetSSRToken(): void {

src/runtime/inc/request-handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class RequestHandler {
1717
this.auth = auth;
1818
this.requestInterceptor = null;
1919
this.responseErrorInterceptor = null;
20-
this.currentToken = this.auth.$storage.memory.value?.[this.scheme.options.token!.prefix + this.scheme.options.name] as string
20+
this.currentToken = this.auth.$storage?.memory?.[this.scheme.options.token!?.prefix + this.scheme.options.name] as string
2121
}
2222

2323
setHeader(token: string): void {

src/runtime/schemes/auth0.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { withQuery } from 'ufo';
22
import { Oauth2Scheme } from '../schemes/oauth2';
33

44
export class Auth0Scheme extends Oauth2Scheme {
5-
logout(): void {
5+
override logout(): void {
66
this.$auth.reset();
77

88
const opts = {

src/runtime/schemes/cookie.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class CookieScheme<OptionsT extends CookieSchemeOptions> extends LocalSch
4545
super($auth, options as OptionsT, DEFAULTS as OptionsT);
4646
}
4747

48-
async mounted(): Promise<HTTPResponse<any> | void> {
48+
override async mounted(): Promise<HTTPResponse<any> | void> {
4949
if (process.server) {
5050
this.$auth.ctx.$http.setHeader('referer', this.$auth.ctx.ssrContext!.event.node.req.headers.host!);
5151
}
@@ -59,7 +59,7 @@ export class CookieScheme<OptionsT extends CookieSchemeOptions> extends LocalSch
5959
return this.$auth.fetchUserOnce();
6060
}
6161

62-
check(): SchemeCheck {
62+
override check(): SchemeCheck {
6363
const response = { valid: false };
6464

6565
if (!super.check().valid && this.options.token?.type) {
@@ -82,7 +82,7 @@ export class CookieScheme<OptionsT extends CookieSchemeOptions> extends LocalSch
8282
return response;
8383
}
8484

85-
async login(endpoint: HTTPRequest): Promise<HTTPResponse<any> | void> {
85+
override async login(endpoint: HTTPRequest): Promise<HTTPResponse<any> | void> {
8686
// Ditch any leftover local tokens before attempting to log in
8787
this.$auth.reset();
8888

@@ -119,7 +119,7 @@ export class CookieScheme<OptionsT extends CookieSchemeOptions> extends LocalSch
119119
return response;
120120
}
121121

122-
async fetchUser(endpoint?: HTTPRequest): Promise<HTTPResponse<any> | void> {
122+
override async fetchUser(endpoint?: HTTPRequest): Promise<HTTPResponse<any> | void> {
123123
if (!this.check().valid) {
124124
return Promise.resolve();
125125
}
@@ -156,7 +156,7 @@ export class CookieScheme<OptionsT extends CookieSchemeOptions> extends LocalSch
156156
});
157157
}
158158

159-
reset(): void {
159+
override reset(): void {
160160
if (this.options.cookie.name) {
161161
this.$auth.$storage.setCookie(this.options.cookie.name, null);
162162
}

src/runtime/schemes/laravel-jwt.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { HTTPResponse } from '../../types';
22
import { RefreshScheme } from './refresh';
33

44
export class LaravelJWTScheme extends RefreshScheme {
5-
protected updateTokens(response: HTTPResponse<any>): void {
5+
protected override updateTokens(response: HTTPResponse<any>): void {
66
super.updateTokens(response);
77
}
88
}

src/runtime/schemes/local.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ export class LocalScheme<OptionsT extends LocalSchemeOptions = LocalSchemeOption
128128
this.$auth.reset({ resetInterceptor: false });
129129
}
130130

131+
endpoint = endpoint || {};
131132
endpoint.body = endpoint.body || {};
132133

133134
// Add client id to payload if defined

0 commit comments

Comments
 (0)