Day 8 - 60 Days of Learning 2025

2 min read

User Refresh Access Token Controller and Song Model
On June 8, 2025, I created controller for refreshing the user access token when it gets expired or on command. Also, started to work on Song entity by creating types and model for it.
User Refresh Access Token Controller
Create a controller for refreshing user access token
Inside user.controller.ts
file
// Refresh access token for user
const refreshAccessToken = asyncHandler(async (req, res) => {
// Get refresh token from cookies or request object
const incomingRefreshToken =
req.cookies?.refreshToken || req.body?.refreshToken;
// Check the received refresh token
if (!incomingRefreshToken) {
throw new APIError(401, "Unauthorized request");
}
// Decode the refresh token
if (!refreshTokenSecret) {
throw new APIError(400, "JWT configurations are missing");
}
const decodedRefreshToken = jwt.verify(
incomingRefreshToken,
refreshTokenSecret
) as JwtPayload;
// Check decoded refresh token
if (!decodedRefreshToken) {
throw new APIError(
500,
"Something went wrong while decoding refresh token"
);
}
// Get user from database
const user: UserType | null = await User.findById(decodedRefreshToken?._id);
// Check user
if (!user) {
throw new APIError(401, "Invalid refresh token");
}
// Generate new tokens
const { accessToken, refreshToken } = await generateTokens(user?._id);
// Send back response
res
.status(200)
.cookie("accessToken", accessToken, cookiesOptions)
.cookie("refreshToken", refreshToken, cookiesOptions)
.json(
new APIResponse(200, "Refreshed access token successfully", {
accessToken,
refreshToken,
})
);
});
Types and Model for Song Entity
Types for song entity
inside song.type.ts
file
import mongoose from "mongoose";
export interface SongType {
_id: mongoose.Types.ObjectId;
name: string;
songFile: string;
duration: number;
artist: mongoose.Types.ObjectId;
image: string;
isPublished: boolean;
createdAt: Date;
updatedAt: Date;
}
export type CreateSongType = Omit<
SongType,
"_id" | "createdAt" | "updatedAt"
>;
Used the types of song to create model for song entity
inside song.model.ts
file
import mongoose from "mongoose";
import { CreateSongType } from "../types/song.type";
const songSchema = new mongoose.Schema<CreateSongType>(
{
name: {
type: String,
required: [true, "Song name is required"],
trim: true,
},
songFile: {
type: String,
required: [true, "Song file is required"],
},
duration: {
type: Number,
required: [true, "Song duration is required"],
default: 0,
},
artist: {
type: mongoose.SchemaTypes.ObjectId,
ref: "User",
required: [true, "Song artist is required"],
},
image: {
type: String,
required: [true, "Song image is required"],
},
isPublished: {
type: Boolean,
required: [true, "Song published status is required"],
default: false,
},
},
{ timestamps: true }
);
export const Song = mongoose.model("Song", songSchema);
After this going to work on controllers for Song entity.
0
Subscribe to my newsletter
Read articles from Ashmin Bhujel directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Ashmin Bhujel
Ashmin Bhujel
Learner | Software Developer | TypeScript | Node.js | React.js | MongoDB | Docker