Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OV-5: Add jwt token #22

Merged
merged 80 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
5af58d6
OV-5: + add jose to installed packages
stefano-lacorazza Aug 19, 2024
5b37d18
OV-5: + add token-services.ts
stefano-lacorazza Aug 19, 2024
7cc363d
Merge branch 'task/OV-2-add-sign-in-flow' into task/OV-5-JWT-token
stefano-lacorazza Aug 19, 2024
e7d454f
OV-5: + add token to UserSignUpResponseDto
stefano-lacorazza Aug 20, 2024
0a5d350
OV-5: + return token in signin method
stefano-lacorazza Aug 20, 2024
e7d5c56
OV-5: * fix userId type in token from string to number
stefano-lacorazza Aug 20, 2024
9d4d9c5
OV-5: * move token services to appropiate folder
stefano-lacorazza Aug 20, 2024
1d0748a
OV-5: - remove trailing white space
stefano-lacorazza Aug 20, 2024
d1eab67
OV-5: * change to property shorthand
stefano-lacorazza Aug 20, 2024
d209ceb
OV-5: * refactor token-services in OOP
stefano-lacorazza Aug 20, 2024
f62acc2
OV-5: * use tokenService.createToken
stefano-lacorazza Aug 20, 2024
add55d7
OV-5: * add tokenservice to services and update imports
stefano-lacorazza Aug 20, 2024
68aba69
OV-5: * fix Wrong amount of left-padding spaces
stefano-lacorazza Aug 20, 2024
bf46eae
OV-5: * added final new line
stefano-lacorazza Aug 20, 2024
e112ea7
OV-5: * run prettier
stefano-lacorazza Aug 20, 2024
a7ce5be
OV-5: * use property shorthand
stefano-lacorazza Aug 20, 2024
8ce8243
OV-8: + empty auth jwt plugin
Sanchousina Aug 20, 2024
44b2532
OV-8: + register plugins
Sanchousina Aug 20, 2024
6154caf
Merge branch 'task/OV-2-add-sign-in-flow' into task/OV-5-JWT-token
stefano-lacorazza Aug 20, 2024
da1397e
OV-8: + dependencies
Sanchousina Aug 20, 2024
5af5016
OV-8: * merging with OV-5
Sanchousina Aug 20, 2024
31fae83
OV-5: * return user object and token
stefano-lacorazza Aug 20, 2024
3356aaf
OV-5: * added secretkey and expiration time to env
stefano-lacorazza Aug 20, 2024
618807b
OV-5: * move signup token creation to auth.service
stefano-lacorazza Aug 20, 2024
9022ef5
OV-5: + add getUserIdFromToken
stefano-lacorazza Aug 20, 2024
58bd5a3
OV-5: * remove Trailing whitespace
stefano-lacorazza Aug 20, 2024
9721153
OV-8: + findById method to Repository type
Sanchousina Aug 20, 2024
89ec60b
OV-8: + findById method to Service type
Sanchousina Aug 20, 2024
782a756
OV-5: * prettier
stefano-lacorazza Aug 20, 2024
b707dc3
OV-8: + findById method to user service and repository
Sanchousina Aug 20, 2024
5e46444
OV-8: * userId to number
Sanchousina Aug 20, 2024
50245d6
OV-8: + http codes to http code enum
Sanchousina Aug 20, 2024
ee3a4e5
OV-8: + hook and error messages enums for auth plugin
Sanchousina Aug 20, 2024
508cd31
Merge remote-tracking branch 'origin/task/OV-5-JWT-token' into task/O…
Sanchousina Aug 20, 2024
8eb1aae
OV-8: * export user entity
Sanchousina Aug 20, 2024
3561451
OV-8: * implement auth jwt plugin
Sanchousina Aug 20, 2024
f034695
OV-8: + white routes constant
Sanchousina Aug 20, 2024
36a220e
OV-8: * refactor checking for white route
Sanchousina Aug 20, 2024
9e4004b
OV-8: * use white routes constant
Sanchousina Aug 20, 2024
502aad9
OV-8: * extract fastify module augmentation into file
Sanchousina Aug 20, 2024
a9e0c53
Merge remote-tracking branch 'origin/next' into task/OV-8-protect-rou…
Sanchousina Aug 20, 2024
d4e33bc
OV-5: - remove jose from root dependencies
stefano-lacorazza Aug 21, 2024
8649f58
OV-5: * use config class for enviroment variables
stefano-lacorazza Aug 21, 2024
5c30474
OV-5: * use config class for enviroment variables
stefano-lacorazza Aug 21, 2024
34ea01d
Merge branch 'next' into task/OV-5-JWT-token
stefano-lacorazza Aug 21, 2024
f07ba3b
OV-5: * prettier
stefano-lacorazza Aug 21, 2024
218665a
OV-8: * modify find user method instead of adding findById
Sanchousina Aug 21, 2024
2f9f951
OV-8: * move dependencies to backend
Sanchousina Aug 21, 2024
9f17f91
OV-8: * white routes constant to include path method
Sanchousina Aug 21, 2024
3aaf539
OV-8: + route type
Sanchousina Aug 21, 2024
dbfec2d
OV-8: * checking if route is in white list with method
Sanchousina Aug 21, 2024
5a24c65
Merge remote-tracking branch 'origin/task/OV-5-JWT-token' into task/O…
Sanchousina Aug 21, 2024
b4fbbde
OV-8: + util function for checking route in white routes
Sanchousina Aug 21, 2024
a88eca1
OV-8: * modify find method in user repository instead of findById
Sanchousina Aug 21, 2024
1ac2392
OV-8: * use id instead of userId in types for repository and service
Sanchousina Aug 21, 2024
b4fe14c
Merge branch 'next' into task/OV-5-JWT-token
stefano-lacorazza Aug 22, 2024
9b53386
OV-5: * prettier
stefano-lacorazza Aug 22, 2024
0354932
OV-8: - remove commented code
Sanchousina Aug 23, 2024
4a7fc7e
OV-5: * change || to ??
stefano-lacorazza Aug 22, 2024
1723fa8
OV-5: - remove whitespace
stefano-lacorazza Aug 23, 2024
1ce3a2e
OV-5: * change getUserIdFromToken
stefano-lacorazza Aug 23, 2024
0a639aa
OV-5: * make token optional in UserSignInResponseDto
stefano-lacorazza Aug 25, 2024
e7c3c1c
OV-5: - delete user-sig-in.validation-schema.ts
stefano-lacorazza Aug 26, 2024
e9b8425
OV-8: * rename find to findById in service and repository
Sanchousina Aug 27, 2024
c1b02a8
Merge branch 'next' into task/OV-5-JWT-token
stefano-lacorazza Aug 27, 2024
70ec67d
OV-5: * fix base-config-package.ts and services.ts imports
stefano-lacorazza Aug 27, 2024
471c32b
OV-5: * prettier
stefano-lacorazza Aug 27, 2024
9e20e1c
Merge remote-tracking branch origin/task/OV-5-JWT-token into task/OV-…
Sanchousina Aug 27, 2024
f826db6
Merge pull request #33 from BinaryStudioAcademy/task/OV-8-protect-rou…
nikita-remeslov Aug 28, 2024
164b863
OV-5: * change createEnvSchema() to get envSchema()
stefano-lacorazza Aug 28, 2024
98edd98
Merge branch 'next' into task/OV-5-JWT-token
stefano-lacorazza Aug 28, 2024
537496e
OV-5: + add final newline to base configpackage
stefano-lacorazza Aug 28, 2024
0984b60
OV-5: * prettier
stefano-lacorazza Aug 28, 2024
ab05215
Merge branch 'next' into task/OV-5-JWT-token
stefano-lacorazza Aug 28, 2024
a8687aa
OV-5: * change id to string and findId to find
stefano-lacorazza Aug 28, 2024
c4bcd77
OV-5: * change notation
stefano-lacorazza Aug 29, 2024
b566e19
OV-5: * change notation to brackets
stefano-lacorazza Aug 29, 2024
113a204
OV-5: * change id notation
stefano-lacorazza Aug 29, 2024
56eae81
OV-5: * change class fields to camelCase
stefano-lacorazza Aug 29, 2024
d095953
OV-5: * UserId to id
stefano-lacorazza Aug 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug-report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Bug Report
description: Report a bug
title: 'BUG:'
labels: ['BUG']
projects: "BinaryStudioAcademy/30"
projects: 'BinaryStudioAcademy/30'
body:
- type: markdown
attributes:
Expand Down
38 changes: 19 additions & 19 deletions .github/ISSUE_TEMPLATE/feature.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
name: Feature
description: Abstract feature description
title: "FEAT:"
projects: "BinaryStudioAcademy/30"
title: 'FEAT:'
projects: 'BinaryStudioAcademy/30'
body:
- type: textarea
id: what-feature
attributes:
label: What feature?
placeholder: Add descriptions
validations:
required: true
- type: textarea
id: screenshots
attributes:
label: Add screenshots
placeholder: Add screenshots, mockups, etc.
- type: textarea
id: acceptance-criteria
attributes:
label: Acceptance criteria
placeholder: Add acceptance criteria.
- type: textarea
id: what-feature
attributes:
label: What feature?
placeholder: Add descriptions
validations:
required: true
- type: textarea
id: screenshots
attributes:
label: Add screenshots
placeholder: Add screenshots, mockups, etc.
- type: textarea
id: acceptance-criteria
attributes:
label: Acceptance criteria
placeholder: Add acceptance criteria.
17 changes: 17 additions & 0 deletions .github/ISSUE_TEMPLATE/task.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Task
description: Abstract task description
title: 'TASK:'
projects: 'BinaryStudioAcademy/30'
body:
- type: textarea
id: what-feature
attributes:
label: What task?
placeholder: Add descriptions
validations:
required: true
- type: textarea
id: screenshots
attributes:
label: Add screenshots
placeholder: Add screenshots, mockups, etc.
14 changes: 0 additions & 14 deletions .github/labeler.yml

