-
Notifications
You must be signed in to change notification settings - Fork 303
Solution #229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Solution #229
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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'] | ||
| }; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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); | ||
| } catch (e) { | ||
| res.status(400).json({ message: 'Activation failed' }); | ||
| } | ||
| }; | ||
|
Comment on lines
+2
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This controller imports and uses a Mongoose model (
Comment on lines
+1
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| 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; |
| 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); | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}`); | ||
| }); | ||
| 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' }); | ||
| } | ||
| } |
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
There was a problem hiding this comment.
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_HOSTwill likely land them on the homepage. You should redirect to a more specific path, for example,${process.env.CLIENT_HOST}/profile.