Skip to content

Commit 6b0596d

Browse files
krishnaacharyaaIzharMohammed
authored andcommitted
fix-#478: solved extensions error of .ts
2 parents e298b45 + ee63d77 commit 6b0596d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+474
-300
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ backend/node_modules
55
/node_modules
66
coverage/
77
.idea/
8+
backend/dist
89
package-lock.json
File renamed without changes.

backend/app.js backend/app.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ import compression from 'compression';
22
import cookieParser from 'cookie-parser';
33
import cors from 'cors';
44
import express from 'express';
5-
import { FRONTEND_URL } from './config/utils.js';
65
import authRouter from './routes/auth.js';
76
import postsRouter from './routes/posts.js';
87
import userRouter from './routes/user.js';
98
import errorMiddleware from './middlewares/error-middleware.js';
109
import passport from './config/passport.js';
1110
import session from 'express-session';
11+
import { FRONTEND_URL } from './config/utils.js';
1212

1313
const app = express();
1414

1515
app.use(
1616
cors({
1717
// added origin
18-
origin: [FRONTEND_URL, 'http://localhost:3000'],
18+
origin: [FRONTEND_URL as string, 'http://localhost:3000'],
1919
credentials: true,
2020
})
2121
);

backend/config/db.js backend/config/db.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import mongoose from 'mongoose';
1+
import mongoose, { AnyArray } from 'mongoose';
22
import { MONGODB_URI } from './utils.js';
33

