Skip to content

Commit

Permalink
bug fixes and nginx conf change
Browse files Browse the repository at this point in the history
  • Loading branch information
farooqpk committed Sep 22, 2024
1 parent 1c70899 commit 8f70ad3
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 16 deletions.
2 changes: 1 addition & 1 deletion nginx/app.conf
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ server {
index index.html;

# Cache static files
location ~* \.(html|jpg|jpeg|png|webp|ico|css|js|mp3)$ {
location ~* \.(jpg|jpeg|png|webp|ico|css|js|mp3)$ {
expires 30d;
add_header Cache-Control "public, no-transform";
}
Expand Down
8 changes: 7 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const {
R2_BUCKET_NAME,
NODE_ENV,
GEMINI_API_KEY,
ACCESS_TOKEN_EXPIRY,
REFRESH_TOKEN_EXPIRY,
} = process.env;

if (
Expand All @@ -23,7 +25,9 @@ if (
!R2_SECRET_KEY ||
!R2_BUCKET_NAME ||
!NODE_ENV ||
!GEMINI_API_KEY
!GEMINI_API_KEY ||
!ACCESS_TOKEN_EXPIRY ||
!REFRESH_TOKEN_EXPIRY
) {
throw new Error("Missing environment variables");
}
Expand All @@ -40,4 +44,6 @@ export {
R2_BUCKET_NAME,
NODE_ENV,
GEMINI_API_KEY,
ACCESS_TOKEN_EXPIRY,
REFRESH_TOKEN_EXPIRY,
};
1 change: 0 additions & 1 deletion src/controllers/auth/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export const login = async (req: Request, res: Response) => {

return res.status(200).send({
success: true,
message: "User logged in successfully",
loginToken,
user: {
userId: isUserNameAlreadyExist.userId,
Expand Down
20 changes: 14 additions & 6 deletions src/controllers/auth/loginToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { CookieOptions, Request, Response } from "express";
import { clearFromRedis, getDataFromRedis } from "../../redis";
import { prisma } from "../../utils/prisma";
import { createJwtToken } from "../../utils/createJwtToken";
import {NODE_ENV } from "../../config";
import {
ACCESS_TOKEN_EXPIRY,
NODE_ENV,
REFRESH_TOKEN_EXPIRY,
} from "../../config";
import dayjs from "dayjs";

export const loginToken = async (req: Request, res: Response) => {
Expand Down Expand Up @@ -63,17 +67,21 @@ export const loginToken = async (req: Request, res: Response) => {
"refresh"
);

const cookieOptions: CookieOptions = {
httpOnly: true
};
const cookieOptions: CookieOptions = {
httpOnly: true,
};

res.cookie("accesstoken", accesstoken, {
...cookieOptions,
expires: dayjs().add(1, "hours").toDate(),
expires: dayjs()
.add(parseInt(ACCESS_TOKEN_EXPIRY || "2"), "hours")
.toDate(),
});
res.cookie("refreshtoken", refreshtoken, {
...cookieOptions,
expires: dayjs().add(14, "days").toDate(),
expires: dayjs()
.add(parseInt(REFRESH_TOKEN_EXPIRY || "30"), "days")
.toDate(),
});

return res.status(200).json({
Expand Down
14 changes: 11 additions & 3 deletions src/controllers/auth/signup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { createJwtToken } from "../../utils/createJwtToken";
import { prisma } from "../../utils/prisma";
import * as bcrypt from "bcrypt";
import { clearFromRedis } from "../../redis";
import { NODE_ENV } from "../../config";
import {
ACCESS_TOKEN_EXPIRY,
REFRESH_TOKEN_EXPIRY,
} from "../../config";
import dayjs from "dayjs";

export const signup = async (req: Request, res: Response) => {
Expand All @@ -26,6 +29,7 @@ export const signup = async (req: Request, res: Response) => {
}

const hashedPassword = await bcrypt.hash(password, 10);

const user = await prisma.user.create({
data: {
username,
Expand Down Expand Up @@ -61,11 +65,15 @@ export const signup = async (req: Request, res: Response) => {

res.cookie("accesstoken", accesstoken, {
...cookieOptions,
expires: dayjs().add(1, "hours").toDate(),
expires: dayjs()
.add(parseInt(ACCESS_TOKEN_EXPIRY || "2"), "hours")
.toDate(),
});
res.cookie("refreshtoken", refreshtoken, {
...cookieOptions,
expires: dayjs().add(14, "days").toDate(),
expires: dayjs()
.add(parseInt(REFRESH_TOKEN_EXPIRY || "30"), "days")
.toDate(),
});

return res.status(201).send({
Expand Down
15 changes: 11 additions & 4 deletions src/utils/createJwtToken.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { Prisma } from "@prisma/client";
import jwt from "jsonwebtoken";
import { ACCESS_TOKEN_SECRET, REFRESH_TOKEN_SECRET } from "../config";
import {
ACCESS_TOKEN_SECRET,
REFRESH_TOKEN_SECRET,
ACCESS_TOKEN_EXPIRY,
REFRESH_TOKEN_EXPIRY,
} from "../config";

export const createJwtToken = (
userId: Prisma.UserCreateInput["userId"],
userId: string,
username: string,
publicKey: string,
tokenType: "access" | "refresh"
Expand All @@ -12,7 +16,10 @@ export const createJwtToken = (
{ userId, username, publicKey },
tokenType === "access" ? ACCESS_TOKEN_SECRET! : REFRESH_TOKEN_SECRET!,
{
expiresIn: tokenType === "access" ? "1h" : "14d",
expiresIn:
tokenType === "access"
? `${ACCESS_TOKEN_EXPIRY}h`
: `${REFRESH_TOKEN_EXPIRY}d`,
}
);
};

0 comments on commit 8f70ad3

Please sign in to comment.