Skip to content

Commit 19caed2

Browse files
committed
chore: init
1 parent 7b8ff47 commit 19caed2

29 files changed

+7972
-0
lines changed

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
[*]
4+
indent_size = 2
5+
indent_style = space
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist
2+
node_modules

.eslintrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"root": true,
3+
"extends": ["@nuxt/eslint-config"]
4+
}

.gitignore

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Dependencies
2+
node_modules
3+
4+
# Logs
5+
*.log*
6+
7+
# Temp directories
8+
.temp
9+
.tmp
10+
.cache
11+
12+
# Yarn
13+
**/.yarn/cache
14+
**/.yarn/*state*
15+
16+
# Generated dirs
17+
dist
18+
19+
# Nuxt
20+
.nuxt
21+
.output
22+
.data
23+
.vercel_build_output
24+
.build-*
25+
.netlify
26+
27+
# Env
28+
.env
29+
30+
# Testing
31+
reports
32+
coverage
33+
*.lcov
34+
.nyc_output
35+
36+
# VSCode
37+
.vscode/*
38+
!.vscode/settings.json
39+
!.vscode/tasks.json
40+
!.vscode/launch.json
41+
!.vscode/extensions.json
42+
!.vscode/*.code-snippets
43+
44+
# Intellij idea
45+
*.iml
46+
.idea
47+
48+
# OSX
49+
.DS_Store
50+
.AppleDouble
51+
.LSOverride
52+
.AppleDB
53+
.AppleDesktop
54+
Network Trash Folder
55+
Temporary Items
56+
.apdisk

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
shamefully-hoist=true

.nuxtrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
imports.autoImport=false
2+
typescript.includeWorkspace=true

README.md

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
# Nuxt Auth Core
2+
3+
[![npm version][npm-version-src]][npm-version-href]
4+
[![npm downloads][npm-downloads-src]][npm-downloads-href]
5+
[![License][license-src]][license-href]
6+
[![Nuxt][nuxt-src]][nuxt-href]
7+
8+
Minimalist Authentication module for Nuxt.
9+
10+
- [ Release Notes](/CHANGELOG.md)
11+
<!-- - [🏀 Online playground](https://stackblitz.com/github/your-org/nuxt-auth-core?file=playground%2Fapp.vue) -->
12+
<!-- - [📖 &nbsp;Documentation](https://example.com) -->
13+
14+
## Features
15+
16+
<!-- Highlight some of the features your module provide here -->
17+
- OAuth Providers
18+
- Secured & sealed sessions
19+
-
20+
21+
## Quick Setup
22+
23+
1. Add `nuxt-auth-core` dependency to your project
24+
25+
```bash
26+
# Using pnpm
27+
pnpm add -D nuxt-auth-core
28+
29+
# Using yarn
30+
yarn add --dev nuxt-auth-core
31+
32+
# Using npm
33+
npm install --save-dev nuxt-auth-core
34+
```
35+
36+
2. Add `nuxt-auth-core` to the `modules` section of `nuxt.config.ts`
37+
38+
```js
39+
export default defineNuxtConfig({
40+
modules: [
41+
'nuxt-auth-core'
42+
]
43+
})
44+
```
45+
46+
3. Add a `NUXT_SESSION_PASSWORD` env variable with at least 32 characters in the `.env`.
47+
48+
```bash
49+
# .env
50+
NUXT_SESSION_PASSWORD=password-with-at-least-32-characters
51+
```
52+
53+
Nuxt Auth Core can generate one for you when running Nuxt in development the first time when no `NUXT_SESSION_PASSWORD` is set.
54+
55+
4. That's it! You can now add authentication to your Nuxt app ✨
56+
57+
## Vue Composables
58+
59+
Nuxt Neo automatically add some plugins to fetch the current user session to let you access it from your Vue components.
60+
61+
### User Session
62+
63+
```vue
64+
<script setup>
65+
const { loggedIn, user, session, clear } = useUserSession()
66+
</script>
67+
68+
<template>
69+
<div v-if="loggedIn">
70+
<h1>Welcome {{ user.login }}!</h1>
71+
<p>Logged in since {{ session.loggedInAt }}</p>
72+
<button @click="clear">Logout</button>
73+
</div>
74+
<div v-else>
75+
<h1>Not logged in</h1>
76+
<a href="/api/auth/github">Login with GitHub</a>
77+
</div>
78+
</template>
79+
```
80+
81+
## Server Utils
82+
83+
The following helpers are auto-imported in your `server/` directory.
84+
85+
### Session Management
86+
87+
```ts
88+
// Set a user session, note that this data is encrypted in the cookie but can be decrypted with an API call
89+
// Only store the data that allow you to recognize an user, but do not store sensitive data
90+
await setUserSession(event, {
91+
user: {
92+
// ... user data
93+
},
94+
loggedInAt: new Date()
95+
// Any extra fields
96+
})
97+
98+
// Get the current user session
99+
const session = await getUserSession(event)
100+
101+
// Clear the current user session
102+
await clearUserSession(event)
103+
104+
// Require a user session (send back 401 if no `user` key in session)
105+
const session = await requireUserSession(event)
106+
```
107+
108+
### OAuth Event Handlers
109+
110+
All helpers are exposed from the `oauth` global variable and can be used in your server routes or API routes.
111+
112+
The pattern is `oauth.<provider>EventHandler({ onSuccess, config?, onError? })`, example: `oauth.githubEventHandler`.
113+
114+
The helper returns an event handler that automatically redirects to the provider authorization page and then call `onSuccess` or `onError` depending on the result.
115+
116+
The `config` can be defined directly from the `runtimeConfig` in your `nuxt.config.ts`:
117+
118+
```ts
119+
export default defineNuxtConfig({
120+
runtimeConfig: {
121+
oauth: {
122+
<provider>: {
123+
clientId: '...',
124+
clientSecret: '...'
125+
}
126+
}
127+
}
128+
})
129+
```
130+
131+
It can also be set using environment variables:
132+
- `NUXT_OAUTH_<PROVIDER>_CLIENT_ID`
133+
- `NUXT_OAUTH_<PROVIDER>_CLIENT_SECRET`
134+
135+
Supported providers:
136+
- GitHub
137+
- Spotify
138+
139+
### Example
140+
141+
Example: `~/server/routes/auth/github.get.ts`
142+
143+
```ts
144+
export default oauth.githubEventHandler({
145+
async onSuccess(event, { user, tokens }) {
146+
await setUserSession(event, { user })
147+
return sendRedirect(event, '/')
148+
},
149+
// Optional, will return a json error and 401 status code by default
150+
onError(event, error) {
151+
console.error('GitHub OAuth error:', error)
152+
return sendRedirect(event, '/')
153+
},
154+
})
155+
```
156+
157+
Make sure to set the callback URL in your OAuth app settings as `<your-domain>/auth/github`.
158+
159+
160+
## Development
161+
162+
```bash
163+
# Install dependencies
164+
npm install
165+
166+
# Generate type stubs
167+
npm run dev:prepare
168+
169+
# Develop with the playground
170+
npm run dev
171+
172+
# Build the playground
173+
npm run dev:build
174+
175+
# Run ESLint
176+
npm run lint
177+
178+
# Run Vitest
179+
npm run test
180+
npm run test:watch
181+
182+
# Release new version
183+
npm run release
184+
```
185+
186+
<!-- Badges -->
187+
[npm-version-src]: https://img.shields.io/npm/v/nuxt-auth-core/latest.svg?style=flat&colorA=18181B&colorB=28CF8D
188+
[npm-version-href]: https://npmjs.com/package/nuxt-auth-core
189+
190+
[npm-downloads-src]: https://img.shields.io/npm/dm/nuxt-auth-core.svg?style=flat&colorA=18181B&colorB=28CF8D
191+
[npm-downloads-href]: https://npmjs.com/package/nuxt-auth-core
192+
193+
[license-src]: https://img.shields.io/npm/l/nuxt-auth-core.svg?style=flat&colorA=18181B&colorB=28CF8D
194+
[license-href]: https://npmjs.com/package/nuxt-auth-core
195+
196+
[nuxt-src]: https://img.shields.io/badge/Nuxt-18181B?logo=nuxt.js
197+
[nuxt-href]: https://nuxt.com

package.json

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"name": "nuxt-auth-core",
3+
"version": "0.1.0",
4+
"description": "Minimalist Auth module for Nuxt",
5+
"repository": "Atinux/nuxt-auth-core",
6+
"license": "MIT",
7+
"type": "module",
8+
"exports": {
9+
".": {
10+
"types": "./dist/types.d.ts",
11+
"import": "./dist/module.mjs",
12+
"require": "./dist/module.cjs"
13+
}
14+
},
15+
"main": "./dist/module.cjs",
16+
"types": "./dist/types.d.ts",
17+
"files": [
18+
"dist"
19+
],
20+
"scripts": {
21+
"prepack": "nuxt-module-build build",
22+
"dev": "nuxi dev playground",
23+
"dev:build": "nuxi build playground",
24+
"dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground",
25+
"release": "npm run lint && npm run test && npm run prepack && changelogen --release && npm publish && git push --follow-tags",
26+
"lint": "eslint .",
27+
"test": "vitest run",
28+
"test:watch": "vitest watch"
29+
},
30+
"dependencies": {
31+
"@nuxt/kit": "^3.8.0"
32+
},
33+
"devDependencies": {
34+
"@types/node": "^20.8.9",
35+
"@nuxt/devtools": "latest",
36+
"@nuxt/eslint-config": "^0.2.0",
37+
"@nuxt/module-builder": "^0.5.2",
38+
"@nuxt/schema": "^3.8.0",
39+
"@nuxt/test-utils": "^3.8.0",
40+
"changelogen": "^0.5.5",
41+
"eslint": "^8.52.0",
42+
"nuxt": "^3.8.0",
43+
"vitest": "^0.33.0"
44+
}
45+
}

playground/app.vue

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script setup>
2+
const { loggedIn } = useUserSession()
3+
</script>
4+
5+
<template>
6+
<div>
7+
Nuxt module playground!
8+
{{ loggedIn ? 'Logged in' : 'Not logged in' }}
9+
</div>
10+
</template>

playground/nuxt.config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default defineNuxtConfig({
2+
modules: ['../src/module'],
3+
auth: {},
4+
devtools: { enabled: true },
5+
imports: {
6+
autoImport: true
7+
}
8+
})

0 commit comments

Comments
 (0)