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