Skip to content

Commit c5e2736

Browse files
committed
Made huge refactoring
1 parent f666857 commit c5e2736

26 files changed

+529
-454
lines changed

source/api/v1/auth/AuthPreHandler.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,44 @@
11
import { FastifyReply, FastifyRequest, HookHandlerDoneFunction } from "fastify";
2-
import { extractToken } from "../utils/TokenExtractor";
2+
import { extractToken } from "../utils/common/TokenExtractor";
33
import { extractJwtPayload } from "./jwt/PayloadExtractor";
44
import { validateSignature } from "./jwt/SignatureValidator";
55
import { USER_EXCEPTIONS } from "../exceptions/UserExceptions";
6-
import { IAuthService } from "../services/auth/AuthServiceInterface";
6+
import { IAuthService } from "../services/interfaces/AuthServiceInterface";
7+
import { isException } from "../utils/guards/ExceptionGuard";
78

89
export const authenticationFactory = (authService: IAuthService) =>
910
async (request: FastifyRequest, reply: FastifyReply, _done: HookHandlerDoneFunction) => {
1011
const token = extractToken(request)
1112
if (!token) {
12-
1313
reply.code(401).send(USER_EXCEPTIONS.NotAuthorized)
1414
return
1515
}
1616

17-
// validating jwt signature
1817
const signatureIsValid = validateSignature(token)
1918
if (!signatureIsValid) {
20-
2119
reply.code(401).send(USER_EXCEPTIONS.NotAuthorized)
2220
return
2321
}
2422

25-
// extracting payload and validating
2623
const payload = extractJwtPayload(token)
2724
if (!payload) {
2825
reply.code(401).send(USER_EXCEPTIONS.NotAuthorized)
2926
return
3027
}
3128

32-
// checking if token is actual
33-
try {
34-
await authService.compareTokens(payload.login, token)
35-
} catch (exception: any) {
29+
// try {
30+
const relevanceState = await authService.checkTokenRelevance(payload.login, token)
31+
if (isException(relevanceState)) {
3632
reply.code(
37-
exception.statusCode
38-
).send(exception)
39-
33+
relevanceState.statusCode
34+
).send(relevanceState)
4035
return
4136
}
37+
// } catch (exception: any) {
38+
// reply.code(
39+
// exception.statusCode
40+
// ).send(exception)
41+
// return
42+
// }
4243

4344
}
Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,50 @@
1-
import { Entity, Column, PrimaryColumn, CreateDateColumn, UpdateDateColumn, ManyToMany, JoinTable } from "typeorm"
2-
import { DeepOptional } from "typing-assets/src"
3-
import { UserEntity, User, UserWithoutSensetives } from "./User"
4-
5-
export namespace NoteEntity {
6-
7-
@Entity()
8-
export class Note {
9-
@PrimaryColumn("text")
10-
public id: string
11-
12-
@Column("varchar", {
13-
length: 16,
14-
nullable: false
15-
})
16-
public author: string
17-
18-
19-
@Column("varchar", {
20-
length: 100,
21-
nullable: false
22-
})
23-
public title: string
24-
25-
@Column("varchar")
26-
public content: string
27-
28-
@Column("varchar", {
29-
array: true
30-
})
31-
public tags: string[]
32-
33-
@ManyToMany(() => UserEntity.User, null, {cascade: true})
34-
@JoinTable()
35-
public collaborators: UserEntity.User[]
36-
37-
@CreateDateColumn({ type: "timestamp", default: () => "CURRENT_TIMESTAMP(6)" })
38-
public createdAt: Date;
39-
40-
@UpdateDateColumn({ type: "timestamp", default: () => "CURRENT_TIMESTAMP(6)", onUpdate: "CURRENT_TIMESTAMP(6)" })
41-
public updatedAt: Date;
42-
}
43-
}
44-
45-
46-
export type Note = Omit<NoteEntity.Note, "collaborators"> & { collaborators: string[] }
47-
export type NoteUpdate = DeepOptional<Pick<Note, "content" | "tags" | "title">>
48-
export type NotePreview = Pick<Note, "id" | "updatedAt" | "tags" | "title">
49-
export type NoteWithoutMetadata = Omit<Note, "createdAt" | "updatedAt" | "id">
1+
import { Entity, Column, PrimaryColumn, CreateDateColumn, UpdateDateColumn, ManyToMany, JoinTable } from "typeorm"
2+
import { DeepOptional } from "typing-assets/src"
3+
import { UserEntity, UserWithoutSensetives } from "./User"
4+
5+
export namespace NoteEntity {
6+
7+
@Entity()
8+
export class Note {
9+
@PrimaryColumn("text")
10+
public id: string
11+
12+
@Column("varchar", {
13+
length: 16,
14+
nullable: false
15+
})
16+
public author: string
17+
18+
19+
@Column("varchar", {
20+
length: 100,
21+
nullable: false
22+
})
23+
public title: string
24+
25+
@Column("varchar")
26+
public content: string
27+
28+
@Column("varchar", {
29+
array: true
30+
})
31+
public tags: string[]
32+
33+
@ManyToMany(() => UserEntity.User, null, {cascade: true})
34+
@JoinTable()
35+
public collaborators: UserEntity.User[]
36+
37+
@CreateDateColumn({ type: "timestamp", default: () => "CURRENT_TIMESTAMP(6)" })
38+
public createdAt: Date;
39+
40+
@UpdateDateColumn({ type: "timestamp", default: () => "CURRENT_TIMESTAMP(6)", onUpdate: "CURRENT_TIMESTAMP(6)" })
41+
public updatedAt: Date;
42+
}
43+
}
44+
45+
46+
export type Note = Omit<NoteEntity.Note, "collaborators"> & { collaborators: string[] }
47+
export type NoteUpdate = DeepOptional<Pick<Note, "content" | "tags" | "title">>
48+
export type NotePreview = Pick<Note, "id" | "updatedAt" | "tags" | "title">
49+
export type NoteWithoutMetadata = Omit<Note, "createdAt" | "updatedAt" | "id">
5050
export type NoteCollaborators = UserWithoutSensetives[]

