-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Make temporary solution for API key [DEV-3400] (#425)
* Refactoring + adding API key logic * Fix log-in/ log-out button sunctionality * Format things * Fix build * Get rid of magic constant * Fix review comments
- Loading branch information
Andrew Nikitin
authored
Nov 15, 2023
1 parent
56f4675
commit 5e6cdf4
Showing
34 changed files
with
2,226 additions
and
803 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { BeforeInsert, BeforeUpdate, Column, Entity, JoinColumn, ManyToOne, PrimaryGeneratedColumn } from 'typeorm'; | ||
|
||
import * as dotenv from 'dotenv'; | ||
import { CustomerEntity } from './customer.entity.js'; | ||
import { UserEntity } from './user.entity.js'; | ||
dotenv.config(); | ||
|
||
@Entity('apiKey') | ||
export class APIKeyEntity { | ||
@PrimaryGeneratedColumn('uuid') | ||
apiKeyId!: string; | ||
|
||
@Column({ | ||
type: 'text', | ||
nullable: false, | ||
}) | ||
apiKey!: string; | ||
|
||
@Column({ | ||
type: 'timestamptz', | ||
nullable: false, | ||
}) | ||
expiresAt!: Date; | ||
|
||
@Column({ | ||
type: 'timestamptz', | ||
nullable: false, | ||
}) | ||
createdAt!: Date; | ||
|
||
@Column({ | ||
type: 'timestamptz', | ||
nullable: true, | ||
}) | ||
updatedAt!: Date; | ||
|
||
@ManyToOne(() => CustomerEntity, (customer) => customer.customerId, { onDelete: 'CASCADE' }) | ||
@JoinColumn({ name: 'customerId' }) | ||
customer!: CustomerEntity; | ||
|
||
@ManyToOne(() => UserEntity, (user) => user.logToId, { onDelete: 'CASCADE' }) | ||
@JoinColumn({ name: 'userId' }) | ||
user!: UserEntity; | ||
|
||
@BeforeInsert() | ||
setCreatedAt() { | ||
this.createdAt = new Date(); | ||
} | ||
|
||
@BeforeUpdate() | ||
setUpdateAt() { | ||
this.updatedAt = new Date(); | ||
} | ||
|
||
public isExpired(): boolean { | ||
return this.expiresAt < new Date(); | ||
} | ||
|
||
constructor(apiKeyId: string, apiKey: string, expiresAt: Date, customer: CustomerEntity, user: UserEntity) { | ||
this.apiKeyId = apiKeyId; | ||
this.apiKey = apiKey; | ||
this.expiresAt = expiresAt; | ||
this.customer = customer; | ||
this.user = user; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { MigrationInterface, QueryRunner, Table, TableForeignKey } from 'typeorm'; | ||
|
||
export class CreateAPIKeyTable1695740345977 implements MigrationInterface { | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
const table = new Table({ | ||
name: 'apiKey', | ||
columns: [ | ||
{ | ||
name: 'apiKeyId', | ||
type: 'uuid', | ||
isPrimary: true, | ||
isGenerated: true, | ||
generationStrategy: 'uuid', | ||
}, | ||
{ name: 'apiKey', type: 'text', isNullable: false }, | ||
{ name: 'customerId', type: 'uuid', isNullable: false }, | ||
{ name: 'userId', type: 'text', isNullable: false }, | ||
{ name: 'expiresAt', type: 'timestamptz', isNullable: false }, | ||
{ name: 'createdAt', type: 'timestamptz', isNullable: false }, | ||
{ name: 'updatedAt', type: 'timestamptz', isNullable: true }, | ||
], | ||
}); | ||
|
||
await queryRunner.createTable(table); | ||
|
||
await queryRunner.createForeignKey( | ||
table, | ||
new TableForeignKey({ | ||
columnNames: ['customerId'], | ||
referencedColumnNames: ['customerId'], | ||
referencedTableName: 'customer', | ||
onDelete: 'CASCADE', | ||
}) | ||
); | ||
|
||
await queryRunner.createForeignKey( | ||
table, | ||
new TableForeignKey({ | ||
columnNames: ['userId'], | ||
referencedColumnNames: ['logToId'], | ||
referencedTableName: 'user', | ||
onDelete: 'CASCADE', | ||
}) | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
throw new Error('illegal_operation: cannot roll back initial migration'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.