token refresh
This commit is contained in:
parent
55caf69bf0
commit
03e0f90279
4 changed files with 40 additions and 9 deletions
|
@ -3,7 +3,7 @@ import { JWTHelper } from "../helpers/jwtHelper";
|
||||||
import { JWTData, JWTToken } from "../type/jwtTypes";
|
import { JWTData, JWTToken } from "../type/jwtTypes";
|
||||||
import InternalException from "../exceptions/internalException";
|
import InternalException from "../exceptions/internalException";
|
||||||
import RefreshCommandHandler from "../command/refreshCommandHandler";
|
import RefreshCommandHandler from "../command/refreshCommandHandler";
|
||||||
import { CreateRefreshCommand } from "../command/refreshCommand";
|
import { CreateRefreshCommand, DeleteRefreshCommand } from "../command/refreshCommand";
|
||||||
import UserService from "../service/userService";
|
import UserService from "../service/userService";
|
||||||
import speakeasy from "speakeasy";
|
import speakeasy from "speakeasy";
|
||||||
import UnauthorizedRequestException from "../exceptions/unauthorizedRequestException";
|
import UnauthorizedRequestException from "../exceptions/unauthorizedRequestException";
|
||||||
|
@ -80,8 +80,8 @@ export async function logout(req: Request, res: Response): Promise<any> {}
|
||||||
* @returns {Promise<*>}
|
* @returns {Promise<*>}
|
||||||
*/
|
*/
|
||||||
export async function refresh(req: Request, res: Response): Promise<any> {
|
export async function refresh(req: Request, res: Response): Promise<any> {
|
||||||
let token = req.body.token;
|
let token = req.body.accessToken;
|
||||||
let refresh = req.body.refresh;
|
let refresh = req.body.refreshToken;
|
||||||
|
|
||||||
const tokenUser = await JWTHelper.decode(token);
|
const tokenUser = await JWTHelper.decode(token);
|
||||||
if (typeof tokenUser == "string" || !tokenUser) {
|
if (typeof tokenUser == "string" || !tokenUser) {
|
||||||
|
@ -121,7 +121,11 @@ export async function refresh(req: Request, res: Response): Promise<any> {
|
||||||
};
|
};
|
||||||
refreshToken = await RefreshCommandHandler.create(refreshCommand);
|
refreshToken = await RefreshCommandHandler.create(refreshCommand);
|
||||||
|
|
||||||
await RefreshCommandHandler.deleteByToken(refresh);
|
let removeToken: DeleteRefreshCommand = {
|
||||||
|
userId: id,
|
||||||
|
token: refresh,
|
||||||
|
};
|
||||||
|
await RefreshCommandHandler.deleteByToken(removeToken);
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
accessToken,
|
accessToken,
|
||||||
|
|
|
@ -4,6 +4,7 @@ import { DataSource } from "typeorm";
|
||||||
import { user } from "./entity/user";
|
import { user } from "./entity/user";
|
||||||
import { refresh } from "./entity/refresh";
|
import { refresh } from "./entity/refresh";
|
||||||
import { Initial1724317398939 } from "./migrations/1724317398939-initial";
|
import { Initial1724317398939 } from "./migrations/1724317398939-initial";
|
||||||
|
import { RefreshPrimaryChange1724573307851 } from "./migrations/1724573307851-refreshPrimaryChange";
|
||||||
|
|
||||||
const dataSource = new DataSource({
|
const dataSource = new DataSource({
|
||||||
type: "mysql",
|
type: "mysql",
|
||||||
|
@ -16,7 +17,7 @@ const dataSource = new DataSource({
|
||||||
logging: process.env.NODE_ENV ? true : ["schema", "error", "warn", "log", "migration"],
|
logging: process.env.NODE_ENV ? true : ["schema", "error", "warn", "log", "migration"],
|
||||||
bigNumberStrings: false,
|
bigNumberStrings: false,
|
||||||
entities: [user, refresh],
|
entities: [user, refresh],
|
||||||
migrations: [Initial1724317398939],
|
migrations: [Initial1724317398939, RefreshPrimaryChange1724573307851],
|
||||||
migrationsRun: true,
|
migrationsRun: true,
|
||||||
migrationsTransactionMode: "each",
|
migrationsTransactionMode: "each",
|
||||||
subscribers: [],
|
subscribers: [],
|
||||||
|
|
|
@ -3,12 +3,12 @@ import { user } from "./user";
|
||||||
|
|
||||||
@Entity()
|
@Entity()
|
||||||
export class refresh {
|
export class refresh {
|
||||||
@PrimaryColumn({ generated: "increment", type: "int" })
|
@PrimaryColumn({ type: "varchar", length: 255 })
|
||||||
id: number;
|
|
||||||
|
|
||||||
@Column({ type: "varchar", length: 255 })
|
|
||||||
token: string;
|
token: string;
|
||||||
|
|
||||||
|
@PrimaryColumn({ type: "int" })
|
||||||
|
userId: number;
|
||||||
|
|
||||||
@Column({ type: "datetime" })
|
@Column({ type: "datetime" })
|
||||||
expiry: Date;
|
expiry: Date;
|
||||||
|
|
||||||
|
|
26
src/migrations/1724573307851-refreshPrimaryChange.ts
Normal file
26
src/migrations/1724573307851-refreshPrimaryChange.ts
Normal 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`);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue