Skip to content

Commit

Permalink
feat: 支持 Auth.js 策略及全部功能配置
Browse files Browse the repository at this point in the history
  • Loading branch information
gxmari007 committed Feb 19, 2024
1 parent ff95eb0 commit a8b2a57
Show file tree
Hide file tree
Showing 30 changed files with 2,436 additions and 1,479 deletions.
32 changes: 18 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
"prepack": "nuxt-module-build build",
"dev:local": "nuxi dev playground-local",
"dev:refresh": "nuxi dev playground-refresh",
"dev:build": "nuxi build playground",
"dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground-local && nuxi prepare playground-refresh",
"dev:authjs": "nuxi dev playground-authjs",
"dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground-local && nuxi prepare playground-refresh && nuxi prepare playground-authjs",
"release": "npm run lint && npm run prepack && changelogen --release && npm publish && git push --follow-tags",
"lint": "eslint .",
"test": "vitest run",
Expand All @@ -48,29 +48,33 @@
"access": "public"
},
"dependencies": {
"@nuxt/kit": "^3.9.0",
"@roshan-labs/http": "^0.0.3",
"defu": "^6.1.3",
"h3": "^1.10.0",
"@nuxt/kit": "^3.10.1",
"@roshan-labs/http": "^0.0.4",
"defu": "^6.1.4",
"h3": "^1.10.1",
"knitwork": "^1.0.0",
"nitropack": "^2.8.1",
"ufo": "^1.3.2"
"ufo": "^1.4.0"
},
"devDependencies": {
"@auth/core": "0.26.3",
"@nuxt/devtools": "latest",
"@nuxt/module-builder": "^0.5.5",
"@nuxt/schema": "^3.9.0",
"@nuxt/test-utils": "^3.9.0",
"@roshan-labs/eslint-config": "^1.4.0",
"@nuxt/schema": "^3.10.1",
"@nuxt/test-utils": "^3.11.0",
"@roshan-labs/eslint-config": "^1.4.1",
"@types/jsonwebtoken": "^9.0.5",
"@types/node": "^20.10.6",
"@types/node": "^20.11.16",
"changelogen": "^0.5.5",
"cz-conventional-changelog": "^3.3.0",
"eslint": "^8.56.0",
"jsonwebtoken": "^9.0.2",
"nuxt": "^3.9.0",
"nuxt": "^3.10.1",
"ts-essentials": "^9.4.1",
"vitest": "^1.1.3",
"vitest": "^1.2.2",
"zod": "^3.22.4"
},
"peerDependencies": {
"@auth/core": "0.20.0"
}
}
}
28 changes: 28 additions & 0 deletions playground-authjs/app.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<template>
<div>
<h1>Authjs provider</h1>
<div>
<h3>Status</h3>
<pre>Status: {{ status }}</pre>
<pre>Session: {{ data ?? 'no session data' }}</pre>
</div>
<div>
<h3>Actions</h3>
<button @click="signIn('github')">sign in github</button>
<br />
<button @click="signIn(undefined)">sign in</button>
<br />
<button @click="signIn('no-provider')">no provider</button>
<br />
<button @click="signOut()">sign out</button>
</div>
{{ sessionData }}
<nuxt-page></nuxt-page>
</div>
</template>

<script lang="ts" setup>
const { status, data, signIn, signOut } = useAuth()

const { data: sessionData } = useFetch('/api/session')
</script>
11 changes: 11 additions & 0 deletions playground-authjs/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default defineNuxtConfig({
devtools: { enabled: true },
modules: ['../src/module'],
auth: {
baseURL: 'http://localhost:3000/api/auth',
provider: {
type: 'authjs',
},
globalAppMiddleware: true,
},
})
13 changes: 13 additions & 0 deletions playground-authjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "playground-authjs",
"private": true,
"type": "module",
"scripts": {
"dev": "nuxi dev",
"build": "nuxi build",
"generate": "nuxi generate"
},
"devDependencies": {
"nuxt": "latest"
}
}
3 changes: 3 additions & 0 deletions playground-authjs/pages/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<div></div>
</template>
16 changes: 16 additions & 0 deletions playground-authjs/server/api/auth/[...].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import GitHub from '@auth/core/providers/github'
import type { AuthConfig } from '@auth/core/types'

import { NuxtAuthHandler } from '#auth'

export const options: AuthConfig = {
secret: 'NtoBpkbWs4uTqsEvBJ/UyvWGFYAXQ9/tdNrmQCf7NIQ=',
providers: [
GitHub({
clientId: '10b3f386a001067199c0',
clientSecret: '85bca5873bed7f8744d88917e639518a3cbc3b90',
}),
],
}

export default NuxtAuthHandler(options)
12 changes: 12 additions & 0 deletions playground-authjs/server/api/session.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { options } from './auth/[...]'
import { getServerSession, getServerToken } from '#auth'

export default defineEventHandler(async (event) => {
const session = await getServerSession(event)
const token = await getServerToken(event, options)

return {
session,
token,
}
})
3 changes: 3 additions & 0 deletions playground-authjs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./.nuxt/tsconfig.json"
}
2 changes: 1 addition & 1 deletion playground-local/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<div>
<button v-permission="'add'">Add</button>
<button v-permission="'edit'">Edit</button>
<button v-cloak v-permission="'delete'">Delete</button>
<button v-permission="'delete'">Delete</button>
<button v-permission="['no-permission']">Can't see me</button>
</div>
<h3>Sign out</h3>
Expand Down
2 changes: 1 addition & 1 deletion playground-local/server/api/auth/login.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default defineEventHandler(async (event) => {
// 1. 校验 body 参数
const result = await readValidatedBody(
event,
z.object({ username: z.string().min(1), password: z.literal('123') }).safeParse
z.object({ username: z.string().min(1), password: z.literal('123') }).safeParse,
)

// 2. 参数错误抛出错误
Expand Down
2 changes: 1 addition & 1 deletion playground-refresh/server/api/auth/login.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default defineEventHandler(async (event) => {
// 1. 校验传参
const result = await readValidatedBody(
event,
z.object({ username: z.string().min(1), password: z.literal('123') }).safeParse
z.object({ username: z.string().min(1), password: z.literal('123') }).safeParse,
)

// 2. 参数错误时响应
Expand Down
Loading

0 comments on commit a8b2a57

Please sign in to comment.