JWT (JSON Web Token) is one way of the authentication system that uses authentication tokens. if user authentication typically uses sessions, this will incriminate the server. because the session will use space on our server. either by using JWT we will not overload the server. In Jwt everytime we want to do something we will also send the token we have generated.
We will use JWT (JSON Web Token) on the server side using Node.js.
Please install Npm init in your project folder. After Npm init you will get package.json in your folder.
We need to install some things. They are express, mongoose, jsonwebtoken, and cors. We can install all Dependencies with NPM
npm install express body-parser mongoose jsonwebtoken cors
After we successfully install the packages. we can see it in package.json
{
"name": "nodejwt",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.18.2",
"cors": "^2.8.4",
"express": "^4.16.3",
"jsonwebtoken": "^8.2.1",
"mongoose": "^5.0.16"
}
}
We will make a simple config useful for storing our server configuration. We can create a file with the name config.js
module.exports = {
'secret' : 'This secret key',
'database' : 'mongodb://127.0.0.1/jwtusers'
}
We can create a file that we will use as a server. in this tutorial, I named server.js. We need to set up on our file server. We will do two type setup, the first is set up for global server and the second is set up local part such as database and others.
Setup
//=======Set up
var express = require('express');
var bodyParser = require('body-parses');
var mongoose = require('mongoose');
var jwt = require('jsonwebtoken');
var app = express();
var router = express.Router();
var cors = require('cors');
Local Setup
//=======Local Setup
var config = require('./app/config');
var user = require('./app/models/user');
var port = 3000;
To hit endpoints or to access the page I will use postman. so we can request URL like get, post, put, patch or etc.
Setting
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
mongoose.connect(config.database);
app.set('secretKey', config.secret);
app.use(cors());
app.listen(port);
node server.jsWe have made a mongoose connection with mongoose.connect () on server.js.
but we have not filled the database yet. We can open the command prompt or terminal in our project and run mongod.
mongod
We can choose the database that we will use, in config.js 'database': 'mongodb://127.0.0.1/jwtusers' we have determined will use the database jwtuser.
We can enter data into our mongoose database in this way
db.users.insert() : users is the name of the jwtusers database collection.
and we can user method insert (). and in insert () we can enter data in the form of object
//object
{
key: value,
key: value,
...........
}
var mongoose = require('mongoose');
var schema = mongoose.Schema;
module.exports = mongoose.model('User', new Schema({
email : String,
password: String
}));
We need package mongoose, we can use require ('mongoose'), Any configuration using Mongoose should be accompanied by Schema. The schema is a mapping of the MongoDB collection and the definition of the type of data used in each object in the collection. we can use mongoose.Schema. While the model ('User') is a compiled constructor derived from Schema that we have defined.
app.get('/users',function(req,res){
User.find({},function(err, users){
res.json(users);
})
})
We have created a setup and installation of mongoose, express, and others. we have also learned to enter data. now to see the result you can open your postman and open the localhost: 3000/users.
we can see the results of what we input in the mongoose database.
We can see the data you input can be accessed via localhost: 3000 / users. in the next tutorial, I will make an authentication with JWT and JWT token Verification. Thank you...