redux-security

0.0.15 • Public • Published

redux-security

Redux security subsystem with user, group and access management

NPM version Discord

alt tag

INSTALL

Stable

npm i -S redux-security

Development

npm i -S lokhmakov/redux-security

FEATURES

  • Service architecture (redux-manager powered)
  • Journaling (redux-journal powered)
  • Persist state (PouchDB powered)
  • Dynamic service configuration (state.config):
    • docsMax = 2 - maximum docs in state.docs
    • docsMaxOverRemove: true - remove old images over docsMax
  • User management
  • Session management
  • Auth
    • signup
    • login
    • logout
    • email confirmation
  • API
    • local
    • express
    • pouch
    • socket (SocketIO)

EXAMPLES

USERS: NODE-API

require('redux-journal').enable()
 
const tags = 'redux-security.examples.users.node-api'
 
import { write, error }   from 'redux-journal'
import { manager }        from 'redux-manager'
import { usersLocal }     from 'redux-security'
 
const api = usersLocal()
 
manager.enableLogger(require('redux-node-logger')())
manager.getStore()
 
api.create({ username: 'user1', password: 'password1' }).then(({ userID }) => {
  write(`USER _id = ${userID} CREATED`, `${tags}.api.create`)
  return api.update({ _id: userID, username: 'user2' }).then((result) => {
    write(`SUCCESS result = ${result}`, `${tags}.api.update`)
    return api.remove({ _id: userID })
  })
}).then((result) => {
  write(`SUCCESS result = ${result}`, `${tags}.api.remove`)
}).catch((e) => {
  error(e)
})

AUTH: NODE-API

require('redux-journal').enable()
 
const tags = 'redux-security.examples.auth.node-api'
 
import { write, error }     from 'redux-journal'
import { manager }          from 'redux-manager'
 
const serviceAuth     = 'auth'
const serviceSessions = 'sessions'
const serviceUsers    = 'users'
 
import { sessionsLocal }    from 'redux-security'
 
const apiSessions = sessionsLocal({ serviceName: serviceSessions })
 
import { usersLocal }       from 'redux-security'
 
const apiUsers = usersLocal({ serviceName: serviceUsers })
 
import { authLocal }        from 'redux-security'
 
const apiAuth = authLocal({ serviceName: serviceAuth, serviceSessions, serviceUsers })
 
manager.enableLogger(require('redux-node-logger')())
manager.getStore()
 
const username = 'root'
const password = 'toor'
 
apiUsers.create({ username, password }).then(() => {
  return apiAuth.login({ username, password})
}).then((result) => {
  write(`({ username = '${username}', password = '${password}'}) SUCCESS`, `${tags}.login`)
}).catch((e) => {
  error(e)
})

AUTH: NODE-REDUX

require('redux-journal').enable()
 
const tags = 'redux-security.examples.auth.node-redux'
 
import { write, error }     from 'redux-journal'
import { manager }          from 'redux-manager'
 
const serviceAuth     = 'auth'
const serviceSessions = 'sessions'
const serviceUsers    = 'users'
 
import { sessionsLocal }    from 'redux-security'
 
const apiSessions = sessionsLocal({ serviceName: serviceSessions })
 
import { usersLocal }       from 'redux-security'
 
const apiUsers = usersLocal({ serviceName: serviceUsers })
 
import { authLocal }        from 'redux-security'
import { authActions }      from 'redux-security'
 
const apiAuth = authLocal({ serviceName: serviceAuth, serviceSessions, serviceUsers })
 
manager.enableLogger(require('redux-node-logger')())
manager.getStore()
 
const username = 'root'
const password = 'toor'
 
apiUsers.create({ username, password }).then(() => {
  manager.dispatch(authActions.login({ username, password }), serviceAuth)
}).catch((e) => {
  error(e)
})

AUTH: NODE-SOCKET

server.js

require('redux-journal').enable()
 
const tags = 'redux-security.examples.auth.node-socket.server'
 
import { write, error }     from 'redux-journal'
import { manager }          from 'redux-manager'
 
const serviceAuth     = 'auth'
const serviceSessions = 'sessions'
const serviceUsers    = 'users'
 
import { sessionsLocal }    from 'redux-security'
 
sessionsLocal({ serviceName: serviceSessions })
 
import { usersLocal }       from 'redux-security'
 
const apiUsers = usersLocal({ serviceName: serviceUsers })
 
import { authLocal }        from 'redux-security'
import { authActions }      from 'redux-security'
 
authLocal({ serviceName: serviceAuth, serviceSessions, serviceUsers })
 
manager.enableLogger(require('redux-node-logger')())
manager.getStore()
 
const username = 'root'
const password = 'toor'
 
apiUsers.create({ username, password })
 
import Express              from 'express'
import { Server }           from 'http'
import SocketIO             from 'socket.io'
 
