Model controllers added

This commit is contained in:
Kiss Dávid
2025-07-28 13:00:46 +02:00
parent 590adb271d
commit 465613f6a0
17 changed files with 470 additions and 100 deletions
+33 -1
View File
@@ -5,11 +5,19 @@ import logger from '@adonisjs/core/services/logger'
export default class AuthController {
/**
* Registers a user using the provided email, password, and full name.
*
* @bodyParam *email* - string | The email of the user.
* @bodyParam *password* - string | The password of the user.
* @bodyParam *fullName* - string | The full name of the user.
* @returns The user access token.
*/
async register({ request }: HttpContext) {
try {
const data = await request.validateUsing(registerValidator)
const user = await User.create(data)
logger.info('User registered: %s', user.fullName, user.email, request.ip())
logger.info('User registered: %s, %s, %s', user.fullName, user.email, request.ip())
return User.accessTokens.create(user)
}
catch(error) {
@@ -18,6 +26,13 @@ export default class AuthController {
}
}
/**
* Log in a user using the provided email and password.
*
* @bodyParam *email* - string | The email of the user.
* @bodyParam *password* - string | The password of the user.
* @returns The access token of the user.
*/
async login({ request }: HttpContext) {
try {
const { email, password } = await request.validateUsing(loginValidator)
@@ -31,6 +46,14 @@ export default class AuthController {
}
}
/**
* Deletes the user's access token and logs them out. If the user is not
* authenticated, returns an unauthorized message.
*/
async logout({ auth }: HttpContext) {
const user = auth.user!
logger.info('User logout: %s', auth.getUserOrFail().email)
@@ -44,6 +67,15 @@ export default class AuthController {
}
}
/**
* Retrieves the authenticated user's information if authenticated,
* otherwise deletes the access token and returns an unauthorized message.
*
* @param auth - The HTTP context's auth object.
* @returns An object containing the user data if authenticated,
* otherwise an unauthorized message.
*/
async me({ auth }: HttpContext) {
if (await auth.check()) {
return { user: auth.user }