-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #150 from Code-Hammers/CHE-167/story/BE-Refactor-E…
…rror-Handling [CHE-167] BE Refactor Error Handling
- Loading branch information
Showing
36 changed files
with
416 additions
and
255 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import path from 'path'; | ||
import express, { Request, Response } from 'express'; | ||
import 'express-async-errors'; | ||
import dotenv from 'dotenv'; | ||
import cookieParser from 'cookie-parser'; | ||
dotenv.config(); | ||
import { | ||
userRouter, | ||
profileRouter, | ||
authRouter, | ||
imageRouter, | ||
alumniRouter, | ||
forumRouter, | ||
devRouter, | ||
} from './routes'; | ||
import errorHandler from './middleware/errorHandler'; | ||
import { NotFoundError } from './errors'; | ||
|
||
// Instantiate application | ||
const app = express(); | ||
|
||
// Middleware to parse request bodies | ||
app.use(express.json()); | ||
// Middleware to parse request cookies | ||
app.use(cookieParser()); | ||
|
||
// AWS Production Health Check | ||
app.get('/health', (_req: Request, res: Response) => { | ||
res.status(200).send('OK'); | ||
}); | ||
|
||
// API routers | ||
app.use('/api/users', userRouter); | ||
app.use('/api/profiles', profileRouter); | ||
app.use('/api/auth', authRouter); | ||
app.use('/api/images', imageRouter); | ||
app.use('/api/alumni', alumniRouter); | ||
app.use('/api/forums', forumRouter); | ||
app.use('/api/devRoutes', devRouter); | ||
|
||
// Serve client from build in production | ||
if (process.env.NODE_ENV === 'production') { | ||
console.log(`SERVER STARTED IN PRODUCTION`); | ||
app.use(express.static(path.join(__dirname, '../../client/build'))); | ||
|
||
app.get('*', (_req: Request, res: Response) => | ||
res.sendFile(path.resolve(__dirname, '../../client/build/index.html')), | ||
); | ||
} | ||
|
||
// Catch all route handler | ||
app.use((_req, _res) => { | ||
throw new NotFoundError(); | ||
}); | ||
|
||
// Global error handler | ||
app.use(errorHandler); | ||
|
||
export default app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,9 @@ | ||
import jwt from 'jsonwebtoken'; | ||
import User from '../models/userModel'; | ||
import asyncHandler from 'express-async-handler'; | ||
import { Request, Response } from 'express'; | ||
import { NotAuthorizedError } from '../errors'; | ||
|
||
const authSession = asyncHandler(async (req, res) => { | ||
let token; | ||
|
||
if (req.cookies.token) { | ||
try { | ||
token = req.cookies.token; | ||
const secret = process.env.JWT_SECRET as string; | ||
const decoded = jwt.verify(token, secret) as jwt.JwtPayload; | ||
|
||
if (!decoded.id) { | ||
throw new Error('Invalid token - ID not found'); | ||
} | ||
|
||
const user = await User.findById(decoded.id).select('-password'); | ||
|
||
if (!user) throw new Error('User not found'); | ||
|
||
res.locals.user = user; | ||
res.json({ isAuthenticated: true, user: res.locals.user }); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(401); | ||
throw new Error('Not authorized, token failed'); | ||
} | ||
} | ||
|
||
if (!token) { | ||
res.status(401); | ||
throw new Error('Not authorized, no token'); | ||
} | ||
}); | ||
const authSession = async (_req: Request, res: Response) => { | ||
if (!res.locals.user) throw new NotAuthorizedError(); | ||
res.json({ isAuthenticated: true, user: res.locals.user }); | ||
}; | ||
|
||
export { authSession }; |
Oops, something went wrong.