Gean-Lima / simple-auth

A simple authentication module using token

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Simple Auth

npm version npm downloads License Nuxt

A simple authentication module using token

Designed to be used with tymondesigns/jwt-auth

Features

  • ⛰  Simple integration
  • 🔐  Middleware
  • 🔗  Axios and Pinia

Quick Setup

Install the module to your Nuxt application with one command:

npx nuxi module add @geanlima/simpleauth

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

Use

export default defineNuxtConfig({
  modules: ['@geanlima/simpleauth'],
  simpleAuth: {
    loginPage: '/', // Authentication page
    homePage: '/home', // Authenticated User Page
    baseUrl: 'http://localhost:8000/api', // API base URL, if not passed it will only use the URL field
    login: {
      url: '/login',
      method: 'POST',
      token: {
        field: 'access_token',
        field_expires: 'expires_in', // Time in seconds that the token is valid
      },
    },
    logout: {
      url: '/auth/me',
      method: 'POST',
    },
    refresh: {
      url: '/auth/refresh',
      method: 'POST',
      token: {
        field: 'access_token',
        field_expires: 'expires_in',
      },
    },
    me: {
      url: '/auth/me',
      method: 'POST',
    },
  }
})

Examples

const auth = useAuth()

definePageMeta({
  middleware: ['auth'] // or guest
})

auth.login({
  email: 'test@test',
  password: 'password'
})

auth.logout();

auth.user
auth.isLogged
auth.token
auth.expires

Contribution

Local development
# Install dependencies
npm install

# Generate type stubs
npm run dev:prepare

# Develop with the playground
npm run dev

# Build the playground
npm run dev:build

# Run ESLint
npm run lint

# Run Vitest
npm run test
npm run test:watch

# Release new version
npm run release

About

A simple authentication module using token


Languages

Language:TypeScript 83.5%Language:Vue 12.5%Language:JavaScript 4.0%