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
26 changes: 17 additions & 9 deletions src/handlers/product.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Response } from 'express';
import { NextFunction, Response } from 'express';
import prisma from '../prisma';
import { CustomRequest } from '../types';

Expand Down Expand Up @@ -26,15 +26,23 @@ export const getOneProduct = async (req: CustomRequest, res: Response) => {
};

// Create product
export const createProduct = async (req: CustomRequest, res: Response) => {
const product = await prisma.product.create({
data: {
name: req.body.name,
ownerId: req.user.id,
},
});
export const createProduct = async (
req: CustomRequest,
res: Response,
next: NextFunction
) => {
try {
const product = await prisma.product.create({
data: {
name: req.body.name,
ownerId: req.user.id,
},
});

res.json({ data: product });
res.json({ data: product });
} catch (e) {
next(e);
}
};

// update product
Expand Down
34 changes: 17 additions & 17 deletions src/handlers/user.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import { Request, Response } from 'express';
import { NextFunction, Request, Response } from 'express';
import prisma from '../prisma';
import { comparePassword, createJWT, hashPassword } from '../utils/auth';

export const createNewUser = async (req: Request, res: Response) => {
if (!req.body.password && !req.body.user) {
res.status(404);
res.json({
message: 'Nope',
export const createNewUser = async (
req: Request,
res: Response,
next: NextFunction
) => {
try {
const user = await prisma.user.create({
data: {
username: req.body.username,
password: await hashPassword(req.body.password),
},
});

return;
const token = createJWT(user);
res.json({ token });
} catch (e) {
e.type = 'input';
next(e);
}

const user = await prisma.user.create({
data: {
username: req.body.username,
password: await hashPassword(req.body.password),
},
});

const token = createJWT(user);
res.json({ token });
};

export const signIn = async (req: Request, res: Response) => {
Expand Down
4 changes: 4 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ dotenv.config();
import router from './router';
import { protect } from './utils/auth';
import { createNewUser, signIn } from './handlers/user';
import { errorHandler } from './utils/errorHandler';

const app = express();

Expand All @@ -20,4 +21,7 @@ app.use('/api', protect, router);
app.post('/user', createNewUser);
app.post('/signin', signIn);

/** Error handler **/
app.use(errorHandler);

export default app;
16 changes: 16 additions & 0 deletions src/utils/errorHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { NextFunction, Request, Response } from 'express';

export const errorHandler = async (
err,
req: Request,
res: Response,
next: NextFunction
) => {
if (err.type === 'auth') {
res.status(401).json({ message: 'unauthorized' });
} else if (err.type === 'input') {
res.status(400).json({ message: 'invalid input' });
} else {
res.status(500).json({ message: 'oops, thats on us' });
}
};