From a4194ace7ec09e0fe1054469305cfec3c509c2fe Mon Sep 17 00:00:00 2001 From: gabrielba15 Date: Sat, 3 Apr 2021 20:01:29 -0400 Subject: [PATCH] resolviendo problemas de compatibilidad en la api modified: routes.ts modified: server.ts modified: userMiddleware.ts --- api/routes.ts | 4 ++-- api/server.ts | 2 +- api/userMiddleware.ts | 35 ++++++++++++++++++++--------------- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/api/routes.ts b/api/routes.ts index 80aada8..76e8a7c 100644 --- a/api/routes.ts +++ b/api/routes.ts @@ -1,5 +1,5 @@ import { RouterContext } from "https://deno.land/x/oak@v5.0.0/mod.ts"; -import { hashSync, compareSync } from "https://deno.land/x/bcrypt@v0.2.1/mod.ts"; +import { hashSync, compareSync } from "https://deno.land/x/bcrypt@v0.2.2/mod.ts"; import { makeJwt, setExpiration, Jose } from "https://deno.land/x/djwt@v0.9.0/create.ts"; import { users, User } from './users.ts'; import { favs } from './favs.ts' @@ -101,4 +101,4 @@ export const postRegister = async (ctx: RouterContext) => { ctx.response.status = 201 } -} \ No newline at end of file +} diff --git a/api/server.ts b/api/server.ts index b01db4c..1319ffa 100644 --- a/api/server.ts +++ b/api/server.ts @@ -1,6 +1,6 @@ import { Application, Router } from "https://deno.land/x/oak@v5.0.0/mod.ts" import { oakCors } from "https://deno.land/x/cors/mod.ts" -import "https://deno.land/x/dotenv@v0.4.1/load.ts" +// import "https://deno.land/x/dotenv@v0.4.1/load.ts" import * as flags from 'https://deno.land/std/flags/mod.ts' import { userMiddleware } from "./userMiddleware.ts" diff --git a/api/userMiddleware.ts b/api/userMiddleware.ts index 24528a9..f4fab73 100644 --- a/api/userMiddleware.ts +++ b/api/userMiddleware.ts @@ -1,30 +1,35 @@ -import { Context } from "https://deno.land/x/oak@v5.0.0/mod.ts"; -import { validateJwt } from "https://deno.land/x/djwt/validate.ts" -import { users, User } from "./users.ts"; +import { Context } from 'https://deno.land/x/oak@v5.0.0/mod.ts'; +import { validateJwt } from 'https://deno.land/x/djwt@v1.5/validate.ts'; +import { users, User } from './users.ts'; const userMiddleware = async (ctx: Context, next: Function) => { // Get JWT from request if available - const { value = {} } = await ctx.request.body(); - let {jwt} = value - + const { value = {} } = await ctx.request.body(); + let { jwt } = value; + if (!jwt) { - jwt = ctx.request.headers.get('Authorization') + jwt = ctx.request.headers.get('Authorization'); } - console.log('using: ', {jwt}) + console.log('using: ', { jwt }); + // const key = Deno.env.get('JWT_KEY'); + const key = ''; + const algorithm = "HS512"// algorithm if (jwt) { // Validate JWT and if it is invalid delete from cookie - const data: any = await validateJwt(jwt, Deno.env.get('JWT_KEY') || ''); - + const data: any = await validateJwt({jwt, key, algorithm}); + if (!data.isValid || data.isExpired) { ctx.cookies.delete('jwt'); - ctx.response.status = 401 + ctx.response.status = 401; } else if (data) { // If it is valid select user and save in context state - const user: any = users.find((u: User) => u.username === data.payload.iss); + const user: any = users.find( + (u: User) => u.username === data.payload.iss + ); ctx.state.currentUser = user; - console.log('found', {user}) + console.log('found', { user }); await next(); } else { ctx.cookies.delete('jwt'); @@ -34,6 +39,6 @@ const userMiddleware = async (ctx: Context, next: Function) => { ctx.state.currentUser = null; await next(); } -} +}; -export {userMiddleware}; \ No newline at end of file +export { userMiddleware };