switched to token based authentication

This commit is contained in:
Sockenklaus
2021-10-22 05:29:50 +02:00
parent 293d192691
commit f8bf1a745f
5 changed files with 79 additions and 22 deletions

View File

@@ -1,5 +1,7 @@
import { OpaqueTokenContract } from '@ioc:Adonis/Addons/Auth'
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import Logger from '@ioc:Adonis/Core/Logger'
import User from 'App/Models/User'
type AuthSuccResult = {
notification: {
@@ -7,7 +9,8 @@ type AuthSuccResult = {
text: string
}
user: string,
role: string
role: string,
token: string
}
type AuthErrResult = {
@@ -20,14 +23,14 @@ type AuthErrResult = {
export default class AuthController {
/**TODO #3 implement rememberMe function */
public async login({auth, request, response}: HttpContextContract) {
const username = request.body().username
const password = request.body().password
const rememberMe = request.body().rememberMe ?? false
// const rememberMe = request.body().rememberMe ?? false
try {
await auth.attempt(username, password, rememberMe)
const token = await auth.use('api').attempt(username, password)
const result : AuthSuccResult = {
notification: {
@@ -35,7 +38,8 @@ export default class AuthController {
text: 'Login successful!'
},
user: auth.user?.username ?? '',
role: auth.user?.role ?? ''
role: auth.user?.role ?? '',
token: token.token
}
return response.ok(result)
@@ -53,8 +57,13 @@ export default class AuthController {
}
public async logout({auth, response}: HttpContextContract) {
await auth.logout()
return response.ok('Logged out successfully')
Logger.info("entering logout function")
try {
await auth.use('api').revoke()
return response.ok('Logged out successfully')
}
catch(error) {
Logger.error(error.message)
}
}
}