npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

adminjs-adonis-auth

v1.2.1

Published

AdonisJS plugin & adapter for AdminJS

Downloads

6

Readme

I wil delete this when real dev merge this commit

The only difference between this and adminjs-adonis is authentication

Admin Js Adonis

Adapter & Plugin package to use AdminJS with AdonisJS.

Getting Started

Installation

# using npm:

npm install --save adminjs-adonis-auth adminjs@^6.0.0

# or using yarn:
yarn add adminjs-adonis-auth adminjs@^6.0.0

After this, run:

node ace configure adminjs-adonis-auth

Configuration

The configuration for this package resides in config/adminjs.ts. Checkout templates/config.txt for configuration options.

Model Customization

This package aims to auto-detect correct types of most of model columns but there are still some cases where it fails (for example: Enums, Attachments, nullable types etc) due to limitations of reflect-metadata package.

For this purpose, there is a @adminColumn decorator which you can use to inform the adapter how exactly you want a particular column to be displayed (or not displayed at all).

// User.ts
import { BaseModel, column } from '@ioc:Adonis/Lucid/Orm'
import { adminColumn } from '@ioc:Adonis/Addons/AdminJS'

export enum UserType {
    STUDENT = 1,
    TEACHER = 2,
}

export class User extends BaseModel {
    @column({ isPrimary: true })
    public id: number

    @column()
    public username: string

    @column()
    @adminColumn({
        // password won't be visible on the list or show page
        visible: false
    })
    public password: string

    @column()
    @adminColumn({
        enum: UserType 
        // type will now be rendered as a select box
        // and will display the choices as text rather than numbers
    })
    public type: UserType

    @column()
    @adminColumn({
        // By default, `number | null` type is parsed as string
        type: "number",
        // By default, every field is required except the primary key
        optional: true,
    })
    public teachingNumber: number | null
}

For full options provided by adminColumn decorator, visit here

Hooks

This package also provides hooks for lifecycle management. These hooks are:

  • beforeCreate
  • beforeUpdate
  • beforeDelete
  • beforeFind
  • beforeFetch
  • afterCreate
  • afterUpdate
  • afterDelete
  • afterFind
  • afterFetch

They work the same as AdonisJS' hooks and when these hooks are called, corresponding AdonisJS hooks are also executed. For example: when user is creating a new object, then the order of hooks is:

  1. beforeCreate (of admin)
  2. beforeCreate (of AdonisJS)
  3. beforeSave (of AdonisJS)
  4. afterCreate (of AdonisJS)
  5. afterSave (of AdonisJS)
  6. afterCreate (of admin)

Note: There is no beforeSave or afterSave hook in this package

Example:

// User.ts
import { beforeCreate, beforeUpdate } from '@ioc:Adonis/Addons/AdminJS'
import Hash from '@ioc:Adonis/Core/Hash'
import { BaseModel, column } from '@ioc:Adonis/Lucid/Orm'

export class User extends BaseModel {
    @column({ isPrimary: true })
    public id: number

    @column()
    public username: string

    @column()
    public password: string

    @beforeCreate()
    @beforeUpdate()
    public static async setPasswordIfDirty(instance: User) {
        if (instance.$dirty.password) {
            instance.password = await Hash.make(instance.password)
        }
    }
}

Authentication

Authentication comes enabled by default

You can change the auth method on config

Model based authentication

authenticate: async (email, password) => {
    const {default:User} = await import('App/Models/User')
    const {default:Hash} = await import("@ioc:Adonis/Core/Hash")
    const user = await User.findBy("email", email)

    if (!user){
        return null
    }
    const isPasswordOk = await Hash.verify(user.password, password)
    if (!isPasswordOk){
        return null
    }
    if (!user.isAdmin){
        return null
    }
    return user
}

Env based authentication

authenticate: (email, password) => {
    if (
        email == Env.get("ADMIN_EMAIL") 
        && 
        password == Env.get("ADMIN_PASSWORD")
    ){
        return {email, password}
    }
    return null
}

Other config variables

auth: {
    /**
     * Authentication enabled/disabled flag.
     * When set to true, the authentication is enabled. When set to false, it's disabled.
    */
    enabled: true,

    /**
     * Maximum number of login retries allowed.
     * The user is locked out after exceeding this limit.
     */
    maxRetries: 5,

    /**
     * Duration (in seconds) for which a user is locked out after exceeding the max retries.
     */
    duration: 60,

    /**
     * Optional login path for authentication.
     * If not provided, a default path is used.
     */
    loginPath: "/admin/login",

    /**
     * Optional logout path for authentication.
     * If not provided, a default path is used.
     */
    logoutPath: "/admin/logout",

    /**
     * Function for authenticating a user.
     * This function takes an email and password as parameters and returns
     * a user object if authentication is successful or null if it fails.
     *
     * @param email - The user's email address for authentication.
     * @param password - The user's password for authentication.
     * @returns A user object if authentication is successful, or null if it fails.
     */
    authenticate: (email, password) => {
        if (email == "[email protected]" && password == "admin12345") {
            return { email, password }
        }
        return null
    }

}