🔧 Configure addons

This commit is contained in:
Nathan 2025-05-18 13:05:06 +02:00
parent 9b616ce4e3
commit e4a802d5f2
12 changed files with 87 additions and 49 deletions

View file

@ -1,26 +1,27 @@
import { DateTime } from 'luxon' import { DateTime } from 'luxon'
import hash from '@adonisjs/core/services/hash'
import { compose } from '@adonisjs/core/helpers'
import { BaseModel, column } from '@adonisjs/lucid/orm' import { BaseModel, column } from '@adonisjs/lucid/orm'
import { withAuthFinder } from '@adonisjs/auth/mixins/lucid' import { DbRememberMeTokensProvider } from '@adonisjs/auth/session'
const AuthFinder = withAuthFinder(() => hash.use('scrypt'), { export default class User extends BaseModel {
uids: ['email'], static rememberMeTokens = DbRememberMeTokensProvider.forModel(User)
passwordColumnName: 'password',
})
export default class User extends compose(BaseModel, AuthFinder) {
@column({ isPrimary: true }) @column({ isPrimary: true })
declare id: number declare id: number
@column({ serializeAs: 'class', serialize: serializeClass })
declare classId: number
@column() @column()
declare fullName: string | null declare firstName: string
@column()
declare lastName: string
@column() @column()
declare email: string declare email: string
@column({ serializeAs: null }) @column()
declare password: string declare avatar: string
@column.dateTime({ autoCreate: true }) @column.dateTime({ autoCreate: true })
declare createdAt: DateTime declare createdAt: DateTime
@ -28,3 +29,8 @@ export default class User extends compose(BaseModel, AuthFinder) {
@column.dateTime({ autoCreate: true, autoUpdate: true }) @column.dateTime({ autoCreate: true, autoUpdate: true })
declare updatedAt: DateTime | null declare updatedAt: DateTime | null
} }
// TODO
function serializeClass(classId: number): string {
return 'class_' + classId
}

View file

@ -6,7 +6,8 @@ const authConfig = defineConfig({
default: 'web', default: 'web',
guards: { guards: {
web: sessionGuard({ web: sessionGuard({
useRememberMeTokens: false, useRememberMeTokens: true,
rememberMeTokensAge: '2 years',
provider: sessionUserProvider({ provider: sessionUserProvider({
model: () => import('#models/user') model: () => import('#models/user')
}), }),

View file

@ -1,4 +1,3 @@
import env from '#start/env'
import { defineConfig, store, drivers } from '@adonisjs/cache' import { defineConfig, store, drivers } from '@adonisjs/cache'
const cacheConfig = defineConfig({ const cacheConfig = defineConfig({
@ -9,12 +8,17 @@ const cacheConfig = defineConfig({
default: store() default: store()
.useL1Layer(drivers.memory()) .useL1Layer(drivers.memory())
.useL2Layer(
.useL2Layer(drivers.redis({ drivers.redis({
connectionName: 'main', connectionName: 'main',
})) })
)
} .useBus(
drivers.redisBus({
connectionName: 'main',
})
),
},
}) })
export default cacheConfig export default cacheConfig

View file

@ -8,6 +8,7 @@ import { defineConfig } from '@adonisjs/cors'
*/ */
const corsConfig = defineConfig({ const corsConfig = defineConfig({
enabled: true, enabled: true,
// TODO: Only same domain
origin: true, origin: true,
methods: ['GET', 'HEAD', 'POST', 'PUT', 'DELETE'], methods: ['GET', 'HEAD', 'POST', 'PUT', 'DELETE'],
headers: true, headers: true,

View file

@ -14,7 +14,7 @@ const driveConfig = defineConfig({
location: app.makePath('storage'), location: app.makePath('storage'),
serveFiles: true, serveFiles: true,
routeBasePath: '/uploads', routeBasePath: '/uploads',
visibility: 'public', visibility: 'private'
}), }),
}, },
}) })

View file

@ -4,7 +4,6 @@ import { defineConfig, stores } from '@adonisjs/limiter'
const limiterConfig = defineConfig({ const limiterConfig = defineConfig({
default: env.get('LIMITER_STORE'), default: env.get('LIMITER_STORE'),
stores: { stores: {
/** /**
* Redis store to save rate limiting data inside a * Redis store to save rate limiting data inside a
* redis database. * redis database.
@ -14,12 +13,11 @@ const limiterConfig = defineConfig({
*/ */
redis: stores.redis({}), redis: stores.redis({}),
/** /**
* Memory store could be used during * Memory store could be used during
* testing * testing
*/ */
memory: stores.memory({}) memory: stores.memory({}),
}, },
}) })

View file

@ -4,18 +4,17 @@ import { defineConfig, transports } from '@adonisjs/mail'
const mailConfig = defineConfig({ const mailConfig = defineConfig({
default: 'mailgun', default: 'mailgun',
/** /**
* The mailers object can be used to configure multiple mailers * The mailers object can be used to configure multiple mailers
* each using a different transport or same transport with different * each using a different transport or same transport with different
* options. * options.
*/ */
mailers: { mailers: {
mailgun: transports.mailgun({ mailgun: transports.mailgun({
key: env.get('MAILGUN_API_KEY'), key: env.get('MAILGUN_API_KEY')!,
baseUrl: 'https://api.mailgun.net/v3', baseUrl: 'https://api.mailgun.net/v3',
domain: env.get('MAILGUN_DOMAIN'), domain: env.get('MAILGUN_DOMAIN')!,
}), }),
}, },
}) })

View file

@ -6,10 +6,10 @@ export default class extends BaseSchema {
async up() { async up() {
this.schema.createTable(this.tableName, (table) => { this.schema.createTable(this.tableName, (table) => {
table.increments('id').notNullable() table.increments('id').notNullable()
table.string('full_name').nullable() table.integer('class_id').notNullable()
table.string('first_name', 80).notNullable()
table.string('last_name', 80).notNullable()
table.string('email', 254).notNullable().unique() table.string('email', 254).notNullable().unique()
table.string('password').notNullable()
table.timestamp('created_at').notNullable() table.timestamp('created_at').notNullable()
table.timestamp('updated_at').nullable() table.timestamp('updated_at').nullable()
}) })

View file

@ -0,0 +1,26 @@
import { BaseSchema } from '@adonisjs/lucid/schema'
export default class extends BaseSchema {
protected tableName = 'remember_me_tokens'
async up() {
this.schema.createTable(this.tableName, (table) => {
table.increments()
table
.integer('tokenable_id')
.notNullable()
.unsigned()
.references('id')
.inTable('users')
.onDelete('CASCADE')
table.string('hash').notNullable().unique()
table.timestamp('created_at').notNullable()
table.timestamp('updated_at').notNullable()
table.timestamp('expires_at').notNullable()
})
}
async down() {
this.schema.dropTable(this.tableName)
}
}

View file

@ -48,12 +48,15 @@ export default await Env.create(new URL('../', import.meta.url), {
| Variables for configuring the mail package | Variables for configuring the mail package
|---------------------------------------------------------- |----------------------------------------------------------
*/ */
SMTP_HOST: Env.schema.string(), // MAILGUN_API_KEY: Env.schema.string(),
SMTP_PORT: Env.schema.string(), // MAILGUN_DOMAIN: Env.schema.string()
/*
|----------------------------------------------------------
| Variables for configuring the redis package
|----------------------------------------------------------
*/
REDIS_HOST: Env.schema.string({ format: 'host' }), REDIS_HOST: Env.schema.string({ format: 'host' }),
REDIS_PORT: Env.schema.number(), REDIS_PORT: Env.schema.number(),
REDIS_PASSWORD: Env.schema.string.optional(), REDIS_PASSWORD: Env.schema.string.optional(),
MAILGUN_API_KEY: Env.schema.string(),
MAILGUN_DOMAIN: Env.schema.string()
}) })