28 lines
817 B
TypeScript
28 lines
817 B
TypeScript
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
|
import Logger from '@ioc:Adonis/Core/Logger'
|
|
|
|
export default class AuthController {
|
|
|
|
public async login({auth, request, response}: HttpContextContract) {
|
|
const username = request.body().username
|
|
const password = request.body().password
|
|
|
|
try {
|
|
await auth.attempt(username, password)
|
|
return response.ok({
|
|
Message: 'Login successful!',
|
|
user: auth.user?.username,
|
|
role: auth.user?.role
|
|
})
|
|
} catch (error) {
|
|
return response.forbidden('Unauthorized')
|
|
}
|
|
}
|
|
|
|
public async logout({auth, response}: HttpContextContract) {
|
|
await auth.logout()
|
|
|
|
return response.ok('Logged out successfully')
|
|
}
|
|
}
|