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
5 changes: 5 additions & 0 deletions Abha_Rana/nodejwt/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
.vscode
npm-debug.log
.serverless
variables.env
13 changes: 13 additions & 0 deletions Abha_Rana/nodejwt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# nodejs-restful-api


It consist of a User model and controller. The model
defines the data, and the controller will contain all
the business logic needed to interact with the database.

It has a db file which will be used to
connect the app to the database, and an app file used
for bootstrapping the application itself.

The server file is used to spin up the server and tells the
app to listen on a specific port.
20 changes: 20 additions & 0 deletions Abha_Rana/nodejwt/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var express = require('express');
var app = express();
var db = require('./db');
var AuthController = require('./auth/AuthController');
app.use('/api/auth', AuthController);
var UserController = require('./user/UserController');
app.use('/users', UserController);
global.__root = __dirname + '/';

app.get('/api', function (req, res) {
res.status(200).send('API works.');
});

var UserController = require(__root + 'user/UserController');
app.use('/api/users', UserController);

var AuthController = require(__root + 'auth/AuthController');
app.use('/api/auth', AuthController);

module.exports = app;
67 changes: 67 additions & 0 deletions Abha_Rana/nodejwt/auth/AuthController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');
router.use(bodyParser.urlencoded({ extended: false }));
router.use(bodyParser.json());
var User = require('../user/User');
var VerifyToken = require('./VerifyToken');
var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
var bcrypt = require('bcryptjs');
var config = require('../config'); // get config file

router.post('/register', function(req, res) {

var hashedPassword = bcrypt.hashSync(req.body.password, 8);

User.create({
name : req.body.name,
email : req.body.email,
password : hashedPassword
},
function (err, user) {
if (err) return res.status(500).send("There was a problem registering the user.")
// create a token
var token = jwt.sign({ id: user._id }, config.secret, {
expiresIn: 86400 // expires in 24 hours
});
res.status(200).send({ auth: true, token: token });
});
});
router.get('/me', function(req, res) {
var token = req.headers['x-access-token'];
if (!token) return res.status(401).send({ auth: false, message: 'No token provided.' });

jwt.verify(token, config.secret, function(err, decoded) {
if (err) return res.status(500).send({ auth: false, message: 'Failed to authenticate token.' });

//res.status(200).send(decoded);
User.findById(decoded.id,{ password: 0 }, function (err, user) {
if (err) return res.status(500).send("There was a problem finding the user.");
if (!user) return res.status(404).send("No user found.");

res.status(200).send(user);
// next(user);
});
});
});
router.post('/login', function(req, res) {

User.findOne({ email: req.body.email }, function (err, user) {
if (err) return res.status(500).send('Error on the server.');
if (!user) return res.status(404).send('No user found.');

var passwordIsValid = bcrypt.compareSync(req.body.password, user.password);
if (!passwordIsValid) return res.status(401).send({ auth: false, token: null });

var token = jwt.sign({ id: user._id }, config.secret, {
expiresIn: 86400 // expires in 24 hours
});

res.status(200).send({ auth: true, token: token });
});

});
router.get('/logout', function(req, res) {
res.status(200).send({ auth: false, token: null });
});
module.exports = router;
19 changes: 19 additions & 0 deletions Abha_Rana/nodejwt/auth/VerifyToken.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var jwt = require('jsonwebtoken');
var config = require('../config');

function verifyToken(req, res, next) {
var token = req.headers['x-access-token'];
if (!token)
return res.status(403).send({ auth: false, message: 'No token provided.' });

jwt.verify(token, config.secret, function(err, decoded) {
if (err)
return res.status(500).send({ auth: false, message: 'Failed to authenticate token.' });

// if everything good, save to request for use in other routes
req.userId = decoded.id;
next();
});
}

module.exports = verifyToken;
3 changes: 3 additions & 0 deletions Abha_Rana/nodejwt/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
'secret': 'supersecret'
};
2 changes: 2 additions & 0 deletions Abha_Rana/nodejwt/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/bootcamp');
82 changes: 82 additions & 0 deletions Abha_Rana/nodejwt/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
<!--Bootsrap 4 CDN-->
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"
crossorigin="anonymous"
/>

<!--Fontawesome CDN-->
<link
rel="stylesheet"
href="https://use.fontawesome.com/releases/v5.3.1/css/all.css"
integrity="sha384-mzrmE5qonljUremFsqc01SB46JvROS7bZs3IO2EmfFsd15uHvIt+Y8vEf7N7fWAU"
crossorigin="anonymous"
/>
<!--<link rel="stylesheet" type="text/css" href="../styles/s1.css" >-->
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"
></script>
</head>
<body>
<div class="container">
<div class="d-flex justify-content-center h-100">
<div class="card">
<div class="card-header">
<h3>Member Log In</h3>
</div>
<div class="card-body">
<form>
<div class="input-group form-group">
<div class="input-group-prepend">
<span class="input-group-text"
><i class="fas fa-user"></i
></span>
</div>
<input
id="Username"
type="text"
class="form-control"
placeholder="username"
/>
</div>
<div class="input-group form-group">
<div class="input-group-prepend">
<span class="input-group-text"
><i class="fas fa-key"></i
></span>
</div>
<input
id="Password"
type="password"
class="form-control"
placeholder="password"
/>
</div>
<div class="form-group">
<input
type="button"
id="btn"
value="Login"
class="btn btn-block login_btn"
/>
</div>
</form>
</div>
<div class="card-footer">
<div class="d-flex justify-content-center links">
Don't have an account?<a href="signup.html">Sign Up</a>
</div>
</div>
</div>
</div>
</div>
<script src="./script.js"></script>
</body>
</html>
Loading