Skip to content

Long dev #6

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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
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
148 changes: 148 additions & 0 deletions public/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>404</title>
<link rel="stylesheet" href="./css/styles.css" />
</head>
<style>
@import url("https://fonts.googleapis.com/css?family=Nunito:400,600,700");
@keyframes floating {
from {
transform: translateY(0px);
}
65% {
transform: translateY(15px);
}
to {
transform: translateY(0px);
}
}
html {
height: 100%;
}
body {
background-image: url("https://assets.codepen.io/1538474/star.svg"),
linear-gradient(to bottom, #05007a, #4d007d);
height: 100%;
margin: 0;
background-attachment: fixed;
overflow: hidden;
}
.mars {
left: 0;
right: 0;
bottom: 0;
position: absolute;
height: 27vmin;
background: url("https://assets.codepen.io/1538474/mars.svg") no-repeat
bottom center;
background-size: cover;
}
.logo-404 {
position: absolute;
margin-left: auto;
margin-right: auto;
left: 0;
right: 0;
top: 16vmin;
width: 30vmin;
}
@media (max-width: 480px) and (min-width: 320px) {
.logo-404 {
top: 45vmin;
}
}
.meteor {
position: absolute;
right: 2vmin;
top: 16vmin;
}
.title {
color: white;
font-family: "Nunito", sans-serif;
font-weight: 600;
text-align: center;
font-size: 5vmin;
margin-top: 31vmin;
}
@media (max-width: 480px) and (min-width: 320px) {
.title {
margin-top: 65vmin;
}
}
.subtitle {
color: white;
font-family: "Nunito", sans-serif;
font-weight: 400;
text-align: center;
font-size: 3.5vmin;
margin-top: -1vmin;
margin-bottom: 9vmin;
}
.btn-back {
border: 1px solid white;
color: white;
height: 5vmin;
padding: 12px;
font-family: "Nunito", sans-serif;
text-decoration: none;
border-radius: 5px;
}
.btn-back:hover {
background: white;
color: #4d007d;
}
@media (max-width: 480px) and (min-width: 320px) {
.btn-back {
font-size: 3.5vmin;
}
}
.astronaut {
position: absolute;
top: 18vmin;
left: 10vmin;
height: 30vmin;
animation: floating 3s infinite ease-in-out;
}
@media (max-width: 480px) and (min-width: 320px) {
.astronaut {
top: 2vmin;
}
}
.spaceship {
position: absolute;
bottom: 15vmin;
right: 24vmin;
}
@media (max-width: 480px) and (min-width: 320px) {
.spaceship {
width: 45vmin;
bottom: 18vmin;
}
}
</style>
<body>
<div class="mars"></div>
<img src="https://assets.codepen.io/1538474/404.svg" class="logo-404" />
<img src="https://assets.codepen.io/1538474/meteor.svg" class="meteor" />
<p class="title">Oh no!!</p>
<p class="subtitle">
You’re either misspelling the URL <br />
or requesting a page that's no longer here.
</p>
<div align="center">
<a class="btn-back" href="#">Back to previous page</a>
</div>
<img
src="https://assets.codepen.io/1538474/astronaut.svg"
class="astronaut"
/>
<img
src="https://assets.codepen.io/1538474/spaceship.svg"
class="spaceship"
/>
</body>
</html>
33 changes: 25 additions & 8 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
const mongoose = require('mongoose');
const express = require('express');
const dotenv = require('dotenv');
const path = require('path');

dotenv.config({ path: './config.env' });

const app = express();

const DB = process.env.DATABASE.replace('<PASSWORD>', process.env.DATABASE_PASSWORD);

mongoose
.connect(DB, {
useUnifiedTopology: true,
useNewUrlParser: true,
})
.then(() => console.log('DB connection successful!'));

const api = require('./src/api');

const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`App running on port ${port}...`);
=======
const api = require('./src/api');
const path = require('path');
const cloudinary = require('cloudinary');
Expand All @@ -12,23 +34,18 @@ cloudinary.config({
api_secret: process.env.CLOUDINARY_SECRET_KEY,
});

