UNPKG

open-music-api-node

Version:
42 lines (32 loc) 876 B
const { Pool } = require('pg'); const InvariantError = require('../../exceptions/InvariantError'); class AuthService { constructor() { this._pool = new Pool(); } async addRefreshToken(token) { const query = { text: 'INSERT INTO authentications VALUES($1)', values: [token], }; await this._pool.query(query); } async verifyRefreshToken(token) { const query = { text: 'SELECT token FROM authentications WHERE token = $1', values: [token], }; const result = await this._pool.query(query); if (!result.rows.length) { throw new InvariantError('Refresh token tidak valid'); } } async deleteRefreshToken(token) { const query = { text: 'DELETE FROM authentications WHERE token = $1', values: [token], }; await this._pool.query(query); } } module.exports = AuthService;