Cancel le reward fulfill + Refresh token dans DB

This commit is contained in:
Zachary Guénot
2023-06-14 18:43:11 +02:00
parent 1478d6ded3
commit bcb1427148
4 changed files with 62 additions and 8 deletions

40
utils/dbRefreshToken.js Normal file
View File

@@ -0,0 +1,40 @@
const mysql = require('mysql2/promise')
require('dotenv').config()
module.exports = dbRefreshToken = (async (action, type, token) => {
// Create a connection to the MySQL database
const connection = await mysql.createConnection({
host: process.env.MYSQL_HOST,
port: process.env.MYSQL_PORT,
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DATABASE
})
if (action === 'get') {
// Get the refresh token from the database
let results = await connection.query('SELECT value FROM env WHERE refresh_token = ?', [type])
.then(async ([rows, fields]) => { return rows[0].value })
.catch(error => { console.error(error) })
// Terminate the connection to the database
await connection.end()
if (!results) return { error: "Can't get the refresh token !" }
return results
}
else if (action === 'update') {
// Update the refresh token in the database
let results = await connection.query('UPDATE env SET value = ? WHERE refresh_token = ?', [token, type])
.then(async ([rows, fields]) => { return rows })
.catch(error => { console.error(error) })
// Terminate the connection to the database
await connection.end()
if (!results) return { error: "Can't update the refresh token !" }
return results
}
return result
})