This file was deleted.

11 changes: 0 additions & 11 deletions .github/workflows/ci-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,3 @@ jobs:

run: |
npx danger ci --failOnErrors --text-only

- name: Add Folder Labels
uses: actions/labeler@v4
with:
repo-token: '${{ secrets.GITHUB_TOKEN }}'
sync-labels: true

- name: Add Environment Labels
uses: ffittschen/pr-branch-labeler@v1
with:
repo-token: '${{ secrets.GITHUB_TOKEN }}'
2 changes: 1 addition & 1 deletion .ls-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ ls:
.ts: snake_case

ignore:
- .git
- .github
- node_modules
- build
- shared/build
Expand Down
7 changes: 5 additions & 2 deletions backend/src/bundles/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
type UserSignInResponseDto,
} from '~/bundles/users/users.js';
import { HttpCode, HttpError } from '~/common/http/http.js';
import { cryptService } from '~/common/services/services.js';
import { cryptService, tokenService } from '~/common/services/services.js';

import { UserValidationMessage } from './enums/enums.js';

Expand Down Expand Up @@ -45,8 +45,11 @@ class AuthService {
status: HttpCode.BAD_REQUEST,
});
}
const id = user.toObject().id;

return user.toObject();
const token = await tokenService.createToken(id);

return { id, email, token };
}

