Deno Alosaur JTW Authentication
As of this year, I have script NodeJS as my go to Backend in favour of Deno, and Alosaur the perfect replacement for Express/NestJs.
Let’s be clear, both Deno and Alosaur is relatively new so it’s still in development stages. However, you can use today thanks to the recently added security module and Deno Redis integration.
Requirements
Deno 1.6
Docker
Out of the Box Alosaur supports both “JWT” and “Cookies”, but only cookies are really ready to use. So with a few tweaks you can use JTW + redis as the session storage.
First you need to setup Redis, if you already have a redis server then skip this step. But here how to set it up wio
version: "3.2"services:
redis:
container_name: redis
image: redis
restart: always
ports:
- "6379:6379"
Next we need to implement Alosaur SessionStore using Redis
Next create a JTW.ts file. This will implement the Authentication Scheme
Finally to register the security module your application code should look like the following
const app = new App({areas: [AuthenticationArea],logging: false,});const sessionStore = new RedisSession('127.0.0.1', 6379);await sessionStore.init();const authMiddleware = new AuthMiddleware( [JWTSchema],);const sessionMiddleware = new SessionMiddleware(sessionStore,{ secret: 1122n, maxAge: DAYS_30, path: "/" },);app.use(new RegExp("/"), sessionMiddleware);app.use(new RegExp("/"), authMiddleware);app.useSecurityContext();app.listen();
You can find an working example on github: Jordan-Hall/alosaur-jwt-redis: Example of using Deno Alosaur and JTW with help from Redis (github.com)