Skip to content

Commit

Permalink
add session auth
Browse files Browse the repository at this point in the history
  • Loading branch information
avibn committed Feb 5, 2024
1 parent 47902ab commit 2814420
Show file tree
Hide file tree
Showing 10 changed files with 283 additions and 14 deletions.
20 changes: 19 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@ services:
ports:
- ${PGADMIN_PORT}:80

redis:
image: redis:7.2.4
restart: always
ports:
- ${REDIS_PORT}:6379
command: redis-server --save 20 1 --loglevel warning --requirepass ${REDIS_PASSWORD}
volumes:
- redis-cache:/data

redisInsight:
container_name: redis-insight
image: redislabs/redisinsight
restart: always
ports:
- ${REDIS_INSIGHT_PORT}:8001

volumes:
postgres-data-volume:
external: true
external: true
redis-cache:
external: true
7 changes: 7 additions & 0 deletions server/@types/session.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import "express-session";

declare module "express-session" {
interface SessionData {
userId: string | undefined;
}
}
156 changes: 156 additions & 0 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"license": "ISC",
"devDependencies": {
"@types/express": "^4.17.21",
"@types/express-session": "^1.17.10",
"@types/http-errors": "^2.0.4",
"@types/morgan": "^1.9.9",
"@types/node": "^20.11.16",
Expand All @@ -26,11 +27,14 @@
"dependencies": {
"@prisma/client": "^5.9.1",
"argon2": "^0.31.2",
"connect-redis": "^7.1.1",
"dotenv": "^16.4.1",
"envalid": "^8.0.0",
"express": "^4.18.2",
"express-session": "^1.18.0",
"http-errors": "^2.0.0",
"morgan": "^1.10.0",
"redis": "^4.6.12",
"zod": "^3.22.4"
}
}
35 changes: 35 additions & 0 deletions server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,49 @@ import "dotenv/config";
import createHttpError, { isHttpError } from "http-errors";
import express, { NextFunction, Request, Response } from "express";

import RedisStore from "connect-redis";
import { ZodError } from "zod";
import { createClient } from "redis";
import env from "./utils/validateEnv";
import morgan from "morgan";
import session from "express-session";
import usersRouter from "./routes/users";

const app = express();

// Redis
const redisClient = createClient({
url: env.REDIS_URL,
});
redisClient.connect().catch(console.error);

// Initialize redis store
const redisStore = new RedisStore({
client: redisClient,
prefix: "lanten:",
});

// Middlewares
app.use(morgan("dev"));
app.use(express.json());

// Session middleware with redis
app.use(
session({
store: redisStore,
resave: false,
saveUninitialized: false,
secret: env.SESSION_SECRET,
rolling: true,
cookie: {
maxAge: 1000 * 60 * 60 * 24 * 5, // 15 days
httpOnly: true,
secure: false,
sameSite: "lax",
},
})
);

// Endpoints
app.get("/", (req, res) => {
res.send("Hello World");
Expand All @@ -20,6 +54,7 @@ app.get("/", (req, res) => {
// Routers
app.use("/users", usersRouter);

// Error handling
app.use((req, res, next) => {
next(createHttpError(404, "Endpoint not found"));
});
Expand Down
Loading

0 comments on commit 2814420

Please sign in to comment.