Session middleware for Oak
For some reason while using deno test usecases are unable to use variables stored as cookies . They always return undefined. Test File: ```typescript import { superoak } from "https://deno.land/x/[email protected]/mod.ts"; import { afterAll, afterEach, beforeAll, describe, it } from "@std/testing/bdd" import process from "node:process"; import mongoose from "mongoose"; import { app } from "../main.ts"; beforeAll(async () => { await mongoose.connect('mongodb://localhost:27017/auth-test') process.env.JWT_KEY = 'SUPER_SECRET' }) afterAll(async () => { await mongoose.connection.destroy() }) afterEach(async () => { await mongoose.connection.db?.dropDatabase() }) describe('the current user flow', () => { it('should return user and cookie in header', async () => { const controller = new AbortController(); const { signal } = controller app.listen({ port: 5000, signal }) const signup = await fetch('http://localhost:5000/api/users/signup', { method: 'POST', body: JSON.stringify({ email: '[email protected]', password: '123456' }) }) await signup.json() console.log('SIGNUP SERVER RESPONSE BODY IS: ', signup.headers.getSetCookie().slice(0, 1)) console.log('SIGNUP SERVER RESPONSE IS: ', signup.status) const currentUser = await fetch('http://localhost:5000/api/users/currentUser', { method: 'GET', headers: { 'Cookie': signup.headers.getSetCookie().slice(0, 1) } }) const bodyCurrentUser = await currentUser.json() console.log('currentUser SERVER RESPONSE BODY IS: ', bodyCurrentUser) console.log('currentUser SERVER RESPONSE IS: ', currentUser.status) controller.abort() }) }) ``` Middleware ```typescript import { Context, createHttpError, Status, type Middleware } from "@oak/oak"; import { Session } from "https://deno.land/x/[email protected]/mod.ts"; import jwt from 'jsonwebtoken' import process from "node:process"; export const authHandler: Middleware<{ session: Session }> = async (ctx: Context<{ session: Session }>, next) => { try { const parsedJTW = await ctx.state.session.get('jwt') as string console.log('PARSED_JWT IS UNDEFINED: ', parsedJTW) if (!parsedJTW) { throw Error('Provide a valid authentication') } const token = parsedJTW.replace('Bearer ', "") const isLogged = jwt.verify(token, process.env.JWT_KEY) if (!isLogged) { throw createHttpError(Status.Unauthorized, 'Provide a valid authentication.') } ctx.response.body = isLogged await next() // deno-lint-ignore no-explicit-any } catch (error: any) { ctx.response.status = Status.Unauthorized ctx.response.body = { message: error.message } } } ```
This issue appears to be discussing a feature request or bug report related to the repository. Based on the content, it seems to be resolved. The issue was opened by jefersoneiji and has received 1 comments.