Skip to content

Commit

Permalink
Merge branch 'main' of github.com:BinaryStudioAcademy/bsa-2024-bebalance
Browse files Browse the repository at this point in the history
  • Loading branch information
fshabanov committed Aug 21, 2024
2 parents 6eeb580 + 22ece1c commit 079aa22
Show file tree
Hide file tree
Showing 112 changed files with 1,749 additions and 290 deletions.
8 changes: 4 additions & 4 deletions .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
".": "1.3.0",
"apps/backend": "0.0.0",
"apps/frontend": "0.0.0",
"packages/shared": "0.0.0"
".": "1.9.0",
"apps/backend": "1.3.0",
"apps/frontend": "1.4.0",
"packages/shared": "1.4.0"
}
48 changes: 48 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,53 @@
# Changelog

## [1.9.0](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/compare/v1.8.0...v1.9.0) (2024-08-21)


### Features

* Button component bb-26 ([#54](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/issues/54)) ([e9d535d](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/commit/e9d535d7685a93cf35eda81e196ee7af7c1e2d1d))
* impl protected routing bb-11 ([#50](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/issues/50)) ([5f2c71c](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/commit/5f2c71ccfd1dd50f4a894d1ab391690b674ab2eb))
* sign in screen - mobile responsiveness bb-61 ([#78](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/issues/78)) ([f8b36f2](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/commit/f8b36f25d71b15d0be03091bdae04b0f664b6ede))

## [1.8.0](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/compare/v1.7.0...v1.8.0) (2024-08-21)


### Features

* add background component bb-24 ([#84](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/issues/84)) ([55dd338](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/commit/55dd338c10a3b34fa0dc959def4606b62571f0d5))
* authorization token (JWT) bb-10 ([#40](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/issues/40)) ([16e3c35](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/commit/16e3c353ff700ab27a7b6af4fa7b3c17059cc916))
* **backend:** add categories migration bb-34 ([#100](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/issues/100)) ([43ececb](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/commit/43ececb50a49723b2d0ff52ef1041dcfe49be225))
* error handling bb-16 ([#57](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/issues/57)) ([4425001](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/commit/442500105802ba497c02978fd1e9af88eb6ae53d))

## [1.7.0](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/compare/v1.6.0...v1.7.0) (2024-08-21)


### Features

* implement sign-in functionality bb-6 ([#37](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/issues/37)) ([1f2b54c](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/commit/1f2b54c94efa5e1553bc8b4bd24f2fdf2b0f8053))

## [1.6.0](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/compare/v1.5.0...v1.6.0) (2024-08-21)


### Features

* Sign In bb-7 ([#49](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/issues/49)) ([7132640](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/commit/7132640bb557dfc3ab67c7ad131be828b576ef05))

## [1.5.0](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/compare/v1.4.0...v1.5.0) (2024-08-21)


### Features

* added color variables to varibles.css bb-52 ([#93](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/issues/93)) ([1544930](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/commit/1544930f5ce7736753d6dd5af0c8383b12dcb1e9))

## [1.4.0](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/compare/v1.3.0...v1.4.0) (2024-08-20)


### Features

* add Not Found page bb-51 ([#56](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/issues/56)) ([befb626](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/commit/befb626bfc67278a9aa40b3f460885807f35969b))
* mobile linter rule adjust bb-2 ([#82](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/issues/82)) ([584c838](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/commit/584c8387a3150c317123beabb9c962de97f012f5))

## [1.3.0](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/compare/v1.2.0...v1.3.0) (2024-08-20)


Expand Down
7 changes: 7 additions & 0 deletions apps/backend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ DB_CONNECTION_STRING=[db_client]://[db_username]:[db_user_password]@localhost:[d
DB_DIALECT=pg
DB_POOL_MIN=2
DB_POOL_MAX=10

#
# JWT
#
JWT_SECRET=<SECRET_PHRASE>
JWT_EXPIRATION_TIME=24hr
JWT_ALGORITHM=HS256
33 changes: 33 additions & 0 deletions apps/backend/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Changelog

## [1.3.0](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/compare/backend-v1.2.0...backend-v1.3.0) (2024-08-21)


### Features

* impl protected routing bb-11 ([#50](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/issues/50)) ([5f2c71c](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/commit/5f2c71ccfd1dd50f4a894d1ab391690b674ab2eb))

## [1.2.0](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/compare/backend-v1.1.0...backend-v1.2.0) (2024-08-21)


### Features

* authorization token (JWT) bb-10 ([#40](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/issues/40)) ([16e3c35](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/commit/16e3c353ff700ab27a7b6af4fa7b3c17059cc916))
* **backend:** add categories migration bb-34 ([#100](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/issues/100)) ([43ececb](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/commit/43ececb50a49723b2d0ff52ef1041dcfe49be225))

## [1.1.0](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/compare/backend-v1.0.0...backend-v1.1.0) (2024-08-21)


### Features

* implement sign-in functionality bb-6 ([#37](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/issues/37)) ([1f2b54c](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/commit/1f2b54c94efa5e1553bc8b4bd24f2fdf2b0f8053))

## 1.0.0 (2024-08-20)


### Features

* add continuous delivery bb-3 ([#62](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/issues/62)) ([4106756](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/commit/4106756bc7ecd9119ecf302b5d0df67089aa5961))
* add mobile starter bb-2 ([#27](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/issues/27)) ([a9381f3](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/commit/a9381f3827dcce9b01c91deaa8343bc9fe212ffc))
* add web starter bb-1 ([#4](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/issues/4)) ([46357b5](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/commit/46357b59ff3818e0e5bbdcb02b10a3689c9bb1d0))
* sign up bb-8 ([#46](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/issues/46)) ([833b096](https://github.com/BinaryStudioAcademy/bsa-2024-bebalance/commit/833b096800fda00136885ae1bffedada943a1e8a))
8 changes: 5 additions & 3 deletions apps/backend/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "backend",
"type": "module",
"version": "1.0.0",
"version": "1.3.0",
"engines": {
"node": "20.x.x",
"npm": "10.x.x"
Expand Down Expand Up @@ -29,13 +29,15 @@
"convict": "6.2.4",
"dotenv": "16.4.5",
"fastify": "4.28.1",
"fastify-plugin": "4.5.1",
"jose": "5.6.3",
"knex": "3.1.0",
"objection": "3.1.4",
"pg": "8.12.0",
"pino": "9.3.2",
"pino-pretty": "11.2.2",
"swagger-jsdoc": "6.2.8",
"shared": "*"
"shared": "*",
"swagger-jsdoc": "6.2.8"
},
"devDependencies": {
"@types/bcrypt": "5.0.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { type Knex } from "knex";

const TABLE_NAME = "categories";

const ColumnName = {
CREATED_AT: "created_at",
ID: "id",
NAME: "name",
UPDATED_AT: "updated_at",
} as const;

async function up(knex: Knex): Promise<void> {
await knex.schema.createTable(TABLE_NAME, (table) => {
table.increments(ColumnName.ID).primary();
table.string(ColumnName.NAME).notNullable().unique();
table
.dateTime(ColumnName.CREATED_AT)
.notNullable()
.defaultTo(knex.fn.now());
table
.dateTime(ColumnName.UPDATED_AT)
.notNullable()
.defaultTo(knex.fn.now());
});
}

async function down(knex: Knex): Promise<void> {
await knex.schema.dropTableIfExists(TABLE_NAME);
}

export { down, up };
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { type Knex } from "knex";

const TABLE_NAME = "categories";

const ColumnName = {
NAME: "name",
} as const;

const CATEGORIES = [
"Physical",
"Work",
"Friends",
"Love",
"Money",
"Free time",
"Spiritual",
"Mental",
];

async function up(knex: Knex): Promise<void> {
await knex(TABLE_NAME).insert(
CATEGORIES.map((name) => ({
[ColumnName.NAME]: name,
})),
);
}

async function down(knex: Knex): Promise<void> {
await knex(TABLE_NAME).whereIn(ColumnName.NAME, CATEGORIES).del();
}

export { down, up };
2 changes: 1 addition & 1 deletion apps/backend/src/libs/enums/enums.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { RelationName } from "./relation-name.enum.js";
export { APIPath, AppEnvironment, ServerErrorType } from "shared";
export { APIPath, AppEnvironment, ErrorMessage, ServerErrorType } from "shared";
20 changes: 20 additions & 0 deletions apps/backend/src/libs/modules/config/base-config.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,26 @@ class BaseConfig implements Config {
format: Number,
},
},
JWT: {
ALGORITHM: {
default: null,
doc: "Token encryption algorithm",
env: "JWT_ALGORITHM",
format: String,
},
EXPIRATION_TIME: {
default: null,
doc: "Token expiration time",
env: "JWT_EXPIRATION_TIME",
format: String,
},
SECRET: {
default: null,
doc: "Used to sign and validate JWT tokens",
env: "JWT_SECRET",
format: String,
},
},
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ type EnvironmentSchema = {
POOL_MAX: number;
POOL_MIN: number;
};
JWT: {
ALGORITHM: string;
EXPIRATION_TIME: string;
SECRET: string;
};
};

export { type EnvironmentSchema };
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const DatabaseTableName = {
CATEGORIES: "categories",
MIGRATIONS: "migrations",
USER_DETAILS: "user_details",
USERS: "users",
Expand Down
13 changes: 10 additions & 3 deletions apps/backend/src/libs/modules/encrypt/base-encrypt.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { genSalt, hash } from "bcrypt";

import { Encrypt } from "./libs/types/types.js";
import { type Encrypt } from "./libs/types/types.js";

class BaseEncrypt implements Encrypt {
private saltRounds: number;
Expand All @@ -17,9 +17,16 @@ class BaseEncrypt implements Encrypt {
return await genSalt(this.saltRounds);
}

public async encrypt(
public async compare(
password: string,
): Promise<{ hash: string; salt: string }> {
passwordHash: string,
salt: string,
): Promise<boolean> {
const hash = await this.generateHash(password, salt);
return hash === passwordHash;
}

public async encrypt(password: string): ReturnType<Encrypt["encrypt"]> {
const salt = await this.generateSalt();
const hash = await this.generateHash(password, salt);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
type Encrypt = {
encrypt: (data: string) => Promise<{ hash: string; salt: string }>;
compare: (
password: string,
passwordHash: string,
salt: string,
) => Promise<boolean>;

encrypt: (password: string) => Promise<{ hash: string; salt: string }>;
};

export { type Encrypt };
2 changes: 1 addition & 1 deletion apps/backend/src/libs/modules/http/http.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { HTTPCode } from "./libs/enums/enums.js";
export { HTTPCode, HTTPHeader } from "./libs/enums/enums.js";
export { HTTPError } from "./libs/exceptions/exceptions.js";
export { type HTTPMethod } from "./libs/types/types.js";
2 changes: 1 addition & 1 deletion apps/backend/src/libs/modules/http/libs/enums/enums.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { HTTPCode } from "shared";
export { HTTPCode, HTTPHeader } from "shared";
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ import { type Config } from "~/libs/modules/config/config.js";
import { type Database } from "~/libs/modules/database/database.js";
import { HTTPCode, HTTPError } from "~/libs/modules/http/http.js";
import { type Logger } from "~/libs/modules/logger/logger.js";
import { token } from "~/libs/modules/token/token.js";
import { authorizationPlugin } from "~/libs/plugins/plugins.js";
import {
type ServerCommonErrorResponse,
type ServerValidationErrorResponse,
type ValidationSchema,
} from "~/libs/types/types.js";
import { userService } from "~/modules/users/users.js";

import { WHITE_ROUTES } from "./libs/constants/constants.js";
import {
type ServerApplication,
type ServerApplicationApi,
Expand Down Expand Up @@ -205,6 +209,12 @@ class BaseServerApplication implements ServerApplication {
await this.app.register(swaggerUi, {
routePrefix: `/${api.version}/documentation`,
});

await this.app.register(authorizationPlugin, {
token,
userService,
whiteRoutes: WHITE_ROUTES,
});
}),
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { AuthApiPath } from "~/modules/auth/auth.js";

import { APIPath } from "../enums/enums.js";

const WHITE_ROUTES: string[] = [
APIPath.AUTH,
`${APIPath.AUTH}${AuthApiPath.SIGN_IN}`,
`${APIPath.AUTH}${AuthApiPath.SIGN_UP}`,
];

export { WHITE_ROUTES };
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { APIPath } from "~/libs/enums/enums.js";
33 changes: 33 additions & 0 deletions apps/backend/src/libs/modules/token/base-token.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { JWTPayload, jwtVerify, JWTVerifyResult, SignJWT } from "jose";

type Constructor = {
algorithm: string;
expirationTime: string;
secret: Uint8Array;
};

class BaseToken<T extends JWTPayload> {
private algorithm: string;
private expirationTime: string;
private secret: Uint8Array;

constructor({ algorithm, expirationTime, secret }: Constructor) {
this.secret = secret;
this.algorithm = algorithm;
this.expirationTime = expirationTime;
}

public async createToken(payload: T): Promise<string> {
return await new SignJWT(payload)
.setProtectedHeader({ alg: this.algorithm })
.setIssuedAt()
.setExpirationTime(this.expirationTime)
.sign(this.secret);
}

public async decode(token: string): Promise<JWTVerifyResult<T>> {
return await jwtVerify<T>(token, this.secret);
}
}

export { BaseToken };
13 changes: 13 additions & 0 deletions apps/backend/src/libs/modules/token/token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { config } from "~/libs/modules/config/config.js";
import { TokenPayload } from "~/libs/types/types.js";

import { BaseToken } from "./base-token.module.js";

const token = new BaseToken<TokenPayload>({
algorithm: config.ENV.JWT.ALGORITHM,
expirationTime: config.ENV.JWT.EXPIRATION_TIME,
secret: Buffer.from(config.ENV.JWT.SECRET),
});

export { BaseToken } from "./base-token.module.js";
export { token };
Loading

0 comments on commit 079aa22

Please sign in to comment.