Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module.exports = {
extends: '@mate-academy/eslint-config',
env: {
jest: true
jest: true,
},
rules: {
'no-proto': 0
'no-proto': 0,
},
plugins: ['jest']
};
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"license": "GPL-3.0",
"devDependencies": {
"@mate-academy/eslint-config": "latest",
"@mate-academy/scripts": "^1.8.6",
"@mate-academy/scripts": "^2.1.3",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
"eslint-plugin-node": "^11.1.0",
Expand Down
22 changes: 22 additions & 0 deletions src/controllers/auth.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// ./auth.controller.js
import { user } from '../models/user.js';

export const activate = async (req, res) => {
try {
const { token } = req.params;

const userToActivate = await user.findOne({ activationToken: token });

if (!userToActivate) {
return res.status(400).json({ message: 'Invalid token' });
}

userToActivate.isActivated = true;
userToActivate.activationToken = null;
await userToActivate.save();

res.redirect(process.env.CLIENT_HOST);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the task requirements, the user should be redirected to their profile page after successful activation. Redirecting to the base CLIENT_HOST will likely land them on the homepage. You should redirect to a more specific path, for example, ${process.env.CLIENT_HOST}/profile.

} catch (e) {
res.status(400).json({ message: 'Activation failed' });
}
};
Comment on lines +2 to +22

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This controller imports and uses a Mongoose model (user.findOne, userToActivate.save). However, the project has been migrated to PostgreSQL. This file seems to be a leftover and is not used anywhere in the application. The activation logic is handled directly in src/routes/auth.js. This file should be removed.

Comment on lines +1 to +22

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This controller file seems to be a leftover from a previous MongoDB setup and is no longer used in the current PostgreSQL implementation. It should be removed from the project.

11 changes: 11 additions & 0 deletions src/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// db.js
// backend/db.js
import pkg from 'pg';

const { Pool } = pkg;

const pool = new Pool({
connectionString: process.env.DATABASE_URL,
});

export default pool;
27 changes: 26 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
'use strict';
// index.js
import 'dotenv/config';
import express from 'express';
import cors from 'cors';
import authRoutes from './routes/auth.js';

const app = express();

app.use(
cors({
origin: ['http://localhost:3000', 'http://192.168.1.24:3000'],
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization'],
}),
);

app.use(express.json());

app.use('/api/auth', authRoutes);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The application is still missing a handler for undefined routes. According to the requirements, you need to show a 404 page for any route that doesn't match. You should add a catch-all middleware here, after all other routes have been defined, to handle this.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the requirements, you need to implement a 404 handler for all undefined routes. You should add a catch-all route here, after all other routes have been defined, to handle any requests that don't match an existing endpoint.

const PORT = process.env.PORT || 5000;

app.listen(PORT, () => {
// console.log(`Server running on http://localhost:${PORT}`);
});
29 changes: 29 additions & 0 deletions src/middleware/auth.middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import jwt from 'jsonwebtoken';

export default function authMiddleware(req, res, next) {
try {
const authHeader = req.headers.authorization;

if (!authHeader) {
return res.status(401).json({ message: 'NO_TOKEN' });
}

if (!authHeader.startsWith('Bearer ')) {
return res.status(401).json({ message: 'INVALID_TOKEN_FORMAT' });
}

const token = authHeader.split(' ')[1];

if (!token) {
return res.status(401).json({ message: 'TOKEN_MISSING' });
}

const decoded = jwt.verify(token, process.env.JWT_SECRET);

req.user = decoded;

next();
} catch (e) {
return res.status(401).json({ message: 'INVALID_TOKEN' });
}
}
13 changes: 13 additions & 0 deletions src/models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// users.js //

import mongoose from 'mongoose';

const userSchema = new mongoose.Schema({
email: { type: String, required: true, unique: true },
password: { type: String, required: true },

isActivated: { type: Boolean, default: false },
activationToken: { type: String },
});

export const user = mongoose.model('User', userSchema);
Comment on lines +1 to +13

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This project uses PostgreSQL, but this file defines a Mongoose schema for MongoDB. This was a critical issue identified in the previous review. Please remove this file as it is not compatible with the project's database setup.

Comment on lines +1 to +13

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This Mongoose model is a remnant from the previous MongoDB implementation. Since the application now uses PostgreSQL, this file is no longer necessary and should be removed from the project.

Loading