added nonce creattion
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
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'
|
||||
import Nonce from 'App/Models/Nonce'
|
||||
import Database from '@ioc:Adonis/Lucid/Database'
|
||||
import { DateTime } from 'luxon'
|
||||
|
||||
type AuthSuccResult = {
|
||||
notification: {
|
||||
@@ -66,4 +67,30 @@ export default class AuthController {
|
||||
Logger.error(error.message)
|
||||
}
|
||||
}
|
||||
|
||||
public async nonce(ctx: HttpContextContract) {
|
||||
const requestId = ctx.request.header('X-REQUEST-ID')
|
||||
|
||||
try {
|
||||
this.deleteExpiredNonces()
|
||||
|
||||
const nonce = await Nonce.create({
|
||||
requestId: requestId
|
||||
})
|
||||
|
||||
return nonce.nonce
|
||||
}
|
||||
catch(err){
|
||||
return ctx.response.forbidden(err)
|
||||
}
|
||||
}
|
||||
|
||||
private async deleteExpiredNonces(){
|
||||
try {
|
||||
await Database.from('nonces').where('expiry_date', '<', DateTime.now().toFormat('yyyy-MM-dd HH:mm:ss')).delete()
|
||||
}
|
||||
catch(err){
|
||||
throw err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user