Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions api/routes.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -101,4 +101,4 @@ export const postRegister = async (ctx: RouterContext) => {
ctx.response.status = 201
}

}
}
2 changes: 1 addition & 1 deletion api/server.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
35 changes: 20 additions & 15 deletions api/userMiddleware.ts
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -34,6 +39,6 @@ const userMiddleware = async (ctx: Context, next: Function) => {
ctx.state.currentUser = null;
await next();
}
}
};

export {userMiddleware};
export { userMiddleware };