From 97eb5387d8ca497675294b0ab6e173366450b743 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Tue, 2 Apr 2024 13:43:05 +0530 Subject: [PATCH] feat: add support for configuring basic_auth guard --- src/auth/main.ts | 2 +- .../stubs/config/basic_auth_with_lucid.stub | 30 +++++++++++++++ .../make/model/user_with_basic_auth.stub | 37 +++++++++++++++++++ tests/auth/preset.spec.ts | 31 ++++++++++++++++ 4 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 src/auth/stubs/config/basic_auth_with_lucid.stub create mode 100644 src/auth/stubs/make/model/user_with_basic_auth.stub diff --git a/src/auth/main.ts b/src/auth/main.ts index 13110a2..caa37ff 100644 --- a/src/auth/main.ts +++ b/src/auth/main.ts @@ -21,7 +21,7 @@ export async function presetAuth( codemods: Codemods, app: Application, options: { - guard: 'session' | 'access_tokens' + guard: 'session' | 'access_tokens' | 'basic_auth' userProvider: 'lucid' } ) { diff --git a/src/auth/stubs/config/basic_auth_with_lucid.stub b/src/auth/stubs/config/basic_auth_with_lucid.stub new file mode 100644 index 0000000..57af4c7 --- /dev/null +++ b/src/auth/stubs/config/basic_auth_with_lucid.stub @@ -0,0 +1,30 @@ +{{{ + exports({ to: app.configPath('auth.ts') }) +}}} +import { defineConfig } from '@adonisjs/auth' +import { InferAuthEvents, Authenticators } from '@adonisjs/auth/types' +import { basicAuthGuard, basicAuthUserProvider } from '@adonisjs/auth/basic_auth' + +const authConfig = defineConfig({ + default: 'basicAuth', + guards: { + basicAuth: basicAuthGuard({ + provider: basicAuthUserProvider({ + model: () => import('#models/user') + }), + }), + }, +}) + +export default authConfig + +/** + * Inferring types from the configured auth + * guards. + */ +declare module '@adonisjs/auth/types' { + interface Authenticators extends InferAuthenticators {} +} +declare module '@adonisjs/core/types' { + interface EventsList extends InferAuthEvents {} +} diff --git a/src/auth/stubs/make/model/user_with_basic_auth.stub b/src/auth/stubs/make/model/user_with_basic_auth.stub new file mode 100644 index 0000000..4673cd5 --- /dev/null +++ b/src/auth/stubs/make/model/user_with_basic_auth.stub @@ -0,0 +1,37 @@ +{{#var modelName = generators.modelName(entity.name)}} +{{#var modelFileName = generators.modelFileName(entity.name)}} +{{{ + exports({ + to: app.modelsPath(entity.path, modelFileName) + }) +}}} +import { DateTime } from 'luxon' +import { withAuthFinder } from '@adonisjs/auth' +import hash from '@adonisjs/core/services/hash' +import { compose } from '@adonisjs/core/helpers' +import { BaseModel, column } from '@adonisjs/lucid/orm' + +const AuthFinder = withAuthFinder(() => hash.use('scrypt'), { + uids: ['email'], + passwordColumnName: 'password', +}) + +export default class {{ modelName }} extends compose(BaseModel, AuthFinder) { + @column({ isPrimary: true }) + declare id: number + + @column() + declare fullName: string | null + + @column() + declare email: string + + @column() + declare password: string + + @column.dateTime({ autoCreate: true }) + declare createdAt: DateTime + + @column.dateTime({ autoCreate: true, autoUpdate: true }) + declare updatedAt: DateTime | null +} diff --git a/tests/auth/preset.spec.ts b/tests/auth/preset.spec.ts index 2d59795..d1cec69 100644 --- a/tests/auth/preset.spec.ts +++ b/tests/auth/preset.spec.ts @@ -76,3 +76,34 @@ test.group('Preset | Auth | access tokens', (group) => { ]) }) }) + +test.group('Preset | Auth | basicAuth', (group) => { + group.each.disableTimeout() + + test('publish stubs and register provider and middleware', async ({ fs, assert }) => { + timekeeper.freeze() + + await createSetupFiles(fs) + await createKernelFile(fs) + + const app = await createApp(fs) + const logger = new Kernel(app).ui.logger + const codemods = await createCodeMods(fs, logger, app) + + await presetAuth(codemods, app, { guard: 'basic_auth', userProvider: 'lucid' }) + await assert.fileContains('adonisrc.ts', ['@adonisjs/auth/auth_provider']) + await assert.fileExists('config/auth.ts') + await assert.fileExists('app/middleware/auth_middleware.ts') + await assert.fileExists('app/models/user.ts') + await assert.fileNotExists('app/middleware/guest_middleware.ts') + await assert.fileNotExists( + `database/migrations/${new Date().getTime()}_create_access_tokens_table.ts` + ) + await assert.fileExists(`database/migrations/${new Date().getTime()}_create_users_table.ts`) + + await assert.fileContains('start/kernel.ts', [ + `() => import('@adonisjs/auth/initialize_auth_middleware')`, + `auth: () => import('#middleware/auth_middleware')`, + ]) + }) +})