Skip to content

Added session based authentication#15

Open
ScribeSavant wants to merge 2 commits intochirgjin:mainfrom
ScribeSavant:main
Open

Added session based authentication#15
ScribeSavant wants to merge 2 commits intochirgjin:mainfrom
ScribeSavant:main

Conversation

@ScribeSavant
Copy link
Copy Markdown

@ScribeSavant ScribeSavant commented Oct 24, 2023

Authentication example with User model and isAdmin field

// User Model

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

  @column()
  public email: string

  @column({ serializeAs: null })
  public password: string

  @column()
  public rememberMeToken: string | null

  @column()
  public isAdmin: boolean

  @column.dateTime({ autoCreate: true })
  public createdAt: DateTime

  @column.dateTime({ autoCreate: true, autoUpdate: true })
  public updatedAt: DateTime

  @beforeSave()
  public static async hashPassword (user: User) {
    if (user.$dirty.password) {
      user.password = await Hash.make(user.password)
    }
  }
}

// Config

plugin: {
        /**
         * Whether to enable plugin or not
         */
        enabled: true,
        /**
         * Base route on which your admin panel resides.
         */
        routePrefix: '/admin',
        /**
         * Middlewares which are applied on all the routes of admin panel
         */
        middlewares: [],
        /**
         * Authentication options for the admin panel.
         */
        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: 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
            }

        }

    },

#Screenshot
image
image

@chirgjin
Copy link
Copy Markdown
Owner

Hey!
Thanks for contributing to this package.
Can't we use adonis' built-in auth package for this?

@ScribeSavant
Copy link
Copy Markdown
Author

Actually, I wanted to add something like this, but there was no time, I can add a new commit soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants