40 lines
1.3 KiB
JavaScript
40 lines
1.3 KiB
JavaScript
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
|
|
}) |