mongoose.connect(process.env.MONGODB_CONNECT_STRING);

app.use(cors());

app.listen('3000', () => {
console.log('Running');
});

// ROUTING
app.use(express.json());

app.use('/api/v1', api);

// ERROR HANDLER
app.use((req, res) => {
res.status(404).json({
statusCode: 404,
message: 'API not found',
});
res.status(404).sendFile(path.join(__dirname, '/public/404.html'));
});

app.use((error, req, res, next) => {
Expand Down
130 changes: 49 additions & 81 deletions src/api/auth/auth.controller.js
Original file line number Diff line number Diff line change
@@ -1,82 +1,50 @@
const { promisify } = require('util');
const jwt = require('jsonwebtoken');
const User = require('../models/userModel');
const catchAsync = require('../utils/catchAsync');
const AppError = require('../utils/appError');

const signToken = (id) => {
return jwt.sign({ id }, process.env.JWT_SECRET, {
expiresIn: process.env.JWT_EXPIRES_IN,
});
const authService = require('./auth.service');

module.exports = {
signup: async (req, res, next) => {
try {
res.send(await authService.signup(req.body));
} catch (error) {
next(error);
}
},
login: async (req, res, next) => {
try {
res.send(await authService.login(req.body));
} catch (error) {
next(error);
}
},
protect: async (req, res, next) => {
try {
await authService.protect(req);
next();
} catch (error) {
next(error);
}
},
forgotPassword: async (req, res, next) => {
try {
let DTO = await authService.forgotPassword(req);
res.status(200).json(DTO);
} catch (error) {
next(error);
}
},
resetPassword: async (req, res, next) => {
try {
let DTO = await authService.resetPassword(req.body, req.params.token);
res.status(200).json(DTO);
} catch (error) {
next(error);
}
},
updatePassword: async (req, res, next) => {
try {
let DTO = await authService.updatePassword(req.user.id, req.body);
res.status(200).json(DTO);
} catch (error) {
next(error);
}
},
};

exports.signup = catchAsync(async (req, res, next) => {
const newUser = await User.create({
name: req.body.name,
email: req.body.email,
password: req.body.password,
passwordConfirm: req.body.passwordConfirm,
});

const token = signToken(newUser.id);

res.status(201).json({
status: 'success',
token,
data: {
user: newUser,
},
});
});

exports.login = async (req, res, next) => {
const { email, password } = req.body;

// 1) Check if email and password exist
if (!email || !password) {
next(new AppError('Please provide both an email and password!', 400));
}
// 2) Check if user exists and password are correct
const user = await User.findOne({ email }).select('+password');

if (!user || !(await user.correctPassword(password, user.password))) {
return next(new AppError('Incorrect email or password!', 401));
}

// 3) If everything ok, send token to client
const token = signToken(user._id);
res.status(200).json({
status: 'success',
token,
});
};

exports.protect = catchAsync(async (req, res, next) => {
let token;
// 1) Getting token and check of it's there
if (req.headers.authorization && req.headers.authorization.startsWith('Bearer ')) {
token = req.headers.authorization.split(' ')[1];
}

if (!token) {
return next(new AppError('You are not logged in! Please log in to get access', 401));
}

// 2) Verification token
const decoded = await promisify(jwt.verify)(token, process.env.JWT_SECRET);

// 3) Check if user still exists
const currentUser = await User.findById(decoded.id);
if (!currentUser) {
return next(new AppError('The token belonging to this token does no longer exist!', 401));
}

// 4) Check if user changed password after the token was issued
if (currentUser.changedPasswordAfter(decoded.iat)) {
return next(new AppError('User recently changed password! Please log in again.', 401));
}

// GRANT ACCESS TO PROTECTED ROUTE
req.user = currentUser;
next();
});
Loading