const PORT = 3000
const app = new Express()
const server = Server(app)
const io = SocketIO(server)
 
server.listen(PORT, (e) => {
  if (e) return error(e)
  write(`==> 🌎  0.0.0.0:${ PORT }`)
})
 
import { authConfigAPISocketServer } from 'redux-security'
 
authConfigAPISocketServer({ io })

client.js

require('redux-journal').enable()
 
const tags = 'redux-security.examples.auth.node-socket'
 
import { write, error }     from 'redux-journal'
import { manager }          from 'redux-manager'
 
const serviceAuth     = 'auth'
const serverServiceName = 'auth'
 
import { authSocket }        from 'redux-security'
import { authActions }      from 'redux-security'
 
const socket = require('socket.io-client')('http://localhost:3000')
authSocket({ serviceName: serviceAuth, serverServiceName, socket })
 
manager.enableLogger(require('redux-node-logger')())
manager.getStore()
 
const username = 'root'
const password = 'toor'
 
manager.dispatch(authActions.login({ username, password }), serviceAuth)

REACT

server.js

require('redux-journal').enable()
 
import { error, write }     from 'redux-journal'
 
import webpack              from 'webpack'
import webpackDevMiddleware from 'webpack-dev-middleware'
import webpackHotMiddleware from 'webpack-hot-middleware'
import webpackConfig        from '../webpack.config'
 
const PORT = 3000
const app = require('express')()
const server = require('http').Server(app)
const io = require('socket.io')(server)
 
const compiler = webpack(webpackConfig)
app.use(webpackDevMiddleware(compiler, { noInfo: false, publicPath: webpackConfig.output.publicPath }))
app.use(webpackHotMiddleware(compiler))
 
app.use((req, res) => { res.sendFile(__dirname + '/static/index.html') })
 
server.listen(PORT, (e) => {
  if (e) return error(e)
  write(`==> 🌎  0.0.0.0:${ PORT }`)
})
 
const tags = 'redux-security.examples.auth.react.server'
 
import { manager }          from 'redux-manager'
 
const serviceAuth     = 'auth'
const serviceSessions = 'sessions'
const serviceUsers    = 'users'
 
import { sessionsLocal }    from 'redux-security'
 
sessionsLocal({ serviceName: serviceSessions })
 
import { usersLocal }       from 'redux-security'
 
const apiUsers = usersLocal({ serviceName: serviceUsers })
 
import { authLocal }        from 'redux-security'
import { authActions }      from 'redux-security'
 
authLocal({ serviceName: serviceAuth, serviceSessions, serviceUsers })
 
manager.enableLogger(require('redux-node-logger')())
manager.getStore()
 
const username = 'root'
const password = 'toor'
 
apiUsers.create({ username, password })
 
import { authConfigAPISocketServer } from 'redux-security'
 
authConfigAPISocketServer({ io })

client.js

require('redux-journal').enable()
 
import injectTapEventPlugin from 'react-tap-event-plugin'
 
injectTapEventPlugin()
 
import { manager }          from 'redux-manager'
import { authSocket }       from 'redux-security'
 
const serviceAuth = 'auth'
 
const socket = require('socket.io-client')('http://localhost:3000')
authSocket({ serviceName: serviceAuth, socket })
 
manager.enableLogger(require('redux-logger')())
const store = manager.getStore()
 
import React                from 'react'
import ReactDOM             from 'react-dom'
import { Provider }         from 'react-redux'
import { connect }          from 'react-redux'
 
import MuiThemeProvider     from 'material-ui/styles/MuiThemeProvider'
import getMuiTheme          from 'material-ui/styles/getMuiTheme'
import AppBar               from 'material-ui/AppBar'
 
import { AuthAvatar }       from 'redux-security'
import { AuthFormTabs }     from 'redux-security'
 
const AuthAvatar1         = connect(state => ({ auth: state[serviceAuth] }))(AuthAvatar)
const AuthFormTabs1       = connect(state => ({ auth: state[serviceAuth] }))(AuthFormTabs)
 
const muiTheme = getMuiTheme({ palette: { accent1Color: require('material-ui/styles/colors').deepOrange500 }})
 
const render = () => ReactDOM.render(
  <Provider store={ store }>
    <MuiThemeProvider muiTheme={ muiTheme }>
      <div>
        <AppBar
          title='redux-security'
          iconElementLeft={ <div/> }
          iconElementRight={ <AuthAvatar1/> }
        />
        <AuthFormTabs1/>
      </div>
    </MuiThemeProvider>
  </Provider>,
  document.getElementById('app')
)
 
render()

Package Sidebar

Install

npm i redux-security

Weekly Downloads

1

Version

0.0.15

License

MIT

Last publish

Collaborators

  • lokhmakov