-
Notifications
You must be signed in to change notification settings - Fork 374
task solution #282
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?
task solution #282
Changes from all commits
b635d12
7c4470f
61570d0
55b0b12
067d5aa
33c59b3
11f7952
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 |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| module.exports = { | ||
| extends: [ | ||
| '@mate-academy/eslint-config', | ||
| 'plugin:@typescript-eslint/recommended', | ||
| ], | ||
| parser: '@typescript-eslint/parser', | ||
| plugins: ['jest', '@typescript-eslint'], | ||
| env: { | ||
| jest: true, | ||
| node: true, | ||
| }, | ||
| ignorePatterns: ['dist/', 'node_modules/', 'src/generated/'], | ||
| rules: { | ||
| 'no-console': 'off', | ||
| 'no-shadow': 'off', | ||
| indent: 'off', | ||
| 'no-proto': 0, | ||
| }, | ||
| }; |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,5 @@ node_modules | |
| # MacOS | ||
| .DS_Store | ||
|
|
||
| # env files | ||
| *.env | ||
| .env* | ||
| .env | ||
| src/generated | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| export class AppError extends Error { | ||
| statusCode: number; | ||
| details?: unknown; | ||
|
|
||
| constructor(statusCode: number, message: string, details?: unknown) { | ||
| super(message); | ||
|
|
||
| this.name = 'AppError'; | ||
| this.statusCode = statusCode; | ||
| this.details = details; | ||
| } | ||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import express from 'express'; | ||
| import cors from 'cors'; | ||
| import dotenv from 'dotenv'; | ||
| import { errorMiddleware } from './middlewares/error.middleware.js'; | ||
| import { AppError } from './errors/AppError.js'; | ||
| import { authRoutes } from './modules/auth/auth.routes.js'; | ||
| import { profileRoutes } from './modules/profile/profile.routes.js'; | ||
| import cookieParser from 'cookie-parser'; | ||
| import { pagesRoutes } from './modules/pages/pages.routes.js'; | ||
|
|
||
| dotenv.config(); | ||
|
|
||
| const app = express(); | ||
|
|
||
| const PORT = process.env.PORT ?? 3000; | ||
|
|
||
| app.use(cors()); | ||
| app.use(express.json()); | ||
|
|
||
| app.use(express.urlencoded({ extended: true })); | ||
| app.use(cookieParser()); | ||
|
|
||
| app.use(pagesRoutes); | ||
|
|
||
| // | ||
| app.use('/api/auth', authRoutes); | ||
| app.use('/api/profile', profileRoutes); | ||
| // | ||
|
|
||
| app.use((_req, _res, next) => { | ||
| next(new AppError(404, 'Route not found')); | ||
| }); | ||
|
|
||
| app.use(errorMiddleware); | ||
|
Comment on lines
+32
to
+34
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 task requires 'redirect to Login after logout', but this returns 204 No Content. Should use |
||
|
|
||
| app.listen(PORT, () => { | ||
| console.log(`Server is running on http://localhost:${PORT}`); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import 'dotenv/config'; | ||
|
|
||
| import { PrismaClient } from '../generated/prisma/client.js'; | ||
| import { PrismaPg } from '@prisma/adapter-pg'; | ||
|
|
||
| const adapter = new PrismaPg({ | ||
| connectionString: process.env.DATABASE_URL!, | ||
| }); | ||
|
|
||
| export const prisma = new PrismaClient({ | ||
| adapter, | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import type { RequestHandler } from 'express'; | ||
| import type { ParamsDictionary, Query } from 'express-serve-static-core'; | ||
|
|
||
| export const asyncHandler = < | ||
| Params extends ParamsDictionary = ParamsDictionary, | ||
| ResBody = unknown, | ||
| ReqBody = unknown, | ||
| ReqQuery extends Query = Query, | ||
| >( | ||
| handler: RequestHandler<Params, ResBody, ReqBody, ReqQuery>, | ||
| ): RequestHandler<Params, ResBody, ReqBody, ReqQuery> => { | ||
| return (req, res, next) => { | ||
| Promise.resolve(handler(req, res, next)).catch(next); | ||
| }; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import type { RequestHandler } from 'express'; | ||
| import { AppError } from '../errors/AppError.js'; | ||
| import { validateAccessToken } from '../utils/jwt.js'; | ||
|
|
||
| export const authMiddleware: RequestHandler = (req, _res, next) => { | ||
| const authHeader = req.headers.authorization; | ||
| const [type, headerToken] = authHeader?.split(' ') || []; | ||
|
|
||
| const token = | ||
| type === 'Bearer' && headerToken ? headerToken : req.cookies?.accessToken; | ||
|
|
||
| if (!token) { | ||
| throw new AppError(401, 'Unauthorized'); | ||
| } | ||
|
|
||
| const userData = validateAccessToken(token); | ||
|
|
||
| if (!userData) { | ||
| throw new AppError(401, 'Invalid or expired token'); | ||
| } | ||
|
|
||
| req.user = userData; | ||
|
|
||
| next(); | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import type { ErrorRequestHandler } from 'express'; | ||
| import { AppError } from '../errors/AppError.js'; | ||
|
|
||
| export const errorMiddleware: ErrorRequestHandler = (error, _req, res) => { | ||
| if (error instanceof AppError) { | ||
| return res.status(error.statusCode).json({ | ||
| error: { | ||
| message: error.message, | ||
| details: error.details, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| console.error(error); | ||
|
|
||
| return res.status(500).json({ | ||
| error: { | ||
| message: 'Internal Server Error', | ||
| }, | ||
| }); | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import type { RequestHandler } from 'express'; | ||
| import { authService } from './auth.service.js'; | ||
|
|
||
| type ActivateParams = { | ||
| token: string; | ||
| }; | ||
|
|
||
| type ResetPasswordParams = { | ||
| token: string; | ||
| }; | ||
|
|
||
| const register: RequestHandler = async (req, res) => { | ||
| const user = await authService.register(req.body); | ||
|
|
||
| res.status(201).json({ user }); | ||
| }; | ||
|
|
||
| const activate: RequestHandler<ActivateParams> = async (req, res) => { | ||
| const { token } = req.params; | ||
|
|
||
| const user = await authService.activate(token); | ||
|
|
||
| res.json({ | ||
| user, | ||
| message: 'Account activated successfully', | ||
| }); | ||
|
Comment on lines
+17
to
+26
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 task requires 'redirect to Profile after activation'. This returns JSON instead of redirecting. Should use |
||
| }; | ||
|
|
||
| const login: RequestHandler = async (req, res) => { | ||
| const authData = await authService.login(req.body); | ||
|
|
||
| res.json(authData); | ||
|
Comment on lines
+28
to
+32
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 task requires 'Redirect to profile after login'. This returns JSON instead of redirecting. Should use |
||
| }; | ||
|
|
||
| const logout: RequestHandler = async (req, res) => { | ||
| res.status(204).send(); | ||
|
Comment on lines
+34
to
+36
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 task requires 'Redirect to login after logging out'. Returning 204 No Content doesn't satisfy this. Should use |
||
| }; | ||
|
|
||
| const forgotPassword: RequestHandler = async (req, res) => { | ||
| await authService.forgotPassword(req.body); | ||
|
|
||
| res.json({ | ||
| message: 'If an account exists, a password reset link has been sent', | ||
| }); | ||
|
Comment on lines
+38
to
+44
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 task requires 'Show email sent page' after forgot password. Returning JSON doesn't satisfy this. Should serve an HTML page or redirect to a confirmation page. |
||
| }; | ||
|
|
||
| const resetPassword: RequestHandler<ResetPasswordParams> = async (req, res) => { | ||
| const { token } = req.params; | ||
|
|
||
| await authService.resetPassword(token, req.body); | ||
|
|
||
| res.json({ | ||
| message: 'Password has been reset successfully', | ||
| }); | ||
|
Comment on lines
+46
to
+54
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 task requires 'Show Success page with a link to login' after password reset. Returning JSON doesn't satisfy this. Should serve an HTML page with a link to the login page. |
||
| }; | ||
|
|
||
| export const authController = { | ||
| register, | ||
| activate, | ||
| login, | ||
| logout, | ||
| forgotPassword, | ||
| resetPassword, | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { Router } from 'express'; | ||
| import { authController } from './auth.controller.js'; | ||
| import { asyncHandler } from '../../middlewares/asyncHandler.js'; | ||
| import { authMiddleware } from '../../middlewares/auth.middleware.js'; | ||
|
|
||
| export const authRoutes = Router(); | ||
|
|
||
| authRoutes.post('/register', asyncHandler(authController.register)); | ||
| authRoutes.get('/activate/:token', asyncHandler(authController.activate)); | ||
| authRoutes.post('/login', asyncHandler(authController.login)); | ||
| authRoutes.post('/logout', authMiddleware, authController.logout); | ||
|
|
||
| authRoutes.post( | ||
| '/forgot-password', | ||
| asyncHandler(authController.forgotPassword), | ||
| ); | ||
|
|
||
| authRoutes.post( | ||
| '/reset-password/:token', | ||
| asyncHandler(authController.resetPassword), | ||
| ); |
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.
The task requires 'redirect to Profile after activation', but this returns JSON. Should use
res.redirect('/profile')or serve an HTML activation page.