All Projects → teng-boy → node-rest-jwt-redis

teng-boy / node-rest-jwt-redis

Licence: other
node restify jwt redis mongo demo

Programming Languages

javascript
184084 projects - #8 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to node-rest-jwt-redis

vue-jwt-mongo
🔐 A simple authentication system for Vue.js
Stars: ✭ 14 (-22.22%)
Mutual labels:  mongo
server-next
😎 The next generation of RESTful API service and more for Mix Space, powered by @nestjs.
Stars: ✭ 43 (+138.89%)
Mutual labels:  mongo
generator-node-api-docker-1st-class-experience
NodeJS + Typescript + TSLint + Restify + MongoDB + Docker, With VS Code Automation = VSCode TypeScript Live Debug with Containers
Stars: ✭ 22 (+22.22%)
Mutual labels:  restify
NodeRestApi
Node.js, Express.js and MongoDB REST API App
Stars: ✭ 38 (+111.11%)
Mutual labels:  mongo
docker
collection of docker / docker-compose files, dind, gitlab, jenkins, mongo, mysql, oracle, rabbitmq, redis, sonarqube
Stars: ✭ 25 (+38.89%)
Mutual labels:  mongo
vertx-mongo-client
Mongo Client for Eclipse Vert.x
Stars: ✭ 54 (+200%)
Mutual labels:  mongo
mongo orm
Mongo ORM: A simple ORM for using MongoDB with the crystal programming language, designed for use with Amber. Based loosely on Granite ORM. Supports Rails-esque models, associations and embedded documents.
Stars: ✭ 32 (+77.78%)
Mutual labels:  mongo
restify-jwt-community
Restify middleware that validates a JsonWebToken
Stars: ✭ 24 (+33.33%)
Mutual labels:  restify
fastapi-oidc-react
React + FastApi + Mongo - Login with Google and Azure (OIDC authorisation code flow)
Stars: ✭ 42 (+133.33%)
Mutual labels:  mongo
tics
🎢 Simple self-hosted analytics ideal for Express / React Native stacks
Stars: ✭ 22 (+22.22%)
Mutual labels:  mongo
NodeExpressCRUD
Node, Express, Mongoose and MongoDB CRUD Web Application
Stars: ✭ 45 (+150%)
Mutual labels:  mongo
FlaskService
API boilerplate using Python Flask with MongoDB
Stars: ✭ 23 (+27.78%)
Mutual labels:  mongo
TIL
Today I Learned
Stars: ✭ 43 (+138.89%)
Mutual labels:  mongo
mongoose-slug-plugin
Slugs for Mongoose with history and i18n support (uses speakingurl by default, but you can use any slug library such as limax, slugify, mollusc, or slugme)
Stars: ✭ 21 (+16.67%)
Mutual labels:  mongo
mongo-mysql
Mongo vs Mysql Test Performance in Nodejs
Stars: ✭ 87 (+383.33%)
Mutual labels:  mongo
json-sql-builder2
Level Up Your SQL-Queries
Stars: ✭ 59 (+227.78%)
Mutual labels:  mongo
df data service
DataFibers Data Service
Stars: ✭ 31 (+72.22%)
Mutual labels:  mongo
blog3.0
博客V3.0 目前使用的技术(Nuxtjs + Nestjs + Vue + Element ui + vuetify),存储(MongoDB + Redis + COS)
Stars: ✭ 37 (+105.56%)
Mutual labels:  mongo
aws-s3-file upload-node-mongo-react-multer
A simple boilerplate project to implement AWS S3 file upload functionality in a Node, React and Mongo app. Using Multer for uploading file.
Stars: ✭ 41 (+127.78%)
Mutual labels:  mongo
mongoolia
Keep your mongoose schemas synced with Algolia
Stars: ✭ 58 (+222.22%)
Mutual labels:  mongo

环境

1、开发使用node版本:v8.0.0

2、demo依赖mongo和redis,所以在使用该demo的时候,必须在本地成功安装它们。

mongo

1、连接信息文件,参考lib下的db.js文件

2、用途:保存测试数据

用户model如下:
{
  'name': String,
  'pwd': String
}

3、配置:进入项目根目录,查看config文件夹下的development.js,修改对应的mongoConfig即可