public signUp(
Expand Down
3 changes: 2 additions & 1 deletion backend/src/bundles/users/user.repository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { UserEntity } from '~/bundles/users/user.entity.js';
import { type UserModel } from '~/bundles/users/user.model.js';
import { type Repository } from '~/common/types/types.js';

import { UserEntity } from '../../bundles/users/user.entity.js';

class UserRepository implements Repository {
private userModel: typeof UserModel;

Expand Down
8 changes: 5 additions & 3 deletions backend/src/bundles/users/user.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { UserEntity } from '~/bundles/users/user.entity.js';
import { type UserRepository } from '~/bundles/users/user.repository.js';
import { cryptService } from '~/common/services/services.js';
import { cryptService, tokenService } from '~/common/services/services.js';
import { type Service } from '~/common/types/types.js';

import { UserEntity } from '../../bundles/users/user.entity.js';
import {
type UserGetAllResponseDto,
type UserSignUpRequestDto,
Expand Down Expand Up @@ -39,8 +39,10 @@ class UserService implements Service {
passwordHash: hash, // TODO
}),
);
const { id, email } = user.toObject();
const token = await tokenService.createToken(id);

stefano-lacorazza marked this conversation as resolved.
Show resolved Hide resolved
return user.toObject();
return { 'id': id, 'email': email, 'token': token };
}
o-nedashkivska marked this conversation as resolved.
Show resolved Hide resolved

