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
1 change: 0 additions & 1 deletion app/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ export const config = {
jwt: process.env.JWT_SECRET || "asdasdawe",
email: process.env.EMAIL,
password: process.env.EMAIL_PASSWORD,
domain: ""
};
6 changes: 4 additions & 2 deletions app/controllers/announcementsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@ export const getAnnouncements = async (req, res, next) => {
};

export const addAnnouncement = async (req, res, next) => {
const { title, description, price, category, animal, city, user } = req.body;
const { title, description, price, category, animal, city } = req.body;

let creator;

try {
creator = await User.findById(user);
creator = await User.findById(req.user.id);
} catch (e) {
res.status(422).json({ message: "Couldn't find user" });
return next();
}

const announcement = new Announcement({
title: title,
description: description,
Expand All @@ -43,6 +44,7 @@ export const addAnnouncement = async (req, res, next) => {
city: city,
user: creator,
});

try {
await announcement.save();
res.status(201).json(announcement);
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/opinionController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { Opinion } from "../db/models/OpinionSchema.js";
import { User } from "../db/models/UserSchema.js";

export const addOpinion = async (req, res, next) => {
const { rate, opinion, user } = req.body;
const { rate, opinion } = req.body;
let creator;
try {
creator = await User.findById(user);
creator = await User.findById(req.user.id);
} catch (error) {
res.status(422).json({ message: "Couldn't find user" });
return next();
Expand Down
77 changes: 56 additions & 21 deletions app/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ export const login = async (req, res, next) => {
}
}
const isValidPassword = user.comparePassword(req.body.password);
const { _id, email, username, city, phone } = user;
if (isValidPassword) {
const token = jsonwebtoken.sign({ id: user.id }, config.jwt);
const token = jsonwebtoken.sign(
{ id: _id, email, username, city, phone },
config.jwt,
);
return res.header("auth-token", token).send(token);
}
return res.json({ error: "Invalid password" });
Expand All @@ -43,7 +47,7 @@ export const login = async (req, res, next) => {
}
};

export const reset = async (req, res) => {
export const forgot = async (req, res) => {

const email = req.body.email;
User.findOne({email: email}, (err, user) =>{
Expand All @@ -53,9 +57,7 @@ export const reset = async (req, res) => {
const token = jsonwebtoken.sign({ id: user.id }, config.jwt, {expiresIn: "20m"});

const transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
service: "Gmail",
auth: {
user: config.email,
pass: config.password
Expand All @@ -66,7 +68,7 @@ export const reset = async (req, res) => {
from: config.email,
to: email,
subject: "Reset password",
html: `<h2> To reset your password <a href="https://coderscamplittledevs.github.io/CodersCamp2021-ProjectSinglePageApplication-PetsAdoptAndCarePortal/#/CodersCamp2021-ProjectSinglePageApplication-PetsAdoptAndCarePortal/auth/reset/${token}">click here</a></h2>`
html: `<h2> To reset your password <a href="http://localhost:${config.port}/auth/reset/${token}">click here</a></h2>`
}
transporter.sendMail(mailOptions, (err, data) =>{
if(err){
Expand All @@ -82,6 +84,25 @@ export const reset = async (req, res) => {
})
})
}

export const reset = async (req, res) => {
const token = req.params.token;
let newPassword = req.body.password;
let user;

let decoded = jsonwebtoken.decode(token);
user = await User.findById(decoded.id);
user.password = newPassword;

try {
await user.save();
return res.status(200).json({message: "Password has been changed!"})

} catch (error) {
res.status(400).json({error: error})
}
}

export const getUserData = async (req, res, next) => {
const id = req.params.uid;
let user;
Expand All @@ -93,6 +114,11 @@ export const getUserData = async (req, res, next) => {
city: user.city,
phone: user.phone,
announcements: user.announcements,
business: user.business,
description: user.description,
NIP: user.NIP,
openHour: user.openHour,
closeHour: user.closeHour,
});
} catch (error) {
res.status(422).json({ error: "User not found" });
Expand All @@ -107,23 +133,32 @@ export const updateUser = async (req, res, next) => {
} catch (error) {
res.status(400).json({ error: "User not found" });
}
if(user.comparePassword(req.body.password)){
const { city, phone, business, description, NIP, openHour, closeHour, password} = req.body;
if(city) user.city = city;
if(phone) user.phone = phone;
if(business) user.business = business;
if(description) user.description = description;
if(NIP) user.NIP = NIP;
if(openHour) user.openHour = openHour;
if(closeHour) user.closeHour = closeHour;
if(password) user.password = password;
if (user.comparePassword(req.body.password)) {
const {
city,
phone,
business,
description,
NIP,
openHour,
closeHour,
password,
} = req.body;
if (city) user.city = city;
if (phone) user.phone = phone;
if (business) user.business = business;
if (description) user.description = description;
if (NIP) user.NIP = NIP;
if (openHour) user.openHour = openHour;
if (closeHour) user.closeHour = closeHour;
if (password) user.password = password;
try {
await user.save();
res.json({message: "Succesfully data changed!"})
res.json({ message: "Succesfully data changed!" });
} catch (error) {
res.json({error: "Couldn't get data"});
res.json({ error: error });
}
}else{
return res.status(422).json({error: "invalid Password"})
} else {
return res.status(422).json({ error: "invalid Password" });
}
};
};
1 change: 0 additions & 1 deletion app/db/models/UserSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const UserSchema = new mongoose.Schema({
type: String,
required: true,
minLength: [4, "At least 4 characters"],
maxlength: [30, "Max length is 30 characters"],
},
username: {
type: String,
Expand Down
8 changes: 7 additions & 1 deletion app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ import { opinionsRouter } from "./routes/opinionsRoutes.js";
import { userRouter } from "./routes/userRouter.js";
import "./db/mongoose.js";
import { config } from "./config.js";
import swaggerUi from "swagger-ui-express";
import swaggerFile from "./swagger-output.json" assert { type: "json" };

const app = express();

app.use(express.json());

app.use(function (req, res, next) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, PATCH");
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, DELETE, PUT, PATCH",
);
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
next();
});
Expand All @@ -21,6 +26,7 @@ app.use("/auth", authRouter);
app.use("/announcements", announcementRouter);
app.use("/opinions", opinionsRouter);
app.use("/user", userRouter);
app.use("/doc", swaggerUi.serve, swaggerUi.setup(swaggerFile));

app
.listen(config.port, () => {
Expand Down
1 change: 1 addition & 0 deletions app/middleware/verifyToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export function auth(req, res, next) {
try {
const verified = jwt.verify(token, config.jwt);
req.user = verified;
return next();
} catch (error) {
res.status(400).send("Invalid Token");
}
Expand Down
10 changes: 3 additions & 7 deletions app/routes/authRoutes.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import express from "express";
import { login, register, reset } from "../controllers/userController.js";
import { login, register, forgot, reset } from "../controllers/userController.js";

export const authRouter = express.Router();

authRouter.post("/login", login);

authRouter.post("/forgot", (req, res) => {
res.status(200).json({
message: "Handling POST requests to /auth/forgot",
});
});
authRouter.post("/forgot", forgot);

authRouter.post("/reset", reset);
authRouter.post("/reset/:token", reset);

authRouter.post("/register", register);
Loading