Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/4sllan/nuxt-simple-auth

🔑 Auth Module Simple
https://github.com/4sllan/nuxt-simple-auth

auth fecth module nuxt nuxt-module nuxt3 pinia security ssr token

Last synced: about 2 months ago
JSON representation

🔑 Auth Module Simple

Awesome Lists containing this project

README

        




Auth Module Simple


Authentication support for Nuxt 3



Nuxt Simple Auth



[![Static Badge](https://img.shields.io/badge/NPM:nuxt-simple-auth?style=flat-square&colorA=18181B&colorB=28CF8D)](https://www.npmjs.com/package/nuxt-simple-auth)
[![Static Badge](https://img.shields.io/badge/GITHUB:nuxt-simple-auth?style=flat-square&colorA=18181B&colorB=28CF8D)](https://github.com/4slanK/nuxt-simple-auth)
[![npm version](https://img.shields.io/npm/v/nuxt-simple-auth/latest.svg?style=flat-square&colorA=18181B&colorB=28CF8D)](https://www.npmjs.com/package/nuxt-simple-auth)
![GitHub License][license]
[![npm downloads](https://img.shields.io/npm/dt/nuxt-simple-auth.svg?style=flat-square&colorA=18181B&colorB=28CF8D)](https://www.npmjs.com/package/nuxt-simple-auth)
[![Nuxt][nuxt-src]][nuxt-href]
[![Static Badge](https://img.shields.io/badge/-%E2%99%A5%20Sponsors-ec5cc6?style=flat-square)](https://github.com/sponsors/4sllan)

## Installation

> nuxt-simple-auth is a feature-rich open source authentication module for Nuxt3 applications.

## Quick Start

```sh
npm i nuxt-simple-auth pinia @pinia/nuxt
```

```sh
yarn add nuxt-simple-auth pinia @pinia/nuxt
```

## Setup

### Installation

Then, add nuxt-simple-auth to the modules section of nuxt.config.js:

### Config

***nuxt.config.js***

``` js
{
modules: [
'nuxt-simple-auth',
'@pinia/nuxt'
],

auth: {

}
},

```

### Strategies

``` js
strategies: {
local: {
redirect: {
logout: "/logout"
},
token: {
property: 'access_token',
},
user: {
property: 'profile',
},
endpoints: {
login: {url: '/oauth/token', method: 'post'},
user: {url: '/api/profile', method: 'get'},
"2fa": {url: '/api/token_2fa', method: 'post'},
logout: false,
},
},
}
```

### Cookie

**prefix** - Default token prefix used in constructing a key for token storage.


**options** - Additional cookie options, passed to
cookie



**path** - path where the cookie is visible. The default is '/'.


**expires** - can be used to specify the lifetime of the cookie in Number of Days or Specific Date. The default is
session only.


**maxAge** - Specifies the number (in seconds) that will be the Max-Age value (preferably expired)

**domain** - domain (and by extension subdomain(s)) where the cookie is visible. The default is domain and all
subdomains.


**secure** - defines whether the cookie requires a secure protocol (https). Default is false, should be set to true if
possible.

**Note:** By default, the prefix on localhost is set to "auth."


**__Secure- prefix:** Cookies with names starting with **__Secure-** (dash is part of the prefix) must be set with the
secure flag from a secure page (HTTPS).


**__Host- prefix:** Cookies with names starting with **__Host-** must be set with the secure flag, must be from a secure
page (HTTPS), must not have a domain specified (and therefore, are not sent to subdomains), and the path must be /.

``` js
cookie: {
options: {
httpOnly: true,
secure: true,
sameSite: 'Lax',
priority: 'high',
//maxAge: 24 * 60 * 60 * 1000,
},
prefix: '__Secure-auth.',
}
```

### 2fa

**Two-factor identification** The 2fa token will have all settings already defined in the cookie
default: false

``` js
"2fa": true,
```

### Pages

``` js
definePageMeta({
middleware: ['auth', '_2fa']
});
```

### runtimeConfig

**nuxt.config.js **

``` js
secret:{
strategyName:{
grant_type: 'password',
client_id: 0,
client_secret: '',
}
}

public: {
apiBase: '/api',
baseURL: process.env.baseURL,
}

```

### Methods

```shell
loginWith(strategyName, ...args)
```

Return: Promise

Set the current strategy as strategyName and attempt to log in. The usage may vary based on the current strategy.

``` js
const {$auth} = useNuxtApp()

$auth.loginWith('local', data)
.then(response => {

})
```

```shell
logout(strategyName)
```

Set the current strategy as strategyName and logout, ensuring the destruction of Pinia's cookies and state.

``` js
const {$auth} = useNuxtApp()

$auth.logout(strategyName)
```

```shell
_2fa(strategyName, ...args)
```

Return: Promise

Set the current strategy as strategyName and attempt to validate the code with a simplified two-factor authentication (
2FA) and the creation of cookies with HttpOnly. The utilization of these features varies based on the current strategy.

``` js
$auth._2fa('local', data).then(response => {

})
```

``` js
const {data, pending, error, refresh} = useFetch(url, {
$fetch: useRequestFetch(),
headers: $auth.$headers,
})
```

``` js
$auth.$headers.set('name', 'value ')
$auth.$headers.get('name', 'value ')
```

## ⚖️ License

Released under [MIT](/LICENSE) by [@4slan](https://github.com/4sllan).

[license]: https://img.shields.io/github/license/4sllan/nuxt-simple-auth?style=flat-square&colorA=18181B&colorB=28CF8D

[nuxt-src]: https://img.shields.io/badge/Nuxt-18181B?logo=nuxt.js

[nuxt-href]: https://nuxt.com