public update(): ReturnType<Service['update']> {
Expand Down
4 changes: 3 additions & 1 deletion backend/src/common/services/services.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { CryptService } from './crypt/crypt.service.js';
import { TokenService } from './token/token.services.js';

const cryptService = new CryptService();
const tokenService = new TokenService('your_secret_key', '24h');
stefano-lacorazza marked this conversation as resolved.
Show resolved Hide resolved

export { cryptService };
export { cryptService, tokenService };
30 changes: 30 additions & 0 deletions backend/src/common/services/token/token.services.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { type JWTPayload as TokenPayload } from 'jose';
import { jwtVerify, SignJWT } from 'jose';

class TokenService {
private SECRET_KEY: Uint8Array;
private EXPIRATION_TIME: string;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are using camelCase for class fields throughout a project


public constructor(secretKey: string, expirationTime: string) {
this.SECRET_KEY = new TextEncoder().encode(secretKey);
this.EXPIRATION_TIME = expirationTime;
}

public async createToken(userId: number): Promise<string> {
return await new SignJWT({ userId })
.setProtectedHeader({ alg: 'HS256' })
.setExpirationTime(this.EXPIRATION_TIME)
.sign(this.SECRET_KEY);
}

public async verifyToken(token: string): Promise<TokenPayload | null> {
try {
const { payload } = await jwtVerify(token, this.SECRET_KEY);
return payload;
} catch {
return null;
}
}
}

export { TokenService };
2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"npm": "10.x.x"
},
"scripts": {
"lint:css": "npx stylelint \"src/**/*.scss\"",
"lint:css": "npx stylelint \"src/**/*.scss\" --aei",
"lint:js": "npx eslint \"src/**/*.{ts,tsx}\"",
"lint:type": "npx tsc --noEmit",
"lint": "npm run lint:type && npm run lint:js",
Expand Down
4 changes: 1 addition & 3 deletions frontend/src/framework/config/base-config.package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ class BaseConfig implements Config {
private get envSchema(): EnvironmentSchema {
return {
APP: {
ENVIRONMENT: import.meta.env[
'VITE_APP_NODE_ENV'
],
ENVIRONMENT: import.meta.env['VITE_APP_NODE_ENV'],
},
API: {
ORIGIN_URL: import.meta.env[
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type AppEnvironment } from '~/bundles/common/enums/enums.js';
import { type ValueOf } from '~/bundles/common/types/types.js';
import { type ValueOf } from '~/bundles/common/types/types.js';

type EnvironmentSchema = {
APP: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type ContentType } from '~/bundles/common/enums/enums.js';
import { type ValueOf } from '~/bundles/common/types/types.js';
import { type ValueOf } from '~/bundles/common/types/types.js';
import { type HttpOptions } from '~/framework/http/http.js';

type HttpApiOptions = Omit<HttpOptions, 'headers' | 'payload'> & {
Expand Down
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,8 @@
"simple-git-hooks": {
"pre-commit": "npx lint-staged",
"commit-msg": "npx commitlint --edit $1"
},
"dependencies": {
"jose": "5.7.0"
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add this as backend dependency

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thank you

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also remove it from from the root dependency

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thank you

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
type UserSignUpResponseDto = {
id: number;
email: string;
token: string;
};

export { type UserSignUpResponseDto };
Loading