Skip to content

Commit

Permalink
loginToken api for getting tokens after login
Browse files Browse the repository at this point in the history
  • Loading branch information
farooqpk committed Jun 5, 2024
1 parent a23c681 commit 7a57264
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 16 deletions.
31 changes: 15 additions & 16 deletions src/controllers/auth/login.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CookieOptions, Request, Response } from "express";
import { createJwtToken } from "../../utils/createJwtToken";
import { Request, Response } from "express";
import { prisma } from "../../utils/prisma";
import * as bcrypt from "bcrypt";
import { setDataInRedis } from "../../redis";

export const login = async (req: Request, res: Response) => {
try {
Expand Down Expand Up @@ -33,24 +33,23 @@ export const login = async (req: Request, res: Response) => {
});
}

const acessToken = createJwtToken(
isUserNameAlreadyExist.userId,
isUserNameAlreadyExist.username,
isUserNameAlreadyExist.publicKey,
"access"
);
const refreshToken = createJwtToken(
isUserNameAlreadyExist.userId,
isUserNameAlreadyExist.username,
isUserNameAlreadyExist.publicKey,
"refresh"
);
// create random token for identifying user
const loginToken =
Math.random().toString(36).substring(2, 15) +
Math.random().toString(36).substring(2, 15);

// store that login token in redis for 5 minutes
await setDataInRedis({
key: `loginToken:${isUserNameAlreadyExist.userId}`,
data: loginToken,
expirationTimeInSeconds: 300,
isString: true,
});

return res.status(200).send({
success: true,
message: "User logged in successfully",
accesstoken: acessToken,
refreshtoken: refreshToken,
loginToken,
user: {
userId: isUserNameAlreadyExist.userId,
username: isUserNameAlreadyExist.username,
Expand Down
70 changes: 70 additions & 0 deletions src/controllers/auth/loginToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { Request, Response } from "express";
import { getDataFromRedis } from "../../redis";
import { prisma } from "../../utils/prisma";
import { createJwtToken } from "../../utils/createJwtToken";

export const loginToken = async (req: Request, res: Response) => {
try {
const userId = req.body?.userId;
const token = req.body?.loginToken;

if (!token || !userId) {
return res.status(400).json({
success: false,
message: "token or userId is missing",
});
}

const user = await prisma.user.findUnique({
where: {
userId,
},
select: {
userId: true,
username: true,
publicKey: true,
},
});

if (!user) {
return res.status(404).json({
success: false,
message: "user not found",
});
}

const storedLoginToken = await getDataFromRedis(
`loginToken:${userId}`,
true
);

if (!storedLoginToken || storedLoginToken !== token) {
return res.status(400).json({
success: false,
message: "login token is invalid",
});
}

const accesstoken = createJwtToken(
user.userId,
user.username,
user.publicKey,
"access"
);
const refreshtoken = createJwtToken(
user.userId,
user.username,
user.publicKey,
"refresh"
);

return res.status(200).json({
success: true,
message: "login token verified successfully",
accesstoken,
refreshtoken,
});
} catch (error) {
res.status(500).json(error);
}
};
4 changes: 4 additions & 0 deletions src/routes/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ import {
signupSchema,
updateUsernameSchema,
} from "../schemas/authSchema";
import { loginToken } from "../controllers/auth/loginToken";

export const authRouter: Router = Express.Router();

authRouter.post("/signup", validateData(signupSchema), signup);

authRouter.post("/login", validateData(loginSchema), login);

// request for access and refresh token after login
authRouter.post("/login/token", loginToken);

authRouter.get("/verifyRoute", verifyRoute);

authRouter.post("/refresh", createAccessTokenFromRefreshToken);
Expand Down

0 comments on commit 7a57264

Please sign in to comment.