Skip to content

Commit

Permalink
Add refreshAccessToken endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
hiteshchoudhary committed Dec 17, 2023
1 parent 591c8f7 commit 0277c88
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
52 changes: 51 additions & 1 deletion src/controllers/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {ApiError} from "../utils/ApiError.js"
import { User} from "../models/user.model.js"
import {uploadOnCloudinary} from "../utils/cloudinary.js"
import { ApiResponse } from "../utils/ApiResponse.js";
import jwt from "jsonwebtoken"


const generateAccessAndRefereshTokens = async(userId) =>{
Expand Down Expand Up @@ -181,8 +182,57 @@ const logoutUser = asyncHandler(async(req, res) => {
.json(new ApiResponse(200, {}, "User logged Out"))
})

const refreshAccessToken = asyncHandler(async (req, res) => {
const incomingRefreshToken = req.cookies.refreshToken || req.body.refreshToken

if (incomingRefreshToken) {
throw new ApiError(401, "unauthorized request")
}

try {
const decodedToken = jwt.verify(
incomingRefreshToken,
process.env.REFRESH_TOKEN_SECRET
)

const user = await User.findById(decodedToken?._id)

if (!user) {
throw new ApiError(401, "Invalid refresh token")
}

if (incomingRefreshToken !== user?.refreshToken) {
throw new ApiError(401, "Refresh token is expired or used")

}

const options = {
httpOnly: true,
secure: true
}

const {accessToken, newRefreshToken} = await generateAccessAndRefereshTokens(user._id)

return res
.status(200)
.cookie("accessToken", accessToken, options)
.cookie("refreshToken", newRefreshToken, options)
.json(
new ApiResponse(
200,
{accessToken, refreshToken: newRefreshToken},
"Access token refreshed"
)
)
} catch (error) {
throw new ApiError(401, error?.message || "Invalid refresh token")
}

})

export {
registerUser,
loginUser,
logoutUser
logoutUser,
refreshAccessToken
}
2 changes: 1 addition & 1 deletion src/middlewares/auth.middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const verifyJWT = asyncHandler(async(req, _, next) => {
const user = await User.findById(decodedToken?._id).select("-password -refreshToken")

if (!user) {
// NEXT_VIDEO: discuss about frontend

throw new ApiError(401, "Invalid Access Token")
}

Expand Down
4 changes: 2 additions & 2 deletions src/routes/user.routes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Router } from "express";
import { loginUser, logoutUser, registerUser } from "../controllers/user.controller.js";
import { loginUser, logoutUser, registerUser, refreshAccessToken } from "../controllers/user.controller.js";
import {upload} from "../middlewares/multer.middleware.js"
import { verifyJWT } from "../middlewares/auth.middleware.js";

Expand All @@ -24,6 +24,6 @@ router.route("/login").post(loginUser)

//secured routes
router.route("/logout").post(verifyJWT, logoutUser)

router.route("/refresh-token").post(refreshAccessToken)

export default router

0 comments on commit 0277c88

Please sign in to comment.