Code Monkey home page Code Monkey logo

nuxt-basic-auth's Introduction

Basic Auth

npm version npm downloads License Nuxt

Nuxt 3 Module for Basic Authentication.

Features

  • ✅ Simple to use
  • ✅ Supports multiple users
  • ✅ Whitelist routes

Quick Setup

Install the module to your Nuxt application with one command:

npx nuxi module add @kgierke/nuxt-basic-auth

Configure the module in nuxt.config.ts

export default defineNuxtConfig({
  modules: ["@kgierke/nuxt-basic-auth"],

  basicAuth: {
    enabled: true,
    users: [
      {
        username: "admin",
        password: "admin",
      },
    ],
    // Optional: Delimiter for users string
    // usersDelimiter: ",",
    // Optional: Whitelist routes
    // allowedRoutes: ["/api/.*"],
  },
});

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

Options

Option Type Default Description
enabled boolean true Enables or disables Basic Auth.
users array [] Array of users. Each user must have a username and password property. Can also be formatted as string <username>:<password>,<username2>:<password2>
usersDelimiter string , Delimiter for users string.
allowedRoutes string[] [] Array of routes that are not protected by Basic Auth. Supports regex patterns.

All options can also be set through environment variables, using the NUXT_BASIC_AUTH_ prefix. For example, NUXT_BASIC_AUTH_ENABLED=true.

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

nuxt-basic-auth's People

Contributors

kgierke avatar danielroe avatar

Stargazers

Philip Akunyiba avatar ikuo saito avatar Aalivexy avatar silvesterwali avatar  avatar Thomas Bonnet avatar Ángel Aguero avatar Muhammad Ubaid Raza avatar Sandro Circi avatar Caleb Waldner avatar Becem avatar Olivier Mourlevat avatar Adam Whitlock avatar Lutte Pok avatar snowdream avatar  avatar

Watchers

 avatar

nuxt-basic-auth's Issues

Public folder still accessible

Hello, so far this module works great! I was wondering if it would be possible to also protect the contents of the “public” folder, as they are still accessible without logging in.

Prerenderer

First of all, thanks for the great work on this module. I'm bumping into an issue with building my Nuxt app for production. If basicAuth is enabled, prerendering won't work as the crawler will hit 401 Unauthorized responses. Is there a way we can either disable auth for the prerender part, or configure prerenderer to use the basic auth credentials while crawling?

Basic auth default user

Just to report a bug.

If you use this module and you define

  basicAuth: {
    enabled: true,
    users: [
      {
        username: "myUsername",
        password: "myPw",
      },
    ],
    allowedRoutes: ["/api/.*"],
  },

It will just append to the users list and still have default value that u have defined as admin:admin

This should not be like that, It should replace your default user array because otherwise, everyone can still access the website by just writing admin:admin instead of this custom-defined password

Thanks

Some letters not allowed in password

This wook a while to figure out, but it seems like this module will refuse to authenticate users that have some common latin letters in the password, e.g. å, ö, ñ or ı, and it will also fail for any(?) non latin character (e.g. 暗号). Could it be that only ascii characters work, for some reason?

When i try to build with this package get error

When i add this package to my project its working on developement but when try to build project i get those errors
while building
image

these are what i just do only adding these to nuxt.config.ts nothing more when delete these it is working

  modules: [
    '@kgierke/nuxt-basic-auth',
  ],
 basicAuth: {
    enabled: true,
    users: [
      {
        username: 'admin',
        password: 'admin',
      },
    ],
  },

Set Username and Password at Run time

Hi,

Is it possible to define username and password properties at runtime with env variables?

Let me explain furthermore. Here is what I'm trying to achieve:

// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
  runtimeConfig: {
    basicAuthUsername: "hardcoded-username", // Will be replace by NUXT_BASIC_AUTH_USERNAME at runtime
    basicAuthPassword: "hardcoded-password", // Will be replace by NUXT_BASIC_AUTH_PASSWORD at runtime
  },

  basicAuth: {
    users: [
      {
        username: useRuntimeConfig().basicAuthUsername,
        password: useRuntimeConfig().basicAuthPassword,
      },
    ],
  },

});

However, useRuntimeConfig is not defined inside the nuxt.config.ts file.
If I switch to process.env. NUXT_BASIC_AUTH_XXX it is not replaced at run time and keep the value of build time.

Do you know any solution / workaround to this issue?

Thanks for your help !

Thib

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.