Introduces Discord OAuth2 integration for account association and login, including new routes for linking, unlinking, and authenticating via Discord. Adds supporting services, repositories, and schema validation for the OAuth2 flow. Refactors database schema and queries for consistency, and updates dependencies to include required OAuth2 libraries.
55 lines
2.0 KiB
JavaScript
55 lines
2.0 KiB
JavaScript
const logger = require("../modules/logger")
|
|
const database = require("../modules/database")
|
|
const { DefaultError } = require("../errors/errors")
|
|
|
|
async function createLinkAttempt(OAuth2LinkId, playerUuid) {
|
|
try {
|
|
const sql = `
|
|
INSERT INTO oaauth2LinkAttempts (OAuth2LinkId, playerUuid)
|
|
VALUES (?, ?)
|
|
ON DUPLICATE KEY UPDATE playerUuid = VALUES(playerUuid), createdAt = NOW()
|
|
`
|
|
const result = await database.query(sql, [OAuth2LinkId, playerUuid])
|
|
return result.affectedRows > 0
|
|
} catch (error) {
|
|
logger.log("Internal Server Error".bold + " : " + error.toString(), ["MariaDB", "yellow"])
|
|
throw new DefaultError(500, "Internal Server Error", "Database Error")
|
|
}
|
|
}
|
|
|
|
async function popLinkAttempt(OAuth2LinkId) {
|
|
try {
|
|
const selectSql = "SELECT playerUuid FROM oaauth2LinkAttempts WHERE OAuth2LinkId = ?"
|
|
const rows = await database.query(selectSql, [OAuth2LinkId])
|
|
|
|
if (rows.length === 0) return null
|
|
|
|
const playerUuid = rows[0].playerUuid
|
|
|
|
const deleteSql = "DELETE FROM oaauth2LinkAttempts WHERE OAuth2LinkId = ?"
|
|
await database.query(deleteSql, [OAuth2LinkId])
|
|
|
|
return playerUuid
|
|
} catch (error) {
|
|
logger.log("Internal Server Error".bold + " : " + error.toString(), ["MariaDB", "yellow"])
|
|
throw new DefaultError(500, "Internal Server Error", "Database Error")
|
|
}
|
|
}
|
|
|
|
async function unlinkProviderAccount(provider, playerUuid) {
|
|
try {
|
|
const sql = `DELETE FROM playersProperties WHERE name = '${provider}Id' AND uuid = ?`
|
|
const result = await database.query(sql, [playerUuid])
|
|
|
|
return result.affectedRows > 0
|
|
} catch (error) {
|
|
logger.log("Internal Server Error".bold + " : " + error.toString(), ["MariaDB", "yellow"])
|
|
throw new DefaultError(500, "Internal Server Error", "Database Error")
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
popLinkAttempt,
|
|
createLinkAttempt,
|
|
unlinkProviderAccount
|
|
} |