# samjs-auth

> Adds a configs-based user management, authentification mechanismen and authorization system for configs.

Latest version **0.4.0** (published 2017-07-17) · 0 weekly downloads

## Install

```sh
npm install samjs-auth
pnpm add samjs-auth
yarn add samjs-auth
bun add samjs-auth
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.0 |
| Published | 2017-07-17 |
| First published | 2015-12-21 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | * |
| Dependencies | 2 |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | Paul Pflugradt |
| Maintainers | paulpflug |

## Links

- npm: https://www.npmjs.com/package/samjs-auth
- Repository: https://github.com/SAMjs/samjs-auth
- Homepage: https://github.com/SAMjs/samjs-auth#readme
- Issues: https://github.com/SAMjs/samjs-auth/issues
- npm.io page: https://npm.io/package/samjs-auth

## Dependencies (2)

- [bcrypt](https://npm.io/package/bcrypt.md) ^1.0.2
- [component-emitter](https://npm.io/package/component-emitter.md) ^1.2.1

## Recent versions

- 0.4.0 (latest) — 2017-07-17
- 0.3.0 — 2017-04-03
- 0.2.4 — 2017-03-29
- 0.2.3 — 2017-03-28
- 0.2.2 — 2016-08-05
- 0.2.1 — 2016-01-21
- 0.2.0 — 2015-12-21

## README

# samjs-auth

Adds a configs-based user management, authentification mechanismen and authorization system for configs.

Client: [samjs-auth-client](https://github.com/SAMjs/samjs-auth-client)

## Getting Started
```sh
npm install --save samjs-auth
npm install --save-dev samjs-auth-client
```

## Usage

```js
// server-side
samjs
.plugins(require("samjs-auth")({dev:process.env.NODE_ENV !== "production")})
.options()
.configs({name:"item", read:true ,write:"root"})
.models()
.startup(server)

// client-side
samjs.plugins(require("samjs-auth-client"))

// one-time configuration
samjs.auth.createRoot("somePWD")

samjs.config.set("item","someValue") // will fail
// authentication
samjs.auth.login({name:"root",pwd:"somePWD"})
.then(function() {
  // success
  // will add a token to browser store to automatically login next time
  samjs.config.set("item","someValue") // will work
  samjs.auth.logout() // to logout and close session (token will be deleted)
})

// will use a token from the browser store to authenticate,
// works when session is still alive
samjs.auth.login()
```

### default options

```coffee
saltWorkFactor: 10 # see bcrypt (security)
tokenExpiration: 1000*60*30 # 30 minutes / server-side session length
tokenSize: 48 # (security)
username: "name" # name of the name prop of a user
password: "pwd" # name of the password prop of a user
rootUser: "root" # name of root user
permissionChecker: "containsUser" # default permissionChecker
```

### permissionCheckers

per default only has `containsUser`, this permission checker will allow acced for username `root`, when the permission is
`true`, `"root"` or `["root"]`

Add your own permission checker:
```js
samjs.auth.permissionCheckers.simple = function(permission, user){
  if (permission == true) {
    return true
  }
  return false
}
```

### default config

samjs-auth adds a `users` config object, which gives `rootUser` read and write access

## API: replaceUserHandler

to replace the user management. Will remove the `users` config object.
(see [samjs-auth-mongo](https://github.com/SAMjs/samjs-auth-mongo) for an example)
```js
samjs.auth.replaceUserHandler(function(username){
  // somehow get a user object by username
  return user
})
```

---
_Source: https://npm.io/package/samjs-auth · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