source/api/v1/exceptions/AuthExceptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Exception } from "../utils/Exception";
1+
import { Exception } from "../utils/typing/Exception";
22

33
export const AUTH_EXCEPTIONS = {
44
WrongCredentials: {

source/api/v1/exceptions/NoteExceptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Exception} from "../utils/Exception";
1+
import {Exception} from "../utils/typing/Exception";
22

33
export const NOTE_EXCEPTIONS = {
44
NoteNotFound: {

source/api/v1/exceptions/UserExceptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Exception } from "../utils/Exception";
1+
import { Exception } from "../utils/typing/Exception";
22

33
export const USER_EXCEPTIONS = {
44
AlreadyExists: {

source/api/v1/handlers/AuthHandlers.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { FastifyInstance, FastifyReply, FastifyRequest, HookHandlerDoneFunction } from "fastify";
2-
import { IAuthService } from "../services/auth/AuthServiceInterface";
2+
import { IAuthService } from "../services/interfaces/AuthServiceInterface";
33
import { UserCredentials, UserWithoutMetadata } from "../database/entities/User";
44
import { AUTH_EXCEPTIONS } from "../exceptions/AuthExceptions";
55
import { AuthUserSchema, ChangePasswordSchema } from "../validation/schemas/AuthSchemas";
66
import { extractJwtPayload } from "../auth/jwt/PayloadExtractor";
7-
import { extractToken } from "../utils/TokenExtractor";
7+
import { extractToken } from "../utils/common/TokenExtractor";
88
import { isException } from "../utils/guards/ExceptionGuard";
99
import { USER_EXCEPTIONS } from "../exceptions/UserExceptions";
1010

@@ -29,7 +29,7 @@ export const handleAuthRoutes = (
2929
}, async (request, reply) => {
3030
const credentials: UserCredentials = request.body
3131

32-
const result = await authService.authorizeAndGetToken(
32+
const result = await authService.authorizeAndGenerateToken(
3333
credentials.email,
3434
credentials.password
3535
)
@@ -57,14 +57,16 @@ export const handleAuthRoutes = (
5757
preHandler: authenticate
5858
}, async (request, reply) => {
5959
const passwords = request.body
60-
const payload = extractJwtPayload(
60+
const { login } = extractJwtPayload(
6161
extractToken(request)
6262
)
63+
6364
const state = await authService.changePassword(
64-
payload.login,
65+
login,
6566
passwords.oldPassword,
6667
passwords.newPassword
6768
)
69+
6870
if (isException(state)) {
6971
reply.code(state.statusCode).send(state)
7072
return

source/api/v1/handlers/NotesHandlers.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { FastifyInstance, FastifyReply, FastifyRequest, HookHandlerDoneFunction } from "fastify";
2-
import { INotesService } from "../services/notes/NotesServiceInterface";
2+
import { INotesService } from "../services/interfaces/NotesServiceInterface";
33
import { Note, NoteCollaborators, NotePreview, NoteUpdate, NoteWithoutMetadata } from "../database/entities/Note";
44
import { NOTE_EXCEPTIONS } from "../exceptions/NoteExceptions";
55
import { extractJwtPayload } from "../auth/jwt/PayloadExtractor";
6-
import { extractToken } from "../utils/TokenExtractor";
6+
import { extractToken } from "../utils/common/TokenExtractor";
77
import {AddCollaboratorSchema, CreateNoteSchema, DeleteNoteSchema, GetNoteCollaboratorsSchema, GetNoteSchema, GetNotesSchema, RemoveCollaboratorSchema, UpdateNoteSchema } from "../validation/schemas/NoteSchemas";
88
import { isException } from "../utils/guards/ExceptionGuard";
99
import { USER_EXCEPTIONS } from "../exceptions/UserExceptions";
@@ -29,13 +29,13 @@ export const handleNoteRoutes = (
2929
schema: CreateNoteSchema,
3030
preHandler: authenticate
3131
}, async (request, reply) => {
32-
const payload = extractJwtPayload(
32+
const { login } = extractJwtPayload(
3333
extractToken(request)
3434
)
3535

3636
const insertData = {
3737
...request.body,
38-
author: payload.login
38+
author: login
3939
}
4040
const createdNote = await notesService.createNote(insertData)
4141
if (isException(createdNote)) {
@@ -64,7 +64,7 @@ export const handleNoteRoutes = (
6464
preHandler: authenticate
6565
},
6666
async (request, reply) => {
67-
const payload = extractJwtPayload(
67+
const { login } = extractJwtPayload(
6868
extractToken(request)
6969
)
7070

@@ -73,7 +73,7 @@ export const handleNoteRoutes = (
7373
const sort = request.query.sort
7474
const tags = request.query.tags
7575

76-
const notes = await notesService.getMyNotes(payload.login, tags, limit, skip, sort)
76+
const notes = await notesService.getMyNotes(login, tags, limit, skip, sort)
7777
if (isException(notes)) {
7878
reply.code(notes.statusCode).send(notes)
7979
return
@@ -98,7 +98,7 @@ export const handleNoteRoutes = (
9898
schema: GetNotesSchema,
9999
preHandler: authenticate
100100
}, async (request, reply) => {
101-
const payload = extractJwtPayload(
101+
const { login } = extractJwtPayload(
102102
extractToken(request)
103103
)
104104

@@ -107,7 +107,7 @@ export const handleNoteRoutes = (
107107
const sort = request.query.sort
108108
const tags = request.query.tags
109109

110-
const notes = await notesService.getCollaboratedNotes(payload.login, tags, limit, skip, sort)
110+
const notes = await notesService.getCollaboratedNotes(login, tags, limit, skip, sort)
111111
if (isException(notes)) {
112112
reply.code(notes.statusCode).send(notes)
113113
return
@@ -128,13 +128,13 @@ export const handleNoteRoutes = (
128128
preHandler: authenticate
129129
},
130130
async (request, reply) => {
131-
const payload = extractJwtPayload(
131+
const { login } = extractJwtPayload(
132132
extractToken(request)
133133
)
134134

135135
const id = request.params.id
136136

137-
const foundNote = await notesService.getNote(id, payload.login)
137+
const foundNote = await notesService.getNote(id, login)
138138
if (isException(foundNote)) {
139139
reply.code(foundNote.statusCode).send(foundNote)
140140
return
@@ -156,13 +156,13 @@ export const handleNoteRoutes = (
156156
preHandler: authenticate
157157
},
158158
async (request, reply) => {
159-
const payload = extractJwtPayload(
159+
const { login } = extractJwtPayload(
160160
extractToken(request)
161161
)
162162

163163
const id = request.params.id
164164

165-
const state = await notesService.deleteNote(id, payload.login)
165+
const state = await notesService.deleteNote(id, login)
166166
if (isException(state)) {
167167
reply.code(state.statusCode).send(state)
168168
return
@@ -183,14 +183,14 @@ export const handleNoteRoutes = (
183183
schema: UpdateNoteSchema,
184184
preHandler: authenticate
185185
}, async (request, reply) => {
186-
const payload = extractJwtPayload(
186+
const { login } = extractJwtPayload(
187187
extractToken(request)
188188
)
189189

190190
const id = request.params.id
191191
const updateData = request.body
192192

193-
const updatedNote = await notesService.updateNote(id, payload.login, updateData)
193+
const updatedNote = await notesService.updateNote(id, login, updateData)
194194
if (isException(updatedNote)) {
195195
reply.code(updatedNote.statusCode).send(updatedNote)
196196
return
@@ -211,11 +211,13 @@ export const handleNoteRoutes = (
211211
schema: GetNoteCollaboratorsSchema,
212212
preHandler: authenticate
213213
}, async (request, reply) => {
214-
const payload = extractJwtPayload(
214+
const { login } = extractJwtPayload(
215215
extractToken(request)
216216
)
217+
217218
const id = request.params.id
218-
const collaborators = await notesService.getCollaborators(id, payload.login)
219+
220+
const collaborators = await notesService.getCollaborators(id, login)
219221
if (isException(collaborators)) {
220222
reply.code(collaborators.statusCode).send(collaborators)
221223
return
@@ -240,7 +242,7 @@ export const handleNoteRoutes = (
240242
schema: AddCollaboratorSchema,
241243
preHandler: authenticate
242244
}, async (request, reply) => {
243-
const payload = extractJwtPayload(
245+
const { login } = extractJwtPayload(
244246
extractToken(request)
245247
)
246248

@@ -249,7 +251,7 @@ export const handleNoteRoutes = (
249251

250252
const state = await notesService.addCollaborator(
251253
id,
252-
payload.login,
254+
login,
253255
collaboratorLogin
254256
)
255257
if (isException(state)) {
@@ -275,7 +277,7 @@ export const handleNoteRoutes = (
275277
schema: RemoveCollaboratorSchema,
276278
preHandler: authenticate
277279
}, async (request, reply) => {
278-
const payload = extractJwtPayload(
280+
const { login } = extractJwtPayload(
279281
extractToken(request)
280282
)
281283

@@ -284,7 +286,7 @@ export const handleNoteRoutes = (
284286

285287
const state = await notesService.removeCollaborator(
286288
id,
287-
payload.login,
289+
login,
288290
collaboratorLogin
289291
)
290292
if (isException(state)) {

0 commit comments

Comments
 (0)