diff --git a/src/controller/authController.ts b/src/controller/authController.ts index 2990b5c..d89c4aa 100644 --- a/src/controller/authController.ts +++ b/src/controller/authController.ts @@ -3,7 +3,7 @@ import { JWTHelper } from "../helpers/jwtHelper"; import { JWTData, JWTToken } from "../type/jwtTypes"; import InternalException from "../exceptions/internalException"; import RefreshCommandHandler from "../command/refreshCommandHandler"; -import { CreateRefreshCommand } from "../command/refreshCommand"; +import { CreateRefreshCommand, DeleteRefreshCommand } from "../command/refreshCommand"; import UserService from "../service/userService"; import speakeasy from "speakeasy"; import UnauthorizedRequestException from "../exceptions/unauthorizedRequestException"; @@ -80,8 +80,8 @@ export async function logout(req: Request, res: Response): Promise {} * @returns {Promise<*>} */ export async function refresh(req: Request, res: Response): Promise { - let token = req.body.token; - let refresh = req.body.refresh; + let token = req.body.accessToken; + let refresh = req.body.refreshToken; const tokenUser = await JWTHelper.decode(token); if (typeof tokenUser == "string" || !tokenUser) { @@ -121,7 +121,11 @@ export async function refresh(req: Request, res: Response): Promise { }; refreshToken = await RefreshCommandHandler.create(refreshCommand); - await RefreshCommandHandler.deleteByToken(refresh); + let removeToken: DeleteRefreshCommand = { + userId: id, + token: refresh, + }; + await RefreshCommandHandler.deleteByToken(removeToken); res.json({ accessToken, diff --git a/src/data-source.ts b/src/data-source.ts index 11806a9..7200e94 100644 --- a/src/data-source.ts +++ b/src/data-source.ts @@ -4,6 +4,7 @@ import { DataSource } from "typeorm"; import { user } from "./entity/user"; import { refresh } from "./entity/refresh"; import { Initial1724317398939 } from "./migrations/1724317398939-initial"; +import { RefreshPrimaryChange1724573307851 } from "./migrations/1724573307851-refreshPrimaryChange"; const dataSource = new DataSource({ type: "mysql", @@ -16,7 +17,7 @@ const dataSource = new DataSource({ logging: process.env.NODE_ENV ? true : ["schema", "error", "warn", "log", "migration"], bigNumberStrings: false, entities: [user, refresh], - migrations: [Initial1724317398939], + migrations: [Initial1724317398939, RefreshPrimaryChange1724573307851], migrationsRun: true, migrationsTransactionMode: "each", subscribers: [], diff --git a/src/entity/refresh.ts b/src/entity/refresh.ts index e7c3ac8..a781197 100644 --- a/src/entity/refresh.ts +++ b/src/entity/refresh.ts @@ -3,12 +3,12 @@ import { user } from "./user"; @Entity() export class refresh { - @PrimaryColumn({ generated: "increment", type: "int" }) - id: number; - - @Column({ type: "varchar", length: 255 }) + @PrimaryColumn({ type: "varchar", length: 255 }) token: string; + @PrimaryColumn({ type: "int" }) + userId: number; + @Column({ type: "datetime" }) expiry: Date; diff --git a/src/migrations/1724573307851-refreshPrimaryChange.ts b/src/migrations/1724573307851-refreshPrimaryChange.ts new file mode 100644 index 0000000..5fe722a --- /dev/null +++ b/src/migrations/1724573307851-refreshPrimaryChange.ts @@ -0,0 +1,26 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class RefreshPrimaryChange1724573307851 implements MigrationInterface { + name = 'RefreshPrimaryChange1724573307851' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`refresh\` CHANGE \`id\` \`id\` int NOT NULL`); + await queryRunner.query(`ALTER TABLE \`refresh\` DROP PRIMARY KEY`); + await queryRunner.query(`ALTER TABLE \`refresh\` DROP COLUMN \`id\``); + await queryRunner.query(`ALTER TABLE \`refresh\` ADD PRIMARY KEY (\`token\`, \`userId\`)`); + await queryRunner.query(`ALTER TABLE \`refresh\` DROP FOREIGN KEY \`FK_b39e4ed3bfa789758e476870ec2\``); + await queryRunner.query(`ALTER TABLE \`refresh\` CHANGE \`userId\` \`userId\` int NOT NULL`); + await queryRunner.query(`ALTER TABLE \`refresh\` ADD CONSTRAINT \`FK_b39e4ed3bfa789758e476870ec2\` FOREIGN KEY (\`userId\`) REFERENCES \`user\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`refresh\` DROP FOREIGN KEY \`FK_b39e4ed3bfa789758e476870ec2\``); + await queryRunner.query(`ALTER TABLE \`refresh\` CHANGE \`userId\` \`userId\` int NULL DEFAULT 'NULL'`); + await queryRunner.query(`ALTER TABLE \`refresh\` ADD CONSTRAINT \`FK_b39e4ed3bfa789758e476870ec2\` FOREIGN KEY (\`userId\`) REFERENCES \`user\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE \`refresh\` DROP PRIMARY KEY`); + await queryRunner.query(`ALTER TABLE \`refresh\` ADD \`id\` int NOT NULL AUTO_INCREMENT`); + await queryRunner.query(`ALTER TABLE \`refresh\` ADD PRIMARY KEY (\`id\`)`); + await queryRunner.query(`ALTER TABLE \`refresh\` CHANGE \`id\` \`id\` int NOT NULL AUTO_INCREMENT`); + } + +}