Skip to content

yamigr/pusudb-use-auth-jwt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pusudb-use-auth-jwt

Middleware to authenticate the pusudb.

This middleware implements authentication with jsonwebtoken.

Framework: https://www.npmjs.com/package/pusudb

Installing

npm install pusudb-use-auth-jwt --save

Use

Create the auth-instance and define the options. The user can be accessed with req.user in other middlewares.

var Pusudb = require('pusudb')
var pusudb = new Pusudb(3000, 'localhost', { log : true })

var AuthJwt = require('pusudb-use-auth-jwt')

/**
 * Default options {
     *  login: '/login',
     *  logout: '/logout',
     *  signin: '/signin',
     *  signinActive: true,
     *  redirect: '/index',
     *  redirectActive: false,
     *  cookie : 'bearer', // cookie key
     *  cookieOptions : { maxAge : 365 * 86400 } // see npm cookies for other options
     *  secret : 'my-token-secret', // default a unique-string
     *  db: './users', // define './other-db' or __dirname + '/pathToDb/users'
     *  form: { password : 'password', name : 'email' },
     *  expire: '1y', // json webtoken expires in
     *  assignUser: false // assign key and userName to req.docs to handle it in the response
 * }
 */

var authjwt = new AuthJwt(/* options */) 

// Add some routes which needs authentication ( string || array )
auth.setRoutes('/admin', '/mypanel')
auth.setRoutes('/private', ['/privateOne', '/privateTwo'])

auth.setRoutesWebSocket('/api')

//add the middleware to the pusudb
pusudb.useBefore('http', authjwt.serve)

//add the middleware to the pusudb for websocket-authentication
pusudb.useBefore('ws', authjwt.serveWebSocket)

pusudb.listen(function(port, host){
    console.log('pusudb listening:', port, host)
})

HTML

POST the userdata to the pusudb. The names of the form-input-fields are defined in option form. The action are defined in options login, logout and signin.

WebSocket

Emit a login, logout, signin or token package to the pusudb.

// Login
ws.send(JSON.stringify({meta : 'login', data: { value : { email : 'pusu@pusu.com', password: '1234'}}} ));
// Logout
ws.send(JSON.stringify({ meta : 'logout', data: { value : { email : 'pusu@pusu.com', password: '1234'}}}));
// Signin
ws.send(JSON.stringify({meta : 'signin', data: { value: { email : 'pusu@pusu.com', password: '1234'}}}));
// Token
ws.send(JSON.stringify({meta : 'token', data: /* jsonwebtoken from http-cookie, check options */));

Authors

  • Yannick Grund - Initial work - yamigr

License

This project is licensed under the MIT License

About

Middleware to authenticate the pusudb.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published