Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmad authored and ahmad committed Aug 8, 2020
1 parent 1bea9a9 commit 6bb9546
Show file tree
Hide file tree
Showing 18 changed files with 929 additions and 161 deletions.
200 changes: 100 additions & 100 deletions _data/courses.json
Original file line number Diff line number Diff line change
@@ -1,101 +1,101 @@
[
{
"_id": "5d725a4a7b292f5f8ceff789",
"title": "Front End Web Development",
"description": "This course will provide you with all of the essentials to become a successful frontend web developer. You will learn to master HTML, CSS and front end JavaScript, along with tools like Git, VSCode and front end frameworks like Vue",
"weeks": 8,
"tuition": 8000,
"minimumSkill": "beginner",
"scholarshipsAvailable": true,
"bootcamp": "5d713995b721c3bb38c1f5d0",
"user": "5d7a514b5d2c12c7449be045"
},
{
"_id": "5d725c84c4ded7bcb480eaa0",
"title": "Full Stack Web Development",
"description": "In this course you will learn full stack web development, first learning all about the frontend with HTML/CSS/JS/Vue and then the backend with Node.js/Express/MongoDB",
"weeks": 12,
"tuition": 10000,
"minimumSkill": "intermediate",
"scholarshipsAvailable": true,
"bootcamp": "5d713995b721c3bb38c1f5d0",
"user": "5d7a514b5d2c12c7449be045"
},
{
"_id": "5d725cb9c4ded7bcb480eaa1",
"title": "Full Stack Web Dev",
"description": "In this course you will learn all about the front end with HTML, CSS and JavaScript. You will master tools like Git and Webpack and also learn C# and ASP.NET with Postgres",
"weeks": 10,
"tuition": 12000,
"minimumSkill": "intermediate",
"scholarshipsAvailable": true,
"bootcamp": "5d713a66ec8f2b88b8f830b8",
"user": "5d7a514b5d2c12c7449be046"
},
{
"_id": "5d725cd2c4ded7bcb480eaa2",
"title": "UI/UX",
"description": "In this course you will learn to create beautiful interfaces. It is a mix of design and development to create modern user experiences on both web and mobile",
"weeks": 12,
"tuition": 10000,
"minimumSkill": "intermediate",
"scholarshipsAvailable": true,
"bootcamp": "5d713a66ec8f2b88b8f830b8",
"user": "5d7a514b5d2c12c7449be046"
},
{
"_id": "5d725ce8c4ded7bcb480eaa3",
"title": "Web Design & Development",
"description": "Get started building websites and web apps with HTML/CSS/JavaScript/PHP. We teach you",
"weeks": 10,
"tuition": 12000,
"minimumSkill": "beginner",
"scholarshipsAvailable": true,
"bootcamp": "5d725a037b292f5f8ceff787",
"user": "5c8a1d5b0190b214360dc031"
},
{
"_id": "5d725cfec4ded7bcb480eaa4",
"title": "Data Science Program",
"description": "In this course you will learn Python for data science, machine learning and big data tools",
"weeks": 10,
"tuition": 9000,
"minimumSkill": "intermediate",
"scholarshipsAvailable": false,
"bootcamp": "5d725a037b292f5f8ceff787",
"user": "5c8a1d5b0190b214360dc031"
},
{
"_id": "5d725cfec4ded7bcb480eaa5",
"title": "Web Development",
"description": "This course will teach you how to build high quality web applications with technologies like React, Node.js, PHP & Laravel",
"weeks": 8,
"tuition": 8000,
"minimumSkill": "beginner",
"scholarshipsAvailable": false,
"bootcamp": "5d725a1b7b292f5f8ceff788",
"user": "5c8a1d5b0190b214360dc032"
},
{
"_id": "5d725cfec4ded7bcb480eaa6",
"title": "Software QA",
"description": "This course will teach you everything you need to know about quality assurance",
"weeks": 6,
"tuition": 5000,
"minimumSkill": "intermediate",
"scholarshipsAvailable": false,
"bootcamp": "5d725a1b7b292f5f8ceff788",
"user": "5c8a1d5b0190b214360dc032"
},
{
"_id": "5d725cfec4ded7bcb480eaa7",
"title": "IOS Development",
"description": "Get started building mobile applications for IOS using Swift and other tools",
"weeks": 8,
"tuition": 6000,
"minimumSkill": "intermediate",
"scholarshipsAvailable": false,
"bootcamp": "5d725a1b7b292f5f8ceff788",
"user": "5c8a1d5b0190b214360dc032"
}
]
{
"_id": "5d725a4a7b292f5f8ceff789",
"title": "Front End Web Development",
"description": "This course will provide you with all of the essentials to become a successful frontend web developer. You will learn to master HTML, CSS and front end JavaScript, along with tools like Git, VSCode and front end frameworks like Vue",
"weeks": 8,
"tuition": 8000,
"minimumSkill": "beginner",
"scholarshipsAvailable": true,
"bootcamp": "5d725a1b7b292f5f8ceff788",
"user": "5d7a514b5d2c12c7449be045"
},
{
"_id": "5d725c84c4ded7bcb480eaa0",
"title": "Full Stack Web Development",
"description": "In this course you will learn full stack web development, first learning all about the frontend with HTML/CSS/JS/Vue and then the backend with Node.js/Express/MongoDB",
"weeks": 12,
"tuition": 10000,
"minimumSkill": "intermediate",
"scholarshipsAvailable": true,
"bootcamp": "5d725a1b7b292f5f8ceff788",
"user": "5d7a514b5d2c12c7449be045"
},
{
"_id": "5d725cb9c4ded7bcb480eaa1",
"title": "Full Stack Web Dev",
"description": "In this course you will learn all about the front end with HTML, CSS and JavaScript. You will master tools like Git and Webpack and also learn C# and ASP.NET with Postgres",
"weeks": 10,
"tuition": 12000,
"minimumSkill": "intermediate",
"scholarshipsAvailable": true,
"bootcamp": "5d713a66ec8f2b88b8f830b8",
"user": "5d7a514b5d2c12c7449be046"
},
{
"_id": "5d725cd2c4ded7bcb480eaa2",
"title": "UI/UX",
"description": "In this course you will learn to create beautiful interfaces. It is a mix of design and development to create modern user experiences on both web and mobile",
"weeks": 12,
"tuition": 10000,
"minimumSkill": "intermediate",
"scholarshipsAvailable": true,
"bootcamp": "5d713a66ec8f2b88b8f830b8",
"user": "5d7a514b5d2c12c7449be046"
},
{
"_id": "5d725ce8c4ded7bcb480eaa3",
"title": "Web Design & Development",
"description": "Get started building websites and web apps with HTML/CSS/JavaScript/PHP. We teach you",
"weeks": 10,
"tuition": 12000,
"minimumSkill": "beginner",
"scholarshipsAvailable": true,
"bootcamp": "5d713a66ec8f2b88b8f830b8",
"user": "5c8a1d5b0190b214360dc031"
},
{
"_id": "5d725cfec4ded7bcb480eaa4",
"title": "Data Science Program",
"description": "In this course you will learn Python for data science, machine learning and big data tools",
"weeks": 10,
"tuition": 9000,
"minimumSkill": "intermediate",
"scholarshipsAvailable": false,
"bootcamp": "5d713a66ec8f2b88b8f830b8",
"user": "5c8a1d5b0190b214360dc031"
},
{
"_id": "5d725cfec4ded7bcb480eaa5",
"title": "Web Development",
"description": "This course will teach you how to build high quality web applications with technologies like React, Node.js, PHP & Laravel",
"weeks": 8,
"tuition": 8000,
"minimumSkill": "beginner",
"scholarshipsAvailable": false,
"bootcamp": "5d725a037b292f5f8ceff787",
"user": "5c8a1d5b0190b214360dc032"
},
{
"_id": "5d725cfec4ded7bcb480eaa6",
"title": "Software QA",
"description": "This course will teach you everything you need to know about quality assurance",
"weeks": 6,
"tuition": 5000,
"minimumSkill": "intermediate",
"scholarshipsAvailable": false,
"bootcamp": "5d725a037b292f5f8ceff787",
"user": "5c8a1d5b0190b214360dc032"
},
{
"_id": "5d725cfec4ded7bcb480eaa7",
"title": "IOS Development",
"description": "Get started building mobile applications for IOS using Swift and other tools",
"weeks": 8,
"tuition": 6000,
"minimumSkill": "intermediate",
"scholarshipsAvailable": false,
"bootcamp": "5d725a037b292f5f8ceff787",
"user": "5c8a1d5b0190b214360dc032"
}
]
144 changes: 144 additions & 0 deletions controller/authController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
const crypto = require('crypto');
const ErrorResponse = require('../utils/errorResponse');

