streamlined auth-controller responses

This commit is contained in:
Sockenklaus
2021-10-25 15:57:53 +02:00
parent 42efa25185
commit 5c8c51dd9d
2 changed files with 10 additions and 26 deletions

View File

@@ -5,22 +5,11 @@ import Database from '@ioc:Adonis/Lucid/Database'
import { DateTime } from 'luxon'
type AuthSuccResult = {
notification: {
type: string,
text: string
}
user: string,
role: string,
token: string
}
type AuthErrResult = {
notification: {
text: string,
type: string
}
}
export default class AuthController {
@@ -34,10 +23,6 @@ export default class AuthController {
const token = await auth.use('api').attempt(username, password)
const result : AuthSuccResult = {
notification: {
type: 'success',
text: 'Login successful!'
},
user: auth.user?.username ?? '',
role: auth.user?.role ?? '',
token: token.token
@@ -46,22 +31,14 @@ export default class AuthController {
return response.ok(result)
} catch (error) {
const result : AuthErrResult = {
notification: {
type: 'danger',
text: error.message
}
}
return response.forbidden(result)
return response.forbidden(error.message)
}
}
public async logout({auth, response}: HttpContextContract) {
Logger.info("entering logout function")
public async logout({auth}: HttpContextContract) {
try {
await auth.use('api').revoke()
return response.ok('Logged out successfully')
return
}
catch(error) {
Logger.error(error.message)

View File

@@ -12,6 +12,13 @@ export default class UserSeeder extends BaseSeeder {
email: 'test@test.de',
isActive: true
})
await User.create({
username: 'user',
password: 'user',
role: 'employee',
email: 'user@test.de',
isActive: true
})
}
catch(error) {
Logger.error(error.message)