Skip to content
This repository was archived by the owner on Mar 23, 2020. It is now read-only.
Open

mvp #475

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
29 changes: 29 additions & 0 deletions auth/restMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// module.exports = function restricted(req, res, next) {
// if (req.session && req.session.user) {
// next();
// } else {
// res.status(401).json({ message: "no pass" });
// }
// };

const jwt = require("jsonwebtoken"); // <<< install this npm package

const { jwtSecret } = require("../config/secrets.js");

module.exports = function restricted (req, res, next) {
const { authorization } = req.headers;

if (authorization) {
jwt.verify(authorization, jwtSecret, (err, decodedToken) => {
if (err) {
res.status(401).json({ message: "Invalid Credentials" });
} else {
req.decodedToken = decodedToken;

next();
}
});
} else {
res.status(400).json({ message: "No credentials provided" });
}
};
3 changes: 3 additions & 0 deletions config/secrets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
jwtSecret: process.env.JWT_SECRET || 'lock this up tight'
}
5 changes: 5 additions & 0 deletions data/dbConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const knex = require('knex');

const config = require('../knexfile');

module.exports = knex(config.development);
Binary file added data/users2.db3
Binary file not shown.
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const server = require('./server.js');

const port = process.env.PORT || 5000;
server.listen(port, () => console.log(`\n** Running on port ${port}**\n`))
44 changes: 44 additions & 0 deletions knexfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Update with your config settings.

module.exports = {

development: {
client: 'sqlite3',
connection: {
filename: './data/users2.db3'
}
},

staging: {
client: 'postgresql',
connection: {
database: 'my_db',
user: 'username',
password: 'password'
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
},

production: {
client: 'postgresql',
connection: {
database: 'my_db',
user: 'username',
password: 'password'
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
}

};
19 changes: 19 additions & 0 deletions migrations/20200226112143_users2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

exports.up = function(knex) {
return knex.schema
.createTable("users", tbl => {
tbl.increments();
tbl.text("username", 30)
.unique()
.notNullable()
tbl.text("password", 30).notNullable()
tbl.string('department')

})
};

exports.down = function(knex) {
return knex.schema
.dropTableIfExists("Users")
};

Loading