Coder Social home page Coder Social logo

nuxt-simple-auth's Introduction

Auth Module Simple

Authentication support for Nuxt 3


Nuxt Simple Auth


Static Badge Static Badge npm version GitHub License npm downloads Nuxt Static Badge

Installation

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

Quick Start

npm i nuxt-simple-auth pinia @pinia/nuxt
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

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

Strategies

 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 /.

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

"2fa":  true,

Pages

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

runtimeConfig

nuxt.config.js

 grant_type: 'password',
 client_id: 0,
 client_secret: '',
        
        
 public: {
     apiBase: '/api',
     siteURL: 'http://localhost:3000/' || URL,
     baseURL: process.env.baseURL,
 }
        

Methods

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.

const {$auth} = useNuxtApp()

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

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

const {$auth} = useNuxtApp()

$auth.logout(strategyName)
_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.

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

})
 const {data, pending, error, refresh} = useFetch(url, {
    $fetch: useRequestFetch(),
    headers: $auth.$headers,
  })
 $auth.$headers.set('name', 'value ')
 $auth.$headers.get('name', 'value ')

⚖️ License

Released under MIT by @4slan.

nuxt-simple-auth's People

Contributors

4sllan avatar

Stargazers

 avatar

Watchers

 avatar

nuxt-simple-auth's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.