Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added session based authentication #15

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ScribeSavant
Copy link

@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
Owner

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

@ScribeSavant
Copy link
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