mirror of
https://github.com/bitinflow/nuxt-oauth.git
synced 2026-03-13 13:45:59 +00:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5454c9677b | ||
|
|
434c335e3f | ||
|
|
ebad02a1e1 | ||
|
|
0981a12d08 | ||
|
|
36ccf819bd | ||
|
|
f2e4b5c1c9 | ||
|
|
e0c8c411a1 | ||
|
|
15c3d43831 | ||
|
|
c954054621 |
14
CHANGELOG.md
Normal file
14
CHANGELOG.md
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
# Changelog
|
||||||
|
|
||||||
|
|
||||||
|
## v1.0.5
|
||||||
|
|
||||||
|
## v1.0.4
|
||||||
|
|
||||||
|
## v1.0.3
|
||||||
|
|
||||||
|
## v1.0.2
|
||||||
|
|
||||||
|
## v1.0.0
|
||||||
|
|
||||||
|
Initial Release
|
||||||
65
README.md
65
README.md
@@ -1,47 +1,66 @@
|
|||||||
# @bitinflow/nuxt-oauth
|
# @bitinflow/nuxt-oauth
|
||||||
|
|
||||||
[![npm version][npm-version-src]][npm-version-href]
|
> Nuxt module for OAuth2 authentication
|
||||||
[![npm downloads][npm-downloads-src]][npm-downloads-href]
|
|
||||||
[![License][license-src]][license-href]
|
|
||||||
|
|
||||||
> My new Nuxt module
|
|
||||||
|
|
||||||
- [✨ Release Notes](/CHANGELOG.md)
|
- [✨ Release Notes](/CHANGELOG.md)
|
||||||
<!-- - [📖 Documentation](https://example.com) -->
|
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
<!-- Highlight some of the features your module provide here -->
|
- 📦 OAuth2 authentication
|
||||||
- ⛰ Foo
|
- 📦 Supports only one OAuth2 provider
|
||||||
- 🚠 Bar
|
- 📦 Supports only implicit flow
|
||||||
- 🌲 Baz
|
|
||||||
|
|
||||||
## Quick Setup
|
## Quick Setup
|
||||||
|
|
||||||
1. Add `my-module` dependency to your project
|
1. Add `@bitinflow/nuxt-oauth` dependency to your project
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Using pnpm
|
# Using pnpm
|
||||||
pnpm add -D my-module
|
pnpm add -D @bitinflow/nuxt-oauth
|
||||||
|
|
||||||
# Using yarn
|
# Using yarn
|
||||||
yarn add --dev my-module
|
yarn add --dev @bitinflow/nuxt-oauth
|
||||||
|
|
||||||
# Using npm
|
# Using npm
|
||||||
npm install --save-dev my-module
|
npm install --save-dev @bitinflow/nuxt-oauth
|
||||||
```
|
```
|
||||||
|
|
||||||
2. Add `my-module` to the `modules` section of `nuxt.config.ts`
|
2. Add `@bitinflow/nuxt-oauth` to the `modules` section of `nuxt.config.ts` and disable `ssr`.
|
||||||
|
|
||||||
|
Or alternatively disable `ssr` via `routeRules`, only for pages where `auth` or `guest` middlewares are needed. Typically account section and login page.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
export default defineNuxtConfig({
|
export default defineNuxtConfig({
|
||||||
modules: [
|
modules: [
|
||||||
'my-module'
|
'@bitinflow/nuxt-oauth'
|
||||||
]
|
],
|
||||||
|
|
||||||
|
ssr: false,
|
||||||
|
// or
|
||||||
|
routeRules: {
|
||||||
|
'/account/**': { ssr: false },
|
||||||
|
'/auth/**': { ssr: false }
|
||||||
|
},
|
||||||
|
|
||||||
|
oauth: {
|
||||||
|
redirect: {
|
||||||
|
login: '/login',
|
||||||
|
logout: '/',
|
||||||
|
callback: '/login',
|
||||||
|
home: '/home'
|
||||||
|
},
|
||||||
|
endpoints: {
|
||||||
|
authorization: 'https://example.com/v1/oauth/authorization',
|
||||||
|
userInfo: `https://example.com/api/users/me`,
|
||||||
|
logout: 'https://example.com/oauth/logout'
|
||||||
|
},
|
||||||
|
clientId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
|
||||||
|
scope: ['user:read']
|
||||||
|
},
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
That's it! You can now use My Module in your Nuxt app ✨
|
That's it! You can now use @bitinflow/nuxt-oauth in your Nuxt app ✨
|
||||||
|
|
||||||
## Development
|
## Development
|
||||||
|
|
||||||
@@ -68,13 +87,3 @@ npm run test:watch
|
|||||||
# Release new version
|
# Release new version
|
||||||
npm run release
|
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
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@bitinflow/nuxt-oauth",
|
"name": "@bitinflow/nuxt-oauth",
|
||||||
"version": "1.0.1",
|
"version": "1.0.5",
|
||||||
"description": "Nuxt 3 OAuth Module",
|
"description": "Nuxt 3 OAuth Module",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
export default defineNuxtConfig({
|
export default defineNuxtConfig({
|
||||||
modules: ['../src/module'],
|
modules: ['../src/module'],
|
||||||
|
|
||||||
|
ssr: false,
|
||||||
|
|
||||||
oauth: {
|
oauth: {
|
||||||
redirect: {
|
redirect: {
|
||||||
login: '/login/', // sandbox appends / at the end of url
|
login: '/login/', // sandbox appends / at the end of url
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ export interface ModuleOptions {
|
|||||||
endpoints: {
|
endpoints: {
|
||||||
authorization: string,
|
authorization: string,
|
||||||
userInfo: string,
|
userInfo: string,
|
||||||
logout: string
|
logout: string | null
|
||||||
},
|
},
|
||||||
clientId: string,
|
clientId: string,
|
||||||
scope: string[]
|
scope: string[]
|
||||||
@@ -28,7 +28,7 @@ const defaults: ModuleOptions = {
|
|||||||
endpoints: {
|
endpoints: {
|
||||||
authorization: 'https://accounts.bitinflow.com/oauth/authorize',
|
authorization: 'https://accounts.bitinflow.com/oauth/authorize',
|
||||||
userInfo: `https://accounts.bitinflow.com/api/v3/user`,
|
userInfo: `https://accounts.bitinflow.com/api/v3/user`,
|
||||||
logout: 'https://accounts.bitinflow.com/logout'
|
logout: null,
|
||||||
},
|
},
|
||||||
clientId: 'please-set-client-id',
|
clientId: 'please-set-client-id',
|
||||||
scope: ['user:read']
|
scope: ['user:read']
|
||||||
|
|||||||
@@ -45,10 +45,20 @@ export default async (options: ComposableOptions = {
|
|||||||
accessToken.value = null;
|
accessToken.value = null;
|
||||||
user.value = null;
|
user.value = null;
|
||||||
|
|
||||||
return navigateTo('/')
|
if (authConfig.endpoints.logout) {
|
||||||
|
// create oauth logout url
|
||||||
|
const params = new URLSearchParams({
|
||||||
|
client_id: authConfig.clientId,
|
||||||
|
redirect_uri: window.location.origin + authConfig.redirect.logout
|
||||||
|
})
|
||||||
|
|
||||||
|
window.location.href = `${authConfig.endpoints.logout}?${params.toString()}`
|
||||||
|
}
|
||||||
|
|
||||||
|
return navigateTo(authConfig.redirect.logout)
|
||||||
}
|
}
|
||||||
|
|
||||||
const setBearer = async (token: string, tokenType: string, expires: number) => {
|
const setBearerToken = async (token: string, tokenType: string, expires: number) => {
|
||||||
accessToken.value = {token, tokenType, expiresAt: Date.now() + expires * 1000};
|
accessToken.value = {token, tokenType, expiresAt: Date.now() + expires * 1000};
|
||||||
await fetchUser()
|
await fetchUser()
|
||||||
}
|
}
|
||||||
@@ -58,11 +68,19 @@ export default async (options: ComposableOptions = {
|
|||||||
await fetchUser()
|
await fetchUser()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const bearerToken = () => {
|
||||||
|
return accessToken.value
|
||||||
|
? `${accessToken.value.tokenType} ${accessToken.value.token}`
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
user,
|
user,
|
||||||
signIn,
|
signIn,
|
||||||
signOut,
|
signOut,
|
||||||
setBearer,
|
setBearerToken,
|
||||||
|
bearerToken,
|
||||||
|
accessToken,
|
||||||
authConfig
|
authConfig
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,17 +3,22 @@ import useAuth from "./composables/useAuth"
|
|||||||
|
|
||||||
export default defineNuxtPlugin(() => {
|
export default defineNuxtPlugin(() => {
|
||||||
addRouteMiddleware('auth', async (to) => {
|
addRouteMiddleware('auth', async (to) => {
|
||||||
const {user, authConfig, setBearer} = await useAuth()
|
const {user, authConfig, setBearerToken} = await useAuth()
|
||||||
|
|
||||||
if (to.path === authConfig.redirect.callback) {
|
if (to.path === authConfig.redirect.callback) {
|
||||||
const params = new URLSearchParams(to.hash.substring(1))
|
const queryParams = new URLSearchParams(to.query.toString());
|
||||||
|
if (queryParams.has('error')) {
|
||||||
|
return navigateTo(authConfig.redirect.login)
|
||||||
|
}
|
||||||
|
|
||||||
if (params.has('access_token')) {
|
const hashParams = new URLSearchParams(to.hash.substring(1))
|
||||||
const token = params.get('access_token') as string;
|
|
||||||
const tokenType = params.get('token_type') as string;
|
|
||||||
const expires = params.get('expires_in') as string;
|
|
||||||
|
|
||||||
await setBearer(token, tokenType, parseInt(expires));
|
if (hashParams.has('access_token')) {
|
||||||
|
const token = hashParams.get('access_token') as string;
|
||||||
|
const tokenType = hashParams.get('token_type') as string;
|
||||||
|
const expires = hashParams.get('expires_in') as string;
|
||||||
|
|
||||||
|
await setBearerToken(token, tokenType, parseInt(expires));
|
||||||
return navigateTo(authConfig.redirect.home)
|
return navigateTo(authConfig.redirect.home)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user