//mongo连接信息
mongoConfig: {
  "host": "192.168.33.10",
  "database": "rest_test"
}

redis

1、连接信息文件,参考lib下的redis.js文件

2、保存token,刷新维护token

说明:系统默认token有效期为2h。

3、配置:进入项目根目录,查看config文件夹下的development.js,修改对应的redisConfig即可

//redis连接信息
redisConfig: {
  "host": "192.168.33.10",
  "port": 6379
}

关于token

demo使用了简单的jsonwebtoken模块管理token。

关键代码:
//token验证
function verify(req, res, next){
	//从head获取token
	let token = req.headers['x-broncos-token'];
	if(!token){
		return res.json({code: 1000, data: {}, msg: 'token is required'});
	}
	//验证token
	jwt.verify(token, config.secret, (err, decode) => {
		if(err) {
			console.log('verify err-->' + err);
			return res.json({code: 401, data: {}, msg: err});
		}
		//验证通过
		let expireIn = decode.expireIn;
		let userId = decode.userId;

		//判断token是否有效
		redis.exists(userId, function(e, ret){
			if(e) throw e;
			console.log('ret-->' + ret);

			if(ret){
				//该token有效,重置token过期时间
				redis.expire(userId, expireIn);
				next();
			}else{
				//token无效
				res.json({code: 401, data: {}, msg: 'invalid token'});
			}
		});
	});
}
说明:
1、token是由header+payload+secret组合而成的。
2、demo中payload部分组成如下:
{
    'user': 'xxx', //用户 _id
    'expireIn': xxx //单位:秒,token有效时间,demo默认2h
}
3、secret是签名,保证token安全的关键,不可暴露
4、关于jsonwebtoken的细致方面,可网上行搜索。

使用说明

1、拉取代码到本地:https://github.com/broncoss/node-rest-jwt-redis.git

2、启动方式:进入项目根目录,先npm install安装依赖,再执行./bin/develop.sh启动项目

3、除了生成token API /v1/token 外,其他所有API接口都需在header中传递x-broncos-token(其value为生成的token)

4、使用post方式请求/v1/token获取token时需要传namepwd,但为了方便测试,系统默认namepwd的值为test

5、在请求获取token时,可通过传expireIn自行设置token有效时间。在token有效期内访问API,系统自动刷新token有效时间。token一旦过期,需要重新获取token

线上测试案例

1、生成有效期为100秒的token

curl -l -H "Content-type: application/json" -X POST -d '{"name": "test", "pwd": "test", "expireIn": 100}' http://api.broncodes.com/v1/token
返回案例:
{
  "code":200,
  "data":{
    "user":{
      "__v":0,
      "name":"test",
      "pwd":"test",
      "_id":"593feff1e5fe22001003b812 //用户ID
    },
    "token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiI1OTNmZWZmMWU1ZmUyMjAwMTAwM2I4MTIiLCJleHBpcmVJbiI6MTAwMDAwMCwiaWF0IjoxNDk3MzYyNDE3fQ.rcZp6DlRIgjr8lID7nV4nV9pxLEz_FNlVnG1US1GHdg"
  },
  "msg":"success"
}

2、根据用户ID查询用户

curl -H "x-broncos-token:获取的token" -X GET http://api.broncodes.com/v1/users/用户ID
返回案例:
{
  "code":200,
  "data":{
    "_id":"593feff1e5fe22001003b812",
    "name":"test",
    "pwd":"test",
    "__v":0
  },
  "msg":"success"
}

3、获取所有用户

curl -H "x-broncos-token:获取的token" -X GET http://api.broncodes.com/v1/users
返回案例:
{
  "code":200,
  "data":[{
    "_id":"593ebf50e5fe22001003b810",
    "name":"test",
    "pwd":"test",
    "__v":0
  },{
    "_id":"593feff1e5fe22001003b812",
    "name":"test",
    "pwd":"test",
    "__v":0
  }],
  "msg":"success"
}

4、根据用户ID删除用户

curl -H "x-broncos-token:获取的token" -X DELETE http://api.broncodes.com/v1/users/用户ID
返回案例:
{
  "code":200,
  "data":{},
  "msg":"success"
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].