feat: improve auth flow
This commit is contained in:
parent
deacf02a18
commit
3058fe2ea2
2 changed files with 14 additions and 7 deletions
|
|
@ -6,6 +6,7 @@ import { inject } from '@adonisjs/core'
|
|||
import app from '@adonisjs/core/services/app'
|
||||
import env from '#start/env'
|
||||
import User from '#models/user'
|
||||
import { DateTime } from 'luxon'
|
||||
|
||||
@inject()
|
||||
export default class AuthController {
|
||||
|
|
@ -89,9 +90,16 @@ export default class AuthController {
|
|||
}
|
||||
}
|
||||
|
||||
const lastYear = DateTime.now().minus({ years: 1 })
|
||||
return User.query()
|
||||
.select('firstName', 'lastName')
|
||||
.select('firstName', 'lastName', 'id')
|
||||
.where('className', className)
|
||||
.whereExists((query) => {
|
||||
query
|
||||
.from('colles')
|
||||
.whereRaw('colles.student_id = users.id')
|
||||
.where('colles.date', '>=', lastYear.toISODate())
|
||||
})
|
||||
.orderBy('lastName', 'asc')
|
||||
.then((users) => {
|
||||
return {
|
||||
|
|
@ -99,6 +107,7 @@ export default class AuthController {
|
|||
data: users.map((user) => ({
|
||||
value: `${user.firstName}::${user.lastName}`,
|
||||
label: user.fullName,
|
||||
userId: user.id,
|
||||
})),
|
||||
}
|
||||
})
|
||||
|
|
@ -106,12 +115,11 @@ export default class AuthController {
|
|||
|
||||
// POST /auth/register
|
||||
async register({ request, response, auth }: HttpContext) {
|
||||
const { name, className, token } = await request.validateUsing(registerValidator)
|
||||
const { userId, className, token } = await request.validateUsing(registerValidator)
|
||||
|
||||
// Validate token
|
||||
const { success, email } = this.authService.validateToken(token)
|
||||
const [firstName, lastName] = name.split('::')
|
||||
if (!success || !email || !firstName || !lastName) {
|
||||
if (!success || !email) {
|
||||
return response.badRequest({
|
||||
success: false,
|
||||
message: 'Votre lien de connexion est invalide ou a expiré.',
|
||||
|
|
@ -119,8 +127,7 @@ export default class AuthController {
|
|||
}
|
||||
|
||||
const user = await User.query()
|
||||
.where('firstName', firstName)
|
||||
.where('lastName', lastName)
|
||||
.where('id', userId)
|
||||
.where('className', className)
|
||||
.first()
|
||||
if (!user) {
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ export const verifyCodeValidator = vine.compile(
|
|||
|
||||
export const registerValidator = vine.compile(
|
||||
vine.object({
|
||||
name: vine.string().minLength(2).maxLength(50).trim(),
|
||||
userId: vine.number().positive(),
|
||||
className: vine.string().minLength(2).maxLength(50),
|
||||
token: vine.string(),
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue