Accompanying repository for scotch article on auth with JWTs and node
- Based on: https://scotch.io/tutorials/authenticate-a-node-es6-api-with-json-web-tokens
- Get MongoDB (before you install mongoose!): https://treehouse.github.io/installation-guides/mac/mongo-mac.html
- https://www.dineshonjava.com/mongodb-hello-world-example/
- https://dzone.com/articles/top-10-most-common-commands-for-beginners
- setting up auth/roles: https://medium.com/mongoaudit/how-to-enable-authentication-on-mongodb-b9e8a924efac
cd mongodb/bin/mongo
sudo mkdir -p /Users/hanz/data/db
./mongod --dbpath=/Users/hanz/data/db
./mongod
./mongo
- hello world: https://www.dineshonjava.com/mongodb-hello-world-example/
- unable to run: https://liuzhenglai.com/post/5f6fa44ca21e0635bcda536f
- installation instructos: https://docs.mongodb.com/guides/server/install/
# list dbs
$/>show dbs
# creates if the db is not found
$/>use opruimen
#show collections
$/>show collections
# show all records in a collection
$/>db.users.find()
# pretty print
$/>db.users.find().pretty()
# clear a collection (deletes all docs)
$/>db.users.remove({});
# insert a record => creates a collection called employees and inserts 'the new document'
$/>db.employees.insert({empName:"Dinesh", age:"26", salary:"50000"})
#search
$/>db.find({empName: "Dinesh"})
# size of a collection
$/>db.collectionName.dataSize()
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test', {useNewUrlParser: true, useUnifiedTopology: true});
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
console.log("successfully connected");
});
- [POST] http://localhost:4201/api/v1/users body: { "name": "arthur", "password": "andreas"}
- [POST] http://localhost:4201/api/v1/login body: { "name": "arthur", "password": "andreas"}
- [GET] http://localhost:4201/api/v1/users header: { "authorization", "Bearer signedjwt"}