const User = require('../models/User');

// @desc Register user
// @route POST /api/v1/auth/register
// @access Public
exports.register = async (req, res, next) => {
const { name, email, password, role } = req.body;

// Create user
const user = await User.create({
name,
email,
password,
role
});

sendTokenResponse(user, 200, res);
};

// @desc Login user
// @route POST /api/v1/auth/login
// @access Public
exports.login = async (req, res, next) => {
const { email, password } = req.body;

// Validate emil & password
if (!email || !password) {
return res.status(400).json({ success: false ,error: "Please provide an email and password"});
}

// Check for user
const user = await User.findOne({ email }).select('+password');

if (!user) {
return res.status(401).json({ success: false ,error: "Invalid credentials"});

}

// Check if password matches
const isMatch = await user.matchPassword(password);

if (!isMatch) {
return res.status(401).json({ success: false ,error: "Invalid credentials"});
}

sendTokenResponse(user, 200, res);
};

// @desc Log user out / clear cookie
// @route GET /api/v1/auth/logout
// @access Public
exports.logout = async (req, res, next) => {
res.cookie('token', 'none', {
expires: new Date(Date.now() + 10 * 1000),
httpOnly: true
});

res.status(200).json({
success: true,
data: {}
});
};

// @desc Get current logged in user
// @route GET /api/v1/auth/me
// @access Private
exports.getMe = async (req, res, next) => {
// user is already available in req due to the protect middleware
const user = req.user;

res.status(200).json({
success: true,
data: user
});
};

