Skip to content

Commit

Permalink
added frontend for the site
Browse files Browse the repository at this point in the history
  • Loading branch information
Hazem020 committed Jun 21, 2024
1 parent e399208 commit a987f0c
Show file tree
Hide file tree
Showing 18 changed files with 754 additions and 60 deletions.
10 changes: 7 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ import AppError from './utils/appError.js';
import globalErrorHandler from './controllers/errorController.js';
import userRouter from './routes/userRoutes.js';
import urlRouter from './routes/urlRoutes.js';
import viewRouter from './routes/viewRoutes.js';
const app = express();
app.set('trust proxy', 2);


// 1) MIDDLEWARES
app.use(cors());
app.options('*', cors());

const __dirname = path.resolve();
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));

app.use(express.static(path.join(__dirname, 'public')));

app.use(cookieParser());
Expand Down Expand Up @@ -55,15 +58,16 @@ app.use(xss());
// })
// );


//read data from body into req.body
app.use(express.json({ limit: '10kb' }));

app.use((req, res, next) => {
req.requestTime = new Date().toISOString();
next();
});
app.use('/api/v1/users',userRouter)

app.use('/', viewRouter);
app.use('/api/v1/users', userRouter);
app.use('/api/v1/urls', urlRouter);
app.all('*', (req, res, next) => {
next(new AppError(`Can't find ${req.originalUrl} on this server!`, 404));
Expand Down
11 changes: 6 additions & 5 deletions controllers/authController.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export const login = catchAsync(async (req, res, next) => {
createAndSendToken(user, 200, req, res);
});


// exports.forgotPassword = catchAsync(async (req, res, next) => {
// const user = await User.findOne({ email: req.body.email });
// if (!user)
Expand Down Expand Up @@ -94,7 +93,7 @@ export const login = catchAsync(async (req, res, next) => {
// }
// });

export const updatePassword = catchAsync(async (req, res, next) => {
export const updatePassword = catchAsync(async (req, res, next) => {
const user = await User.findById(req.user._id).select('+password');
const correct = await user.checkPassword(
req.body.passwordCurrent,
Expand All @@ -117,7 +116,9 @@ export const auth = catchAsync(async (req, res, next) => {
} else if (req.cookies.jwt) {
token = req.cookies.jwt;
}
if (!token) return next(new AppError('You are not logged in', 401));
if (!token) {
return next(new AppError('You are not logged in', 401));
}
// 2) Verification token
const decoded = jwt.verify(token, process.env.JWT_KEY);
// 3) Check if user still exists
Expand Down Expand Up @@ -152,6 +153,7 @@ export const isLoggedIn = catchAsync(async (req, res, next) => {
return next();
}
}
res.locals.user = null;
next();
});

Expand All @@ -161,7 +163,6 @@ export const logOut = (req, res) => {
httpOnly: true,
});
res.status(200).json({
status: ' success',
status: 'success',
});
};

35 changes: 17 additions & 18 deletions controllers/urlController.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import catchAsync from '../utils/catchAsync.js';
// Helper function to create a short URL
const createShortUrl = async () => {
const shortUrl = nanoid(8);
if( await URL.findOne({ shortUrl })) {
if (await URL.findOne({ shortUrl })) {
createShortUrl();
}
return shortUrl;
Expand Down Expand Up @@ -52,7 +52,7 @@ export const getMyUrls = async (req, res) => {
const urls = await URL.find({ user: req.user.id });
res.status(200).json({
message: 'Success',
data: urls,
urls: urls,
});
} catch (err) {
return new AppError(err, 500);
Expand All @@ -77,7 +77,6 @@ export const getOriginalUrl = async (req, res) => {
}
};


// @desc Delete a short URL
// @route DELETE /api/v1/urls/:id
// @access Private
Expand All @@ -103,19 +102,19 @@ export const deleteUrl = async (req, res) => {
// @route GET /api/v1/urls/stats/:id
// @access Private
export const getStats = async (req, res) => {
try {
const url = await URL.findById(req.params.id)
if (!url) {
return res.status(404).json('No URL found');
}
if (url.user.toString() !== req.user.id) {
return res.status(401).json('Not authorized');
}
res.json({
message: 'Success',
data: url.visitors,
});
} catch (err) {
return new AppError(err, 500);
try {
const url = await URL.findById(req.params.id);
if (!url) {
return res.status(404).json('No URL found');
}
if (url.user.toString() !== req.user.id) {
return res.status(401).json('Not authorized');
}
};
res.json({
message: 'Success',
data: url.visitors,
});
} catch (err) {
return new AppError(err, 500);
}
};
2 changes: 0 additions & 2 deletions models/urlModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ const urlSchema = new mongoose.Schema({
type: Date,
default: Date.now,
},

});


// Post hook to increment visitors count when a document is retrieved
urlSchema.post('findOne', function (doc) {
if (doc) {
Expand Down
Loading

0 comments on commit a987f0c

Please sign in to comment.