Skip to content

Commit

Permalink
feat: add logto auth
Browse files Browse the repository at this point in the history
  • Loading branch information
ZerNico committed May 8, 2023
1 parent 09a9577 commit f44f902
Show file tree
Hide file tree
Showing 27 changed files with 8,189 additions and 81 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
dist
node_modules
.nuxt
4 changes: 0 additions & 4 deletions .eslintrc

This file was deleted.

4 changes: 4 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
root: true,
extends: ['@nuxt/eslint-config', 'prettier'],
}
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist
node_modules
.nuxt
6 changes: 6 additions & 0 deletions .prettierrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
semi: false,
trailingComma: 'es5',
singleQuote: true,
printWidth: 120,
}
Empty file added CHANGELOG.md
Empty file.
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2012-2023 Scott Chacon and others

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
113 changes: 69 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,94 +1,119 @@
<!--
Get your module up and running quickly.
Find and replace all on all files (CMD+SHIFT+F):
- Name: My Module
- Package name: my-module
- Description: My new Nuxt module
-->

# My Module
# Nuxt Logto

[![npm version][npm-version-src]][npm-version-href]
[![npm downloads][npm-downloads-src]][npm-downloads-href]
[![License][license-src]][license-href]
[![Nuxt][nuxt-src]][nuxt-href]

My new Nuxt module for doing amazing things.
Logto auth module for Nuxt 3.