// @desc Update user details
// @route PUT /api/v1/auth/updatedetails
// @access Private
exports.updateDetails = async (req, res, next) => {
const fieldsToUpdate = {
name: req.body.name,
email: req.body.email
};

const user = await User.findByIdAndUpdate(req.user.id, fieldsToUpdate, {
new: true,
runValidators: true
});

res.status(200).json({
success: true,
data: user
});
};

// @desc Update password
// @route PUT /api/v1/auth/updatepassword
// @access Private
exports.updatePassword = async (req, res, next) => {
const user = await User.findById(req.user.id).select('+password');

// Check current password
if (!(await user.matchPassword(req.body.currentPassword))) {
return next(new ErrorResponse('Password is incorrect', 401));
}

user.password = req.body.newPassword;
await user.save();

sendTokenResponse(user, 200, res);
};





// Get token from model, create cookie and send response
const sendTokenResponse = (user, statusCode, res) => {
// Create token
const token = user.getSignedJwtToken();

const options = {
expires: new Date(
Date.now() + process.env.JWT_COOKIE_EXPIRE * 24 * 60 * 60 * 1000
),
httpOnly: true
};

if (process.env.NODE_ENV === 'production') {
options.secure = true;
}

res
.status(statusCode)
.cookie('token', token, options)
.json({
success: true,
token
});
};
Loading

0 comments on commit 6bb9546

Please sign in to comment.