Skip to content

Commit

Permalink
change preHandler on preValidation and change scheme on signOut
Browse files Browse the repository at this point in the history
  • Loading branch information
mk1020 committed Jun 15, 2021
1 parent a1dc4c7 commit da5de89
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 31 deletions.
42 changes: 18 additions & 24 deletions app/components/auth/signOut.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
import {
FastifyInstance,
FastifyReply,
FastifyRequest,
RouteShorthandOptions,
} from 'fastify';

export const options: RouteShorthandOptions = {
schema: {
body: {
type: 'object',
required: ['token'],
properties: {
token: {type: 'string'},
},
import {FastifyInstance, FastifySchema} from 'fastify';
import {checkToken} from '@/hooks';
//todo во всех схемах добавить описание хедеров как тут
export const options: FastifySchema = {
headers: {
type: 'object',
required: ['token'],
properties: {
token: {type: 'string'},
},
response: {
200: {
type: 'string',
},
404: {
type: 'string'
}
},
response: {
200: {
type: 'boolean',
},
404: {
type: 'boolean'
}
},
};
interface IBody {
Expand All @@ -30,11 +24,11 @@ interface IBody {
export const signOut = async (server: FastifyInstance) => {
server.delete<{Body: IBody}>(
'/signout',
options,
{schema: options, preValidation: checkToken},
async (req, reply) => {
const {rowCount} = await server.pg.query('delete from root.users_access where token = $1 AND expires > current_timestamp', [req.body.token]);

rowCount ? reply.send() : reply.status(500).send();
rowCount ? reply.send(false) : reply.status(500).send(true);
}
);
};
4 changes: 2 additions & 2 deletions app/components/children/children.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface IHeaders {
export const children = async (server: FastifyInstance) => {
server.post<{ Body: IBodyPost, Headers: IHeaders }>(
'/children',
{schema: schemePost, preHandler: checkToken},
{schema: schemePost, preValidation: checkToken},
async (req, reply) => {
const {children} = req.body;
const {userId} = req.headers;
Expand Down Expand Up @@ -54,7 +54,7 @@ export const children = async (server: FastifyInstance) => {

server.delete<{ Params: IParamsDelete }>(
'/children/:id',
{schema: schemeDelete, preHandler: checkToken},
{schema: schemeDelete, preValidation: checkToken},
async (req, reply) => {
const {id} = req.params;

Expand Down
4 changes: 2 additions & 2 deletions app/components/diary/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface IHeaders {
export const note = async (server: FastifyInstance) => {
server.post<{ Body: INoteBodyPost, Headers: IHeaders }>(
'/diary-note',
{schema: noteSchemePost, preHandler: checkToken},
{schema: noteSchemePost, preValidation: checkToken},
async (req, reply) => {
const {userId} = req.headers;
const {note} = req.body;
Expand Down Expand Up @@ -53,7 +53,7 @@ export const note = async (server: FastifyInstance) => {

server.patch<{ Body: INoteBodyPatch, Headers: IHeaders }>(
'/diary-note',
{schema: noteSchemePost, preHandler: checkToken},
{schema: noteSchemePost, preValidation: checkToken},
async (req, reply) => {
const {userId} = req.headers;
const {note} = req.body;
Expand Down
2 changes: 1 addition & 1 deletion app/components/mainScreen/mainScreen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface IHeaders {
export const mainScreen = async (server: FastifyInstance) => {
server.get<{ Headers: IHeaders }>(
'/mainScreen',
{schema: schemeGet, preHandler: checkToken},
{schema: schemeGet, preValidation: checkToken},
async (req, reply) => {
const {userId} = req.headers;

Expand Down
2 changes: 1 addition & 1 deletion app/components/users/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface IBodyPatch {
export const users = async (server: FastifyInstance) => {
server.patch<{ Body: IBodyPatch }>(
'/users',
{schema: schemePatch, preHandler: checkToken},
{schema: schemePatch, preValidation: checkToken},
async (req, reply) => {
const {conceptionDate, dateEnd, gestationalAge, lastMenstruationDate, userId, cycleDuration, deletePregnant} = req.body;

Expand Down
2 changes: 1 addition & 1 deletion app/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface IHeaders {
userId?: number
}
export const checkToken = async <A, B, C, RouteGeneric> (req: FastifyRequest<RouteGeneric & {Body?: IBody, Headers?: IHeaders}>, reply: FastifyReply) => {
const token = req.body?.token || req.headers?.token;
const token = req.headers?.token;

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

0 comments on commit da5de89

Please sign in to comment.