From 734953f89bd19192540452d9e0fbb225d01b28c3 Mon Sep 17 00:00:00 2001 From: Don Isaac Date: Thu, 10 Oct 2024 06:19:07 -0400 Subject: [PATCH] fix: violations of upcoming `eslint/no-else-return` oxlint rule (#921) --- playground-authjs/server/api/auth/[...].ts | 11 ++++---- src/runtime/composables/commonAuthState.ts | 6 ++-- src/runtime/composables/local/useAuth.ts | 9 ++---- src/runtime/middleware/auth.ts | 28 +++++++++---------- src/runtime/plugins/refresh-token.server.ts | 4 +-- .../server/services/authjs/nuxtAuthHandler.ts | 8 ++---- 6 files changed, 27 insertions(+), 39 deletions(-) diff --git a/playground-authjs/server/api/auth/[...].ts b/playground-authjs/server/api/auth/[...].ts index d310edee..eccec0c7 100644 --- a/playground-authjs/server/api/auth/[...].ts +++ b/playground-authjs/server/api/auth/[...].ts @@ -35,14 +35,13 @@ export default NuxtAuthHandler({ // Any object returned will be saved in `user` property of the JWT return user } - else { - console.error('Warning: Malicious login attempt registered, bad credentials provided') - // If you return null then an error will be displayed advising the user to check their details. - return null + console.error('Warning: Malicious login attempt registered, bad credentials provided') - // You can also Reject this callback with an Error thus the user will be sent to the error page with the error message as a query parameter - } + // If you return null then an error will be displayed advising the user to check their details. + return null + + // You can also Reject this callback with an Error thus the user will be sent to the error page with the error message as a query parameter } }) ] diff --git a/src/runtime/composables/commonAuthState.ts b/src/runtime/composables/commonAuthState.ts index 08b2a006..19c53d67 100644 --- a/src/runtime/composables/commonAuthState.ts +++ b/src/runtime/composables/commonAuthState.ts @@ -24,12 +24,10 @@ export function makeCommonAuthState() { if (loading.value) { return 'loading' } - else if (data.value) { + if (data.value) { return 'authenticated' } - else { - return 'unauthenticated' - } + return 'unauthenticated' }) // Determine base url of app diff --git a/src/runtime/composables/local/useAuth.ts b/src/runtime/composables/local/useAuth.ts index cd4ff21c..1b311dfc 100644 --- a/src/runtime/composables/local/useAuth.ts +++ b/src/runtime/composables/local/useAuth.ts @@ -152,9 +152,7 @@ async function getSession(getSessionOptions?: GetSessionOptions): Promise { // @ts-ignore This is valid for a backend-type of `authjs`, where sign-in accepts a provider as a first argument return signIn(undefined, signInOptions) as ReturnType } - else if (typeof metaAuth === 'object' && metaAuth.navigateUnauthenticatedTo) { + if (typeof metaAuth === 'object' && metaAuth.navigateUnauthenticatedTo) { return navigateTo(metaAuth.navigateUnauthenticatedTo) } - else { - if (typeof globalAppMiddleware === 'object' && globalAppMiddleware.addDefaultCallbackUrl) { - let redirectUrl: string = to.fullPath - if (typeof globalAppMiddleware.addDefaultCallbackUrl === 'string') { - redirectUrl = globalAppMiddleware.addDefaultCallbackUrl - } - - return navigateTo({ - path: authConfig.provider.pages.login, - query: { - redirect: redirectUrl - } - }) + if (typeof globalAppMiddleware === 'object' && globalAppMiddleware.addDefaultCallbackUrl) { + let redirectUrl: string = to.fullPath + if (typeof globalAppMiddleware.addDefaultCallbackUrl === 'string') { + redirectUrl = globalAppMiddleware.addDefaultCallbackUrl } - return navigateTo(authConfig.provider.pages.login) + + return navigateTo({ + path: authConfig.provider.pages.login, + query: { + redirect: redirectUrl + } + }) } + return navigateTo(authConfig.provider.pages.login) }) diff --git a/src/runtime/plugins/refresh-token.server.ts b/src/runtime/plugins/refresh-token.server.ts index bd79d432..1d05065d 100644 --- a/src/runtime/plugins/refresh-token.server.ts +++ b/src/runtime/plugins/refresh-token.server.ts @@ -63,9 +63,7 @@ export default defineNuxtPlugin({ ) return } - else { - rawRefreshToken.value = extractedRefreshToken - } + rawRefreshToken.value = extractedRefreshToken } rawToken.value = extractedToken diff --git a/src/runtime/server/services/authjs/nuxtAuthHandler.ts b/src/runtime/server/services/authjs/nuxtAuthHandler.ts index ee6baa18..82e845a6 100644 --- a/src/runtime/server/services/authjs/nuxtAuthHandler.ts +++ b/src/runtime/server/services/authjs/nuxtAuthHandler.ts @@ -253,12 +253,10 @@ function getRequestBaseFromH3Event(event: H3Event, trustHost: boolean): string { return `${protocol}://${host}` } - else { - // This may throw, we don't catch it - const origin = getServerOrigin(event) + // This may throw, we don't catch it + const origin = getServerOrigin(event) - return origin - } + return origin } /** Actions supported by auth handler */