- [&nbsp;Release Notes](/CHANGELOG.md)
<!-- - [🏀 Online playground](https://stackblitz.com/github/your-org/my-module?file=playground%2Fapp.vue) -->
<!-- - [📖 &nbsp;Documentation](https://example.com) -->

## Features

<!-- Highlight some of the features your module provide here -->
-&nbsp;Foo
- 🚠 &nbsp;Bar
- 🌲 &nbsp;Baz

## Quick Setup

1. Add `my-module` dependency to your project
1. Add `@zernico/nuxt-logto` dependency to your project

```bash
# Using pnpm
pnpm add -D my-module
pnpm add -D @zernico/nuxt-logto

# Using yarn
yarn add --dev my-module
yarn add --dev @zernico/nuxt-logto

# Using npm
npm install --save-dev my-module
npm install --save-dev @zernico/nuxt-logto
```

2. Add `@zernico/nuxt-logto` to the `modules` section of `nuxt.config.ts`

```js
export default defineNuxtConfig({
modules: ['@zernico/nuxt-logto'],
})
```

2. Add `my-module` to the `modules` section of `nuxt.config.ts`
3. Configure it:

```js
export default defineNuxtConfig({
modules: [
'my-module'
]
modules: ['@zernico/nuxt-logto'],
logto: {
appId: '<your-application-id>',
appSecret: '<your-app-secret-copied-from-console>',
endpoint: '<your-logto-endpoint>', // E.g. http://localhost:3001
baseUrl: '<your-nextjs-app-base-url>', // E.g. http://localhost:3000
cookieSecret: 'complex_password_at_least_32_characters_long',
cookieSecure: process.env.NODE_ENV === 'production',
cookieHttpOnly: true,
resources: ['<your-resource-id>'], // optionally add a resource
},
})
```

That's it! You can now use My Module in your Nuxt app ✨
4. Add the api routes

```js
// lib/logto.ts
import LogtoClient from '@logto/next'

export const logtoClient = new LogtoClient()
```

```js
// server/api/logto/[action].ts
import { logtoClient } from '~/lib/logto'

export default logtoClient.handleAuthRoutes()
```

5. Optional configuration

```js
// server/api/logto/[action].ts
import { logtoClient } from '~/lib/logto'

export default logtoClient.handleAuthRoutes({
getAccessToken: true, // get access token from logto
resource: '<your-resource-id>', // optionally add a resource to audience of access token
fetchUserInfo: true, // fetch user info from logto, in most cases you want to use claims instead
})
```

That's it! You can now use Nuxt Logto in your Nuxt app ✨

## Development

```bash
# Install dependencies
npm install
pnpm install

# Generate type stubs
npm run dev:prepare
pnpm run dev:prepare

# Develop with the playground
npm run dev
pnpm run dev

# Build the playground
npm run dev:build
pnpm run dev:build

# Run ESLint
npm run lint
pnpm run lint

# Run Vitest
npm run test
npm run test:watch
pnpm run test
pnpm run test:watch

# Release new version
npm run release
```

<!-- Badges -->
[npm-version-src]: https://img.shields.io/npm/v/my-module/latest.svg?style=flat&colorA=18181B&colorB=28CF8D
[npm-version-href]: https://npmjs.com/package/my-module

[npm-downloads-src]: https://img.shields.io/npm/dm/my-module.svg?style=flat&colorA=18181B&colorB=28CF8D
[npm-downloads-href]: https://npmjs.com/package/my-module

[license-src]: https://img.shields.io/npm/l/my-module.svg?style=flat&colorA=18181B&colorB=28CF8D
[license-href]: https://npmjs.com/package/my-module

[npm-version-src]: https://img.shields.io/npm/v/@zernico/nuxt-logto/latest.svg?style=flat&colorA=18181B&colorB=28CF8D
[npm-version-href]: https://npmjs.com/package/@zernico/nuxt-logto
[npm-downloads-src]: https://img.shields.io/npm/dm/@zernico/nuxt-logto.svg?style=flat&colorA=18181B&colorB=28CF8D
[npm-downloads-href]: https://npmjs.com/package/@zernico/nuxt-logto
[license-src]: https://img.shields.io/npm/l/@zernico/nuxt-logto.svg?style=flat&colorA=18181B&colorB=28CF8D
[license-href]: https://npmjs.com/package/@zernico/nuxt-logto
[nuxt-src]: https://img.shields.io/badge/Nuxt-18181B?logo=nuxt.js
[nuxt-href]: https://nuxt.com
24 changes: 18 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"name": "my-module",
"version": "1.0.0",
"description": "My new Nuxt module",
"repository": "your-org/my-module",
"name": "@zernico/nuxt-logto",
"version": "0.0.0",
"description": "Logto auth module for Nuxt 3",
"repository": {
"type": "git",
"url": "https://github.com/ZerNico/nuxt-logto.git"
},
"license": "MIT",
"type": "module",
"exports": {
Expand All @@ -24,21 +27,30 @@
"dev:prepare": "nuxt-module-build --stub && nuxi prepare playground",
"release": "npm run lint && npm run test && npm run prepack && changelogen --release && npm publish && git push --follow-tags",
"lint": "eslint .",
"prettier": "prettier --write .",
"test": "vitest run",
"test:watch": "vitest watch"
},
"dependencies": {
"@nuxt/kit": "^3.4.3"
"@logto/node": "^2.0.0",
"@nuxt/kit": "^3.4.3",
"defu": "^6.1.2",
"h3": "^1.6.5",
"iron-session": "^6.3.1"
},
"devDependencies": {
"@types/node": "^18",
"@nuxt/eslint-config": "^0.1.1",
"@nuxt/module-builder": "^0.3.0",
"@nuxt/schema": "^3.4.3",
"@nuxt/test-utils": "^3.4.3",
"@types/node": "^18",
"@types/supertest": "^2.0.12",
"changelogen": "^0.5.3",
"eslint": "^8.39.0",
"eslint-config-prettier": "^8.8.0",
"nuxt": "^3.4.3",
"prettier": "^2.8.8",
"supertest": "^6.3.3",
"vitest": "^0.30.1"
}
}
17 changes: 14 additions & 3 deletions playground/app.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
<script setup>
import { useLogto } from './.nuxt/imports'
const { claims, accessToken, signIn, signOut, signUp, refresh } = useLogto()
</script>

<template>
<div>
Nuxt module playground!
{{ JSON.stringify(claims) }}

{{ JSON.stringify(accessToken) }}

<button @click="() => signIn()">Sign In</button>
<button @click="() => signUp()">Sign Up</button>
<button @click="() => signOut()">Sign Out</button>
<button @click="() => refresh()">Refresh</button>
</div>
</template>

<script setup>
</script>
3 changes: 3 additions & 0 deletions playground/lib/logto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { LogtoClient } from '#logto'

export const logtoClient = new LogtoClient()
12 changes: 11 additions & 1 deletion playground/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
export default defineNuxtConfig({
modules: ['../src/module'],
myModule: {}
ssr: true,
logto: {
appId: '<your-application-id>',
appSecret: '<your-app-secret-copied-from-console>',
endpoint: '<your-logto-endpoint>', // E.g. http://localhost:3001
baseUrl: '<your-nextjs-app-base-url>', // E.g. http://localhost:3000
cookieSecret: 'complex_password_at_least_32_characters_long',
cookieSecure: process.env.NODE_ENV === 'production',
cookieHttpOnly: true,
resources: ['<your-resource-id>'], // optionally add a resource
},
})
2 changes: 1 addition & 1 deletion playground/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"private": true,
"name": "my-module-playground"
"name": "nuxt-logto-playground"
}
6 changes: 6 additions & 0 deletions playground/server/api/logto/[action].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { logtoClient } from '~/lib/logto'

export default logtoClient.handleAuthRoutes({
getAccessToken: true,
resource: '<your-resource-id>',
})
Loading

0 comments on commit f44f902

Please sign in to comment.