44
export default async function connectDB() {
55
try {
6-
await mongoose.connect(MONGODB_URI, {
6+
await mongoose.connect(MONGODB_URI as string, {
77
dbName: 'wanderlust',
88
});
99
console.log(`Database connected: ${MONGODB_URI}`);
10-
} catch (err) {
10+
} catch (err: any) {
1111
console.error(err.message);
1212
process.exit(1);
1313
}

backend/config/passport.js backend/config/passport.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import passport from 'passport';
22
import { Strategy as GoogleStrategy } from 'passport-google-oauth20';
33
import User from '../models/user.js';
4-
54
passport.use(
65
new GoogleStrategy(
76
{
8-
clientID: process.env.GOOGLE_CLIENT_ID,
9-
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
7+
clientID: process.env.GOOGLE_CLIENT_ID as string,
8+
clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
109
callbackURL: `${process.env.BACKEND_URL}/api/auth/google/callback`,
1110
},
12-
async (accessToken, refreshToken, profile, done) => {
11+
async (accessToken: string, refreshToken: string, profile: any, done: any) => {
1312
try {
1413
let user = await User.findOne({ googleId: profile.id });
1514

@@ -40,7 +39,7 @@ passport.use(
4039
)
4140
);
4241

43-
passport.serializeUser((user, done) => done(null, user.id));
42+
passport.serializeUser((user: any, done) => done(null, user.id));
4443

4544
passport.deserializeUser(async (id, done) => {
4645
try {
File renamed without changes.

backend/controllers/auth-controller.js backend/controllers/auth-controller.ts

+56-22
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@ import { JWT_SECRET } from '../config/utils.js';
66
import { ApiError } from '../utils/api-error.js';
77
import { ApiResponse } from '../utils/api-response.js';
88
import { asyncHandler } from '../utils/async-handler.js';
9+
import { NextFunction, Request, Response } from 'express';
910

1011
//REGULAR EMAIL PASSWORD STRATEGY
1112
//1.Sign Up
12-
export const signUpWithEmail = asyncHandler(async (req, res) => {
13+
14+
export const signUpWithEmail = asyncHandler(async (req: Request, res: Response) => {
1315
const { userName, fullName, email, password } = req.body;
1416
if (!userName || !fullName || !email || !password) {
15-
throw new ApiError(HTTP_STATUS.BAD_REQUEST, RESPONSE_MESSAGES.COMMON.REQUIRED_FIELDS);
17+
throw new ApiError({
18+
status: HTTP_STATUS.BAD_REQUEST,
19+
message: RESPONSE_MESSAGES.COMMON.REQUIRED_FIELDS,
20+
});
1621
}
1722

1823
const existingUser = await User.findOne({
@@ -21,10 +26,16 @@ export const signUpWithEmail = asyncHandler(async (req, res) => {
2126

2227
if (existingUser) {
2328
if (existingUser.userName === userName) {
24-
throw new ApiError(HTTP_STATUS.BAD_REQUEST, RESPONSE_MESSAGES.USERS.USER_USERNAME_EXISTS);
29+
throw new ApiError({
30+
status: HTTP_STATUS.BAD_REQUEST,
31+
message: RESPONSE_MESSAGES.USERS.USER_USERNAME_EXISTS,
32+
});
2533
}
2634
if (existingUser.email === email) {
27-
throw new ApiError(HTTP_STATUS.BAD_REQUEST, RESPONSE_MESSAGES.USERS.USER_EMAIL_EXISTS);
35+
throw new ApiError({
36+
status: HTTP_STATUS.BAD_REQUEST,
37+
message: RESPONSE_MESSAGES.USERS.USER_EMAIL_EXISTS,
38+
});
2839
}
2940
}
3041

@@ -37,12 +48,15 @@ export const signUpWithEmail = asyncHandler(async (req, res) => {
3748

3849
try {
3950
await user.validate();
40-
} catch (error) {
41-
const validationErrors = [];
51+
} catch (error: any) {
52+
const validationErrors: any = [];
4253
for (const key in error.errors) {
43-
validationErrors.push(error.errors[key].message);
54+
validationErrors.push(error.errors[key].message as never);
4455
}
45-
throw new ApiError(HTTP_STATUS.BAD_REQUEST, validationErrors.join(', '));
56+
throw new ApiError({
57+
status: HTTP_STATUS.BAD_REQUEST,
58+
errors: validationErrors.join(', '),
59+
});
4660
}
4761

4862
const accessToken = await user.generateAccessToken();
@@ -71,24 +85,33 @@ export const signUpWithEmail = asyncHandler(async (req, res) => {
7185
});
7286

7387
//2.Sign In
74-
export const signInWithEmailOrUsername = asyncHandler(async (req, res) => {
88+
export const signInWithEmailOrUsername = asyncHandler(async (req: Request, res: Response) => {
7589
const { userNameOrEmail, password } = req.body;
7690
if (!userNameOrEmail || !password) {
77-
throw new ApiError(HTTP_STATUS.BAD_REQUEST, RESPONSE_MESSAGES.COMMON.REQUIRED_FIELDS);
91+
throw new ApiError({
92+
status: HTTP_STATUS.BAD_REQUEST,
93+
message: RESPONSE_MESSAGES.COMMON.REQUIRED_FIELDS,
94+
});
7895
}
7996

8097
const user = await User.findOne({
8198
$or: [{ email: userNameOrEmail }, { userName: userNameOrEmail }],
8299
}).select('+password');
83100

84101
if (!user) {
85-
throw new ApiError(HTTP_STATUS.BAD_REQUEST, RESPONSE_MESSAGES.USERS.USER_NOT_EXISTS);
102+
throw new ApiError({
103+
status: HTTP_STATUS.BAD_REQUEST,
104+
message: RESPONSE_MESSAGES.USERS.USER_NOT_EXISTS,
105+
});
86106
}
87107

88108
const isCorrectPassword = await user.isPasswordCorrect(password);
89109

90110
if (!isCorrectPassword) {
91-
throw new ApiError(HTTP_STATUS.UNAUTHORIZED, RESPONSE_MESSAGES.USERS.INVALID_PASSWORD);
111+
throw new ApiError({
112+
status: HTTP_STATUS.UNAUTHORIZED,
113+
message: RESPONSE_MESSAGES.USERS.INVALID_PASSWORD,
114+
});
92115
}
93116
const accessToken = await user.generateAccessToken();
94117
const refreshToken = await user.generateRefreshToken();
@@ -115,7 +138,7 @@ export const signInWithEmailOrUsername = asyncHandler(async (req, res) => {
115138
});
116139

117140
//Sign Out
118-
export const signOutUser = asyncHandler(async (req, res, next) => {
141+
export const signOutUser = asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
119142
await User.findByIdAndUpdate(
120143
req.user?._id,
121144
{
@@ -129,9 +152,14 @@ export const signOutUser = asyncHandler(async (req, res, next) => {
129152
);
130153

131154
// Passport.js logout
132-
req.logout((err) => {
155+
req.logout((err: any) => {
133156
if (err) {
134-
return next(new ApiError(HTTP_STATUS.INTERNAL_SERVER_ERROR, 'Logout failed'));
157+
return next(
158+
new ApiError({
159+
status: HTTP_STATUS.INTERNAL_SERVER_ERROR,
160+
message: 'Logout failed',
161+
})
162+
);
135163
}
136164

137165
res
@@ -143,7 +171,7 @@ export const signOutUser = asyncHandler(async (req, res, next) => {
143171
});
144172
});
145173
// check user
146-
export const isLoggedIn = asyncHandler(async (req, res) => {
174+
export const isLoggedIn = asyncHandler(async (req: Request, res: Response) => {
147175
let access_token = req.cookies?.access_token;
148176
let refresh_token = req.cookies?.refresh_token;
149177
const { _id } = req.params;
@@ -155,18 +183,22 @@ export const isLoggedIn = asyncHandler(async (req, res) => {
155183
}
156184
if (access_token) {
157185
try {
158-
await jwt.verify(access_token, JWT_SECRET);
186+
if (JWT_SECRET) {
187+
await jwt.verify(access_token, JWT_SECRET);
188+
}
159189
return res
160190
.status(HTTP_STATUS.OK)
161191
.json(new ApiResponse(HTTP_STATUS.OK, access_token, RESPONSE_MESSAGES.USERS.VALID_TOKEN));
162-
} catch (error) {
192+
} catch (error: any) {
163193
console.log('Access token verification error:', error.message);
164194
}
165195
}
166196
// If access token is not valid, check the refresh token
167197
if (refresh_token) {
168198
try {
169-
await jwt.verify(refresh_token, JWT_SECRET);
199+
if (JWT_SECRET) {
200+
await jwt.verify(refresh_token, JWT_SECRET);
201+
}
170202
const user = await User.findById(_id);
171203
if (!user) {
172204
return res
@@ -180,7 +212,7 @@ export const isLoggedIn = asyncHandler(async (req, res) => {
180212
.status(HTTP_STATUS.OK)
181213
.cookie('access_token', access_token, cookieOptions)
182214
.json(new ApiResponse(HTTP_STATUS.OK, access_token, RESPONSE_MESSAGES.USERS.VALID_TOKEN));
183-
} catch (error) {
215+
} catch (error: any) {
184216
console.log('Refresh token verification error:', error.message);
185217
}
186218
}
@@ -202,7 +234,9 @@ export const isLoggedIn = asyncHandler(async (req, res) => {
202234
}
203235

204236
try {
205-
await jwt.verify(refreshToken, JWT_SECRET);
237+
if (JWT_SECRET) {
238+
await jwt.verify(refreshToken, JWT_SECRET);
239+
}
206240
access_token = await user.generateAccessToken();
207241
refresh_token = await user.generateRefreshToken();
208242

@@ -213,7 +247,7 @@ export const isLoggedIn = asyncHandler(async (req, res) => {
213247
.cookie('access_token', access_token, cookieOptions)
214248
.cookie('refresh_token', refresh_token, cookieOptions)
215249
.json(new ApiResponse(HTTP_STATUS.OK, access_token, RESPONSE_MESSAGES.USERS.VALID_TOKEN));
216-
} catch (error) {
250+
} catch (error: any) {
217251
return res
218252
.status(HTTP_STATUS.UNAUTHORIZED)
219253
.json(

0 commit comments

Comments
 (0)