streamlined auth-controller responses
This commit is contained in:
@@ -5,22 +5,11 @@ import Database from '@ioc:Adonis/Lucid/Database'
|
|||||||
import { DateTime } from 'luxon'
|
import { DateTime } from 'luxon'
|
||||||
|
|
||||||
type AuthSuccResult = {
|
type AuthSuccResult = {
|
||||||
notification: {
|
|
||||||
type: string,
|
|
||||||
text: string
|
|
||||||
}
|
|
||||||
user: string,
|
user: string,
|
||||||
role: string,
|
role: string,
|
||||||
token: string
|
token: string
|
||||||
}
|
}
|
||||||
|
|
||||||
type AuthErrResult = {
|
|
||||||
notification: {
|
|
||||||
text: string,
|
|
||||||
type: string
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default class AuthController {
|
export default class AuthController {
|
||||||
|
|
||||||
|
|
||||||
@@ -34,10 +23,6 @@ export default class AuthController {
|
|||||||
const token = await auth.use('api').attempt(username, password)
|
const token = await auth.use('api').attempt(username, password)
|
||||||
|
|
||||||
const result : AuthSuccResult = {
|
const result : AuthSuccResult = {
|
||||||
notification: {
|
|
||||||
type: 'success',
|
|
||||||
text: 'Login successful!'
|
|
||||||
},
|
|
||||||
user: auth.user?.username ?? '',
|
user: auth.user?.username ?? '',
|
||||||
role: auth.user?.role ?? '',
|
role: auth.user?.role ?? '',
|
||||||
token: token.token
|
token: token.token
|
||||||
@@ -46,22 +31,14 @@ export default class AuthController {
|
|||||||
return response.ok(result)
|
return response.ok(result)
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const result : AuthErrResult = {
|
return response.forbidden(error.message)
|
||||||
notification: {
|
|
||||||
type: 'danger',
|
|
||||||
text: error.message
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return response.forbidden(result)
|
public async logout({auth}: HttpContextContract) {
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public async logout({auth, response}: HttpContextContract) {
|
|
||||||
Logger.info("entering logout function")
|
|
||||||
try {
|
try {
|
||||||
await auth.use('api').revoke()
|
await auth.use('api').revoke()
|
||||||
return response.ok('Logged out successfully')
|
return
|
||||||
}
|
}
|
||||||
catch(error) {
|
catch(error) {
|
||||||
Logger.error(error.message)
|
Logger.error(error.message)
|
||||||
|
|||||||
@@ -12,6 +12,13 @@ export default class UserSeeder extends BaseSeeder {
|
|||||||
email: 'test@test.de',
|
email: 'test@test.de',
|
||||||
isActive: true
|
isActive: true
|
||||||
})
|
})
|
||||||
|
await User.create({
|
||||||
|
username: 'user',
|
||||||
|
password: 'user',
|
||||||
|
role: 'employee',
|
||||||
|
email: 'user@test.de',
|
||||||
|
isActive: true
|
||||||
|
})
|
||||||
}
|
}
|
||||||
catch(error) {
|
catch(error) {
|
||||||
Logger.error(error.message)
|
Logger.error(error.message)
|
||||||
|
|||||||
Reference in New Issue
Block a user