Skip to content

Commit

Permalink
feat: add support for configuring basic_auth guard
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Apr 2, 2024
1 parent 5b5cd50 commit 97eb538
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/auth/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export async function presetAuth(
codemods: Codemods,
app: Application<any>,
options: {
guard: 'session' | 'access_tokens'
guard: 'session' | 'access_tokens' | 'basic_auth'
userProvider: 'lucid'
}
) {
Expand Down
30 changes: 30 additions & 0 deletions src/auth/stubs/config/basic_auth_with_lucid.stub
Original file line number Diff line number Diff line change
@@ -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<typeof authConfig> {}
}
declare module '@adonisjs/core/types' {
interface EventsList extends InferAuthEvents<Authenticators> {}
}
37 changes: 37 additions & 0 deletions src/auth/stubs/make/model/user_with_basic_auth.stub
Original file line number Diff line number Diff line change
@@ -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
}
31 changes: 31 additions & 0 deletions tests/auth/preset.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')`,
])
})
})

0 comments on commit 97eb538

Please sign in to comment.