token refresh

This commit is contained in:
Julian Krauser 2024-08-25 10:09:57 +02:00
parent 55caf69bf0
commit 03e0f90279
4 changed files with 40 additions and 9 deletions

View file

@ -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<any> {}
* @returns {Promise<*>}
*/
export async function refresh(req: Request, res: Response): Promise<any> {
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<any> {
};
refreshToken = await RefreshCommandHandler.create(refreshCommand);
await RefreshCommandHandler.deleteByToken(refresh);
let removeToken: DeleteRefreshCommand = {
userId: id,
token: refresh,
};
await RefreshCommandHandler.deleteByToken(removeToken);
res.json({
accessToken,

View file

@ -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: [],

View file

@ -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;

View file

@ -0,0 +1,26 @@
import { MigrationInterface, QueryRunner } from "typeorm";
export class RefreshPrimaryChange1724573307851 implements MigrationInterface {
name = 'RefreshPrimaryChange1724573307851'
public async up(queryRunner: QueryRunner): Promise<void> {
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<void> {
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`);
}
}