Skip to content

Commit

Permalink
chore: init
Browse files Browse the repository at this point in the history
  • Loading branch information
atinux committed Nov 7, 2023
1 parent 18ea43a commit 9b75953
Show file tree
Hide file tree
Showing 13 changed files with 1,578 additions and 19 deletions.
4 changes: 0 additions & 4 deletions .eslintrc

This file was deleted.

10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ Minimalist Authentication module for Nuxt.
- Secured & sealed cookies sessions
- OAuth Providers

## Requirements

This module only works with SSR (server-side rendering) enabled as it uses server API routes. You cannot use this module with `nuxt generate`.

## Quick Setup

1. Add `nuxt-auth-core` dependency to your project
Expand Down Expand Up @@ -141,7 +145,11 @@ Example: `~/server/routes/auth/github.get.ts`
```ts
export default oauth.githubEventHandler({
async onSuccess(event, { user, tokens }) {
await setUserSession(event, { user })
await setUserSession(event, {
user: {
githubId: user.id
}
})
return sendRedirect(event, '/')
},
// Optional, will return a json error and 401 status code by default
Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,20 @@
"test:watch": "vitest watch"
},
"dependencies": {
"@nuxt/kit": "^3.8.0"
"@nuxt/kit": "^3.8.0",
"defu": "^6.1.3",
"ohash": "^1.1.3"
},
"devDependencies": {
"@types/node": "^20.8.9",
"@iconify-json/simple-icons": "^1.1.76",
"@nuxt/devtools": "latest",
"@nuxt/eslint-config": "^0.2.0",
"@nuxt/module-builder": "^0.5.2",
"@nuxt/schema": "^3.8.0",
"@nuxt/test-utils": "^3.8.0",
"@nuxt/ui": "^2.10.0",
"@nuxt/ui-pro": "^0.4.2",
"@types/node": "^20.8.9",
"changelogen": "^0.5.5",
"eslint": "^8.52.0",
"nuxt": "^3.8.0",
Expand Down
4 changes: 4 additions & 0 deletions playground/.env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
NUXT_SESSION_PASSWORD=
# GitHub OAuth
NUXT_OAUTH_GITHUB_CLIENT_ID=
NUXT_OAUTH_GITHUB_CLIENT_SECRET=
# Spotify OAuth
NUXT_OAUTH_SPOTIFY_CLIENT_ID=
NUXT_OAUTH_SPOTIFY_CLIENT_SECRET=
16 changes: 16 additions & 0 deletions playground/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = {
root: true,
extends: [
'@nuxt/eslint-config'
],
rules: {
// Global
semi: ['error', 'never'],
quotes: ['error', 'single'],
'quote-props': ['error', 'as-needed'],
// Vue
'vue/multi-word-component-names': 0,
'vue/max-attributes-per-line': 'off',
'vue/no-v-html': 0
}
}
43 changes: 38 additions & 5 deletions playground/app.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,43 @@
<script setup>
const { loggedIn } = useUserSession()
const { loggedIn, session, clear } = useUserSession()
</script>

<template>
<div>
Nuxt module playground!
{{ loggedIn ? 'Logged in' : 'Not logged in' }}
</div>
<UHeader>
<template #right>
<UButton
v-if="!loggedIn || !session.user.github"
to="/auth/github"
icon="i-simple-icons-github"
external
color="gray"
size="xs"
>
Login with GitHub
</UButton>
<UButton
v-if="!loggedIn || !session.user.spotify"
to="/auth/spotify"
icon="i-simple-icons-spotify"
external
color="gray"
size="xs"
>
Login with Spotify
</UButton>
<UButton
v-if="loggedIn"
color="gray"
size="xs"
@click="clear"
>
Logout
</UButton>
</template>
</UHeader>
<UMain>
<UContainer>
<NuxtPage />
</UContainer>
</UMain>
</template>
9 changes: 8 additions & 1 deletion playground/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
export default defineNuxtConfig({
modules: ['../src/module'],
extends: ['@nuxt/ui-pro'],
modules: [
'../src/module',
'@nuxt/ui'
],
auth: {},
ui: {
icons: ['simple-icons']
},
devtools: { enabled: true },
imports: {
autoImport: true
Expand Down
7 changes: 7 additions & 0 deletions playground/pages/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script setup>
const { session } = useUserSession()
</script>

<template>
<pre>{{ session }}</pre>
</template>
10 changes: 7 additions & 3 deletions playground/server/routes/auth/github.get.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
export default oauth.githubEventHandler({
onSuccess(event, res) {
console.log(res)
async onSuccess(event, { user }) {
await setUserSession(event, {
user: {
github: user,
}
})

return { success: true }
return sendRedirect(event, '/')
}
})
11 changes: 11 additions & 0 deletions playground/server/routes/auth/spotify.get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default oauth.spotifyEventHandler({
async onSuccess(event, { user }) {
await setUserSession(event, {
user: {
spotify: user,
}
})

return sendRedirect(event, '/')
}
})
Loading

0 comments on commit 9b75953

Please sign in to comment.