This repository was archived by the owner on Mar 5, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 29
feat: allow custom base path #169
Open
freality
wants to merge
10
commits into
Hebilicious:main
Choose a base branch
from
freality:feat/custom-base-path
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
24e7010
generate #auth-config module
freality 6f251a4
use basePath variable
freality 6cb8261
use ufo
freality 6f76ab9
update docs
freality 8da234a
cleanup
freality 6d4db7e
add tests
freality 48ea8fc
remove overload definitions
freality 4c3f72a
avoid using "as"
freality 3816403
remove ts-ignore
freality 88c5a10
Merge branch 'main' into feat/custom-base-path
Hebilicious File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
declare module "#auth-config" { | ||
export const verifyClientOnEveryRequest: boolean | ||
export const guestRedirectTo: string | ||
export const authenticatedRedirectTo: string | ||
export const baseUrl: string | ||
export const basePath: string | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { fileURLToPath } from "node:url" | ||
import { describe, expect, it } from "vitest" | ||
import { $fetch, setup } from "@nuxt/test-utils" | ||
import { createPage } from "@nuxt/test-utils/e2e" | ||
|
||
describe("custom base path test", async () => { | ||
await setup({ | ||
rootDir: fileURLToPath(new URL("./fixtures/custom-base-path", import.meta.url)) | ||
}) | ||
|
||
it("displays homepage", async () => { | ||
const html = await $fetch("/") | ||
expect(html).toContain("Sign In") | ||
}) | ||
|
||
it("displays signin page", async () => { | ||
const page = await $fetch("/custom/auth/signin") | ||
expect(page).toContain("Username") | ||
expect(page).toContain("Password") | ||
}) | ||
|
||
it("fetches auth providers", async () => { | ||
const json = await $fetch("/custom/auth/providers") | ||
expect(json).toMatchObject(expect.objectContaining({ | ||
credentials: expect.objectContaining({ | ||
id: "credentials", | ||
name: "Credentials", | ||
type: "credentials", | ||
signinUrl: expect.stringMatching(/\/custom\/auth\/signin\/credentials$/), | ||
callbackUrl: expect.stringMatching(/\/custom\/auth\/callback\/credentials$/) | ||
}) | ||
})) | ||
}) | ||
|
||
// @todo: requires install playwright-core | ||
it.skip("can signin", async () => { | ||
const page = await createPage("/custom/auth/signin") | ||
expect(await page.title()).toBe("Sign In") | ||
|
||
await page.getByLabel("Username").fill("admin") | ||
await page.getByLabel("Password").fill("admin") | ||
await page.locator("#submitButton").click() | ||
|
||
expect(await page.getByText("authenticated")).toBeTruthy() | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export default defineNuxtConfig({ | ||
modules: ["../../packages/authjs-nuxt/src/module.ts"], | ||
authJs: { | ||
baseUrl: "http://localhost:3000", | ||
basePath: "/custom/auth", | ||
verifyClientOnEveryRequest: true | ||
}, | ||
experimental: { | ||
renderJsonPayloads: true | ||
}, | ||
runtimeConfig: { | ||
authJs: { secret: "/OEjlRC2DK74ZEj5nl8qHNy+E6/JptnouIyHnANbBz0=" }, | ||
github: { | ||
clientId: "", | ||
clientSecret: "" | ||
} | ||
} | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "basic-fixture", | ||
"private": true, | ||
"dependencies": { | ||
"@hebilicious/authjs-nuxt": "latest", | ||
"nuxt": "3.7.4" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<script setup lang="ts"> | ||
const { signIn, signOut, session, status, cookies } = useAuth() | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<div> | ||
<button @click="signIn(`credentials`)"> | ||
Sign In | ||
</button> | ||
<button @click="signOut()"> | ||
Sign Out | ||
</button> | ||
</div> | ||
<div> | ||
<pre>{{ status }}</pre> | ||
<pre>{{ session?.user }}</pre> | ||
<pre>{{ cookies }}</pre> | ||
</div> | ||
</div> | ||
</template> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<script setup> | ||
definePageMeta({ middleware: "auth" }) | ||
</script> | ||
|
||
<template> | ||
<h1>PRIVATE</h1> | ||
</template> |
36 changes: 36 additions & 0 deletions
36
test/fixtures/custom-base-path/server/routes/custom/auth/[...].ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import CredentialsProvider from "@auth/core/providers/credentials" | ||
import type { AuthConfig } from "@auth/core/types" | ||
|
||
import { NuxtAuthHandler } from "#auth" | ||
|
||
// The #auth virtual import comes from this module. You can use it on the client | ||
// and server side, however not every export is universal. For example do not | ||
// use sign-in and sign-out on the server side. | ||
|
||
const runtimeConfig = useRuntimeConfig() | ||
|
||
// Refer to Auth.js docs for more details | ||
|
||
export const authOptions: AuthConfig = { | ||
secret: runtimeConfig.authJs.secret, | ||
providers: [ | ||
CredentialsProvider({ | ||
credentials: { | ||
username: { label: "Username", type: "text", placeholder: "admin" }, | ||
password: { label: "Password", type: "password" } | ||
}, | ||
async authorize(credentials) { | ||
if ( | ||
credentials.username === "admin" | ||
&& credentials.password === "admin" | ||
) | ||
return { id: "1", name: "admin", email: "admin", role: "test" } | ||
return null | ||
} | ||
}) | ||
] | ||
} | ||
|
||
export default NuxtAuthHandler(authOptions, runtimeConfig) | ||
// If you don't want to pass the full runtime config, | ||
// you can pass something like this: { public: { authJs: { baseUrl: "" } } } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.