unified Auth results
This commit is contained in:
@@ -1,22 +1,55 @@
|
||||
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||
import Logger from '@ioc:Adonis/Core/Logger'
|
||||
|
||||
type AuthSuccResult = {
|
||||
notification: {
|
||||
type: string,
|
||||
text: string
|
||||
}
|
||||
user: string,
|
||||
role: string
|
||||
}
|
||||
|
||||
type AuthErrResult = {
|
||||
notification: {
|
||||
text: string,
|
||||
type: string
|
||||
}
|
||||
}
|
||||
|
||||
export default class AuthController {
|
||||
|
||||
|
||||
|
||||
public async login({auth, request, response}: HttpContextContract) {
|
||||
const username = request.body().username
|
||||
const password = request.body().password
|
||||
const rememberMe = request.body().rememberMe ?? false
|
||||
|
||||
try {
|
||||
await auth.attempt(username, password)
|
||||
return response.ok({
|
||||
Message: 'Login successful!',
|
||||
user: auth.user?.username,
|
||||
role: auth.user?.role
|
||||
})
|
||||
await auth.attempt(username, password, rememberMe)
|
||||
|
||||
const result : AuthSuccResult = {
|
||||
notification: {
|
||||
type: 'success',
|
||||
text: 'Login successful!'
|
||||
},
|
||||
user: auth.user?.username ?? '',
|
||||
role: auth.user?.role ?? ''
|
||||
}
|
||||
|
||||
return response.ok(result)
|
||||
|
||||
} catch (error) {
|
||||
return response.forbidden('Unauthorized')
|
||||
}
|
||||
const result : AuthErrResult = {
|
||||
notification: {
|
||||
type: 'danger',
|
||||
text: error.message
|
||||
}
|
||||
}
|
||||
|
||||
return response.forbidden(result)
|
||||
}
|
||||
}
|
||||
|
||||
public async logout({auth, response}: HttpContextContract) {
|
||||
|
||||
Reference in New Issue
Block a user