Skip to content

Commit da5de89

Browse files
committed
change preHandler on preValidation and change scheme on signOut
1 parent a1dc4c7 commit da5de89

File tree

6 files changed

+25
-31
lines changed

6 files changed

+25
-31
lines changed

app/components/auth/signOut.ts

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
1-
import {
2-
FastifyInstance,
3-
FastifyReply,
4-
FastifyRequest,
5-
RouteShorthandOptions,
6-
} from 'fastify';
7-
8-
export const options: RouteShorthandOptions = {
9-
schema: {
10-
body: {
11-
type: 'object',
12-
required: ['token'],
13-
properties: {
14-
token: {type: 'string'},
15-
},
1+
import {FastifyInstance, FastifySchema} from 'fastify';
2+
import {checkToken} from '@/hooks';
3+
//todo во всех схемах добавить описание хедеров как тут
4+
export const options: FastifySchema = {
5+
headers: {
6+
type: 'object',
7+
required: ['token'],
8+
properties: {
9+
token: {type: 'string'},
1610
},
17-
response: {
18-
200: {
19-
type: 'string',
20-
},
21-
404: {
22-
type: 'string'
23-
}
11+
},
12+
response: {
13+
200: {
14+
type: 'boolean',
2415
},
16+
404: {
17+
type: 'boolean'
18+
}
2519
},
2620
};
2721
interface IBody {
@@ -30,11 +24,11 @@ interface IBody {
3024
export const signOut = async (server: FastifyInstance) => {
3125
server.delete<{Body: IBody}>(
3226
'/signout',
33-
options,
27+
{schema: options, preValidation: checkToken},
3428
async (req, reply) => {
3529
const {rowCount} = await server.pg.query('delete from root.users_access where token = $1 AND expires > current_timestamp', [req.body.token]);
3630

37-
rowCount ? reply.send() : reply.status(500).send();
31+
rowCount ? reply.send(false) : reply.status(500).send(true);
3832
}
3933
);
4034
};

app/components/children/children.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ interface IHeaders {
2323
export const children = async (server: FastifyInstance) => {
2424
server.post<{ Body: IBodyPost, Headers: IHeaders }>(
2525
'/children',
26-
{schema: schemePost, preHandler: checkToken},
26+
{schema: schemePost, preValidation: checkToken},
2727
async (req, reply) => {
2828
const {children} = req.body;
2929
const {userId} = req.headers;
@@ -54,7 +54,7 @@ export const children = async (server: FastifyInstance) => {
5454

5555
server.delete<{ Params: IParamsDelete }>(
5656
'/children/:id',
57-
{schema: schemeDelete, preHandler: checkToken},
57+
{schema: schemeDelete, preValidation: checkToken},
5858
async (req, reply) => {
5959
const {id} = req.params;
6060

app/components/diary/note.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface IHeaders {
1111
export const note = async (server: FastifyInstance) => {
1212
server.post<{ Body: INoteBodyPost, Headers: IHeaders }>(
1313
'/diary-note',
14-
{schema: noteSchemePost, preHandler: checkToken},
14+
{schema: noteSchemePost, preValidation: checkToken},
1515
async (req, reply) => {
1616
const {userId} = req.headers;
1717
const {note} = req.body;
@@ -53,7 +53,7 @@ export const note = async (server: FastifyInstance) => {
5353

5454
server.patch<{ Body: INoteBodyPatch, Headers: IHeaders }>(
5555
'/diary-note',
56-
{schema: noteSchemePost, preHandler: checkToken},
56+
{schema: noteSchemePost, preValidation: checkToken},
5757
async (req, reply) => {
5858
const {userId} = req.headers;
5959
const {note} = req.body;

app/components/mainScreen/mainScreen.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface IHeaders {
99
export const mainScreen = async (server: FastifyInstance) => {
1010
server.get<{ Headers: IHeaders }>(
1111
'/mainScreen',
12-
{schema: schemeGet, preHandler: checkToken},
12+
{schema: schemeGet, preValidation: checkToken},
1313
async (req, reply) => {
1414
const {userId} = req.headers;
1515

app/components/users/users.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ interface IBodyPatch {
1515
export const users = async (server: FastifyInstance) => {
1616
server.patch<{ Body: IBodyPatch }>(
1717
'/users',
18-
{schema: schemePatch, preHandler: checkToken},
18+
{schema: schemePatch, preValidation: checkToken},
1919
async (req, reply) => {
2020
const {conceptionDate, dateEnd, gestationalAge, lastMenstruationDate, userId, cycleDuration, deletePregnant} = req.body;
2121

app/hooks/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ interface IHeaders {
1313
userId?: number
1414
}
1515
export const checkToken = async <A, B, C, RouteGeneric> (req: FastifyRequest<RouteGeneric & {Body?: IBody, Headers?: IHeaders}>, reply: FastifyReply) => {
16-
const token = req.body?.token || req.headers?.token;
16+
const token = req.headers?.token;
1717

1818
if (token) {
1919
const {rows} = await server.pg.query('select user_id from root.users_access where token=$1 AND expires > current_timestamp', [token]);

0 commit comments

Comments
 (0)