feat: add BJRepas credentials
This commit is contained in:
parent
90722673f8
commit
040ae43652
8 changed files with 110 additions and 26 deletions
|
|
@ -1,5 +1,10 @@
|
||||||
import type { HttpContext } from '@adonisjs/core/http'
|
import type { HttpContext } from '@adonisjs/core/http'
|
||||||
import { registerValidator, requestLoginValidator, verifyCodeValidator } from '#validators/auth'
|
import {
|
||||||
|
credentialsValidator,
|
||||||
|
registerValidator,
|
||||||
|
requestLoginValidator,
|
||||||
|
verifyCodeValidator,
|
||||||
|
} from '#validators/auth'
|
||||||
import mail from '@adonisjs/mail/services/main'
|
import mail from '@adonisjs/mail/services/main'
|
||||||
import { AuthService } from '#services/auth_service'
|
import { AuthService } from '#services/auth_service'
|
||||||
import { inject } from '@adonisjs/core'
|
import { inject } from '@adonisjs/core'
|
||||||
|
|
@ -7,6 +12,7 @@ import app from '@adonisjs/core/services/app'
|
||||||
import env from '#start/env'
|
import env from '#start/env'
|
||||||
import User from '#models/user'
|
import User from '#models/user'
|
||||||
import { DateTime } from 'luxon'
|
import { DateTime } from 'luxon'
|
||||||
|
import redis from '@adonisjs/redis/services/main'
|
||||||
|
|
||||||
@inject()
|
@inject()
|
||||||
export default class AuthController {
|
export default class AuthController {
|
||||||
|
|
@ -126,10 +132,7 @@ export default class AuthController {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const user = await User.query()
|
const user = await User.query().where('id', userId).where('className', className).first()
|
||||||
.where('id', userId)
|
|
||||||
.where('className', className)
|
|
||||||
.first()
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
return response.badRequest({
|
return response.badRequest({
|
||||||
success: false,
|
success: false,
|
||||||
|
|
@ -163,6 +166,29 @@ export default class AuthController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// POST /auth/test
|
||||||
|
public async testCredentials({ request, auth }: HttpContext) {
|
||||||
|
const { username, password } = await request.validateUsing(credentialsValidator)
|
||||||
|
await redis.publish(
|
||||||
|
'jobs_queue',
|
||||||
|
JSON.stringify({
|
||||||
|
type: 5, // Test credentials
|
||||||
|
bj_username: username,
|
||||||
|
bj_password: password,
|
||||||
|
user_id: auth.user!.id,
|
||||||
|
class_name: env.get('DEFAULT_CLASS_NAME'),
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
return { success: true, message: 'Testing credentials...' }
|
||||||
|
}
|
||||||
|
|
||||||
|
// GET /auth/status
|
||||||
|
public async status({ auth }: HttpContext) {
|
||||||
|
const redisKey = `auth_success_${auth.user!.id}`
|
||||||
|
return { success: true, data: { authenticated: await redis.get(redisKey) } }
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Magic link login
|
// TODO: Magic link login
|
||||||
// magicLink({ }: HttpContext) {
|
// magicLink({ }: HttpContext) {
|
||||||
// // Validate signed url (adonis)
|
// // Validate signed url (adonis)
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import Meal from '#models/meal'
|
||||||
import MealRegistration from '#models/meal_registration'
|
import MealRegistration from '#models/meal_registration'
|
||||||
import env from '#start/env'
|
import env from '#start/env'
|
||||||
import { updateMealsRegistrationsValidator } from '#validators/meal'
|
import { updateMealsRegistrationsValidator } from '#validators/meal'
|
||||||
|
|
@ -43,7 +44,7 @@ export default class InternalsController {
|
||||||
// Remove all existing registrations for the user
|
// Remove all existing registrations for the user
|
||||||
await MealRegistration.query().where('user_id', userId).delete()
|
await MealRegistration.query().where('user_id', userId).delete()
|
||||||
for (const mealData of meals) {
|
for (const mealData of meals) {
|
||||||
const meal = await MealRegistration.query()
|
const meal = await Meal.query()
|
||||||
.where({
|
.where({
|
||||||
date: mealData.date,
|
date: mealData.date,
|
||||||
type: mealData.meal_type === 'lunch' ? 0 : 1,
|
type: mealData.meal_type === 'lunch' ? 0 : 1,
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
import Meal from '#models/meal'
|
import Meal from '#models/meal'
|
||||||
|
import MealRegistration from '#models/meal_registration'
|
||||||
|
import { credentialsValidator } from '#validators/auth'
|
||||||
import { createMealValidator } from '#validators/meal'
|
import { createMealValidator } from '#validators/meal'
|
||||||
import type { HttpContext } from '@adonisjs/core/http'
|
import type { HttpContext } from '@adonisjs/core/http'
|
||||||
import redis from '@adonisjs/redis/services/main'
|
import redis from '@adonisjs/redis/services/main'
|
||||||
|
|
@ -53,7 +55,7 @@ export default class MealsController {
|
||||||
|
|
||||||
// Create the new meal
|
// Create the new meal
|
||||||
const meal = await Meal.create({
|
const meal = await Meal.create({
|
||||||
date: DateTime.fromJSDate(date),
|
date,
|
||||||
type: mealType,
|
type: mealType,
|
||||||
})
|
})
|
||||||
await meal.related('courses').createMany(courses)
|
await meal.related('courses').createMany(courses)
|
||||||
|
|
@ -61,13 +63,39 @@ export default class MealsController {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET /meals
|
// GET /meals
|
||||||
public async index({}: HttpContext) {
|
public async index({ auth }: HttpContext) {
|
||||||
// TODO: Check if the user is registered for each meal
|
const meals = await Meal.query().orderBy('date', 'asc').preload('courses')
|
||||||
return Meal.query().orderBy('date', 'asc').preload('courses')
|
const data = meals.map(async (meal) => {
|
||||||
|
if (meal.submittable) {
|
||||||
|
const isRegistered = await MealRegistration.query()
|
||||||
|
.where('meal_id', meal.id)
|
||||||
|
.where('user_id', auth.user!.id)
|
||||||
|
.first()
|
||||||
|
// Remove temporary registrations that are older than 5 minutes
|
||||||
|
if (isRegistered?.temporary) {
|
||||||
|
const oneHourAgo = DateTime.now().minus({ minutes: 5 })
|
||||||
|
if (isRegistered.createdAt < oneHourAgo) {
|
||||||
|
await isRegistered.delete()
|
||||||
|
return meal.serialize()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
...meal.serialize(),
|
||||||
|
isRegistered: !!isRegistered,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return meal.serialize()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return Promise.all(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
// POST /meals/:id
|
// POST /meals/:id
|
||||||
public async registerForMeal({ params, auth, response }: HttpContext) {
|
// TODO: Unregister from meal
|
||||||
|
public async registerForMeal({ params, auth, response, request }: HttpContext) {
|
||||||
|
const { username, password } = await request.validateUsing(credentialsValidator)
|
||||||
|
|
||||||
const meal = await Meal.find(params.id)
|
const meal = await Meal.find(params.id)
|
||||||
if (!meal) {
|
if (!meal) {
|
||||||
return response.notFound({ message: 'Meal not found' })
|
return response.notFound({ message: 'Meal not found' })
|
||||||
|
|
@ -76,18 +104,28 @@ export default class MealsController {
|
||||||
return response.badRequest({ message: 'Meal is not submittable' })
|
return response.badRequest({ message: 'Meal is not submittable' })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create a bullshit registration
|
||||||
|
// (to confirm to the user that the registration is in progress...)
|
||||||
|
// that will eventually be deleted if the registration fails (worker callback)
|
||||||
|
await MealRegistration.firstOrCreate({
|
||||||
|
mealId: meal.id,
|
||||||
|
userId: auth.user!.id,
|
||||||
|
temporary: true,
|
||||||
|
})
|
||||||
|
|
||||||
// Register the user for the meal
|
// Register the user for the meal
|
||||||
await redis.publish(
|
await redis.publish(
|
||||||
'jobs_queue',
|
'jobs_queue',
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
type: 4, // Submit meals
|
type: 4, // Submit meals
|
||||||
meal: {
|
meal: {
|
||||||
date: meal.date.toFormat('yyyy-MM-dd'),
|
date: DateTime.fromJSDate(meal.date).toISODate(),
|
||||||
meal_type: meal.type === 0 ? 'lunch' : 'dinner',
|
meal_type: meal.type === 0 ? 'lunch' : 'dinner',
|
||||||
},
|
},
|
||||||
class_name: auth.user!.className,
|
class_name: auth.user!.className,
|
||||||
user_id: auth.user!.id,
|
user_id: auth.user!.id,
|
||||||
// TODO: add user credentials
|
bj_username: username,
|
||||||
|
bj_password: password,
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
import { DateTime } from 'luxon'
|
|
||||||
import { BaseModel, column, computed, hasMany } from '@adonisjs/lucid/orm'
|
import { BaseModel, column, computed, hasMany } from '@adonisjs/lucid/orm'
|
||||||
import Course from './course.js'
|
import Course from './course.js'
|
||||||
import type { HasMany } from '@adonisjs/lucid/types/relations'
|
import type { HasMany } from '@adonisjs/lucid/types/relations'
|
||||||
|
|
@ -8,7 +7,7 @@ export default class Meal extends BaseModel {
|
||||||
declare id: number
|
declare id: number
|
||||||
|
|
||||||
@column()
|
@column()
|
||||||
declare date: DateTime
|
declare date: Date
|
||||||
|
|
||||||
@column()
|
@column()
|
||||||
declare type: 0 | 1 // 0 = lunch, 1 = dinner
|
declare type: 0 | 1 // 0 = lunch, 1 = dinner
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import { BaseModel, column } from '@adonisjs/lucid/orm'
|
import { BaseModel, column } from '@adonisjs/lucid/orm'
|
||||||
|
import { DateTime } from 'luxon'
|
||||||
|
|
||||||
export default class MealRegistration extends BaseModel {
|
export default class MealRegistration extends BaseModel {
|
||||||
@column({ isPrimary: true })
|
@column({ isPrimary: true })
|
||||||
|
|
@ -9,4 +10,10 @@ export default class MealRegistration extends BaseModel {
|
||||||
|
|
||||||
@column()
|
@column()
|
||||||
declare userId: number
|
declare userId: number
|
||||||
|
|
||||||
|
@column()
|
||||||
|
declare temporary: boolean
|
||||||
|
|
||||||
|
@column.dateTime({ autoCreate: true })
|
||||||
|
declare createdAt: DateTime
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,13 @@ export const registerValidator = vine.compile(
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
|
export const credentialsValidator = vine.compile(
|
||||||
|
vine.object({
|
||||||
|
username: vine.string().minLength(1).maxLength(255),
|
||||||
|
password: vine.string().minLength(1).maxLength(255),
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
// TODO: Magic link login
|
// TODO: Magic link login
|
||||||
// export const magicLinkValidator = vine.compile(
|
// export const magicLinkValidator = vine.compile(
|
||||||
// vine.object({
|
// vine.object({
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,15 @@
|
||||||
import { BaseSchema } from '@adonisjs/lucid/schema'
|
import { BaseSchema } from '@adonisjs/lucid/schema'
|
||||||
|
|
||||||
export default class extends BaseSchema {
|
export default class extends BaseSchema {
|
||||||
protected tableName = 'meals_registrations'
|
protected tableName = 'meal_registrations'
|
||||||
|
|
||||||
async up() {
|
async up() {
|
||||||
this.schema.createTable(this.tableName, (table) => {
|
this.schema.createTable(this.tableName, (table) => {
|
||||||
table.increments('id')
|
table.increments('id')
|
||||||
table.integer('meal_id').unsigned().references('id').inTable('meals').onDelete('CASCADE')
|
table.integer('meal_id').unsigned().references('id').inTable('meals').onDelete('CASCADE')
|
||||||
table.integer('user_id').unsigned().references('id').inTable('users').onDelete('CASCADE')
|
table.integer('user_id').unsigned().references('id').inTable('users').onDelete('CASCADE')
|
||||||
|
table.boolean('temporary').defaultTo(false)
|
||||||
|
table.timestamp('created_at').notNullable()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,16 +18,20 @@ import { middleware } from './kernel.js'
|
||||||
|
|
||||||
const AuthController = () => import('#controllers/auth_controller')
|
const AuthController = () => import('#controllers/auth_controller')
|
||||||
|
|
||||||
router.group(() => {
|
router
|
||||||
router.post('/auth/request', [AuthController, 'requestLogin']).use(authThrottle)
|
.group(() => {
|
||||||
router.post('/auth/verify', [AuthController, 'verifyCode']).use(throttle)
|
router.post('/request', [AuthController, 'requestLogin']).use(authThrottle)
|
||||||
router.post('/auth/register', [AuthController, 'register']).use(throttle)
|
router.post('/verify', [AuthController, 'verifyCode']).use(throttle)
|
||||||
router.post('/auth/logout', [AuthController, 'logout'])
|
router.post('/register', [AuthController, 'register']).use(throttle)
|
||||||
router.get('/auth/autocomplete', [AuthController, 'listNames']).use(throttle)
|
router.post('/logout', [AuthController, 'logout'])
|
||||||
// TODO: Magic link login
|
router.get('/autocomplete', [AuthController, 'listNames']).use(throttle)
|
||||||
// router.get('/auth/magic-link', 'AuthController.magicLink').use(throttle)
|
router.post('/test', [AuthController, 'testCredentials']).use(throttle).use(middleware.auth())
|
||||||
// router.get('/auth/listen', 'AuthController.listen')
|
router.get('/status', [AuthController, 'status']).use(middleware.auth())
|
||||||
})
|
// TODO: Magic link login
|
||||||
|
// router.get('/auth/magic-link', 'AuthController.magicLink').use(throttle)
|
||||||
|
// router.get('/auth/listen', 'AuthController.listen')
|
||||||
|
})
|
||||||
|
.prefix('/auth')
|
||||||
|
|
||||||
const UserController = () => import('#controllers/user_controller')
|
const UserController = () => import('#controllers/user_controller')
|
||||||
router.get('/users/@me', [UserController, 'me']).use(middleware.auth())
|
router.get('/users/@me', [UserController, 'me']).use(middleware.auth())
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue