-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
loginToken api for getting tokens after login
- Loading branch information
Showing
3 changed files
with
89 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters