# just-login-core

> Handles the authentication at the database level for other just login modules

Latest version **2.0.1** (published 2015-07-15) · VOL license · 0 weekly downloads

## Install

```sh
npm install just-login-core
pnpm add just-login-core
yarn add just-login-core
bun add just-login-core
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.1 |
| Published | 2015-07-15 |
| First published | 2014-07-01 |
| Weekly downloads | 0 |
| License | VOL |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 10 |
| Author | ArtskydJ |
| Maintainers | artskydj, tehshrike |
| Keywords | just login, login, authentication |

## Links

- npm: https://www.npmjs.com/package/just-login-core
- Repository: https://github.com/coding-in-the-wild/just-login-core
- Homepage: https://github.com/coding-in-the-wild/just-login-core#readme
- Issues: https://github.com/coding-in-the-wild/just-login-core/issues
- npm.io page: https://npm.io/package/just-login-core

## Dependencies (6)

- [xtend](https://npm.io/package/xtend.md) ^4.0.0
- [mutexify](https://npm.io/package/mutexify.md) ^1.0.1
- [key-master](https://npm.io/package/key-master.md) ^1.1.0
- [random-uuid-v4](https://npm.io/package/random-uuid-v4.md) ^0.0.5
- [tiny-level-ttl](https://npm.io/package/tiny-level-ttl.md) ^3.1.1
- [safe-json-parse](https://npm.io/package/safe-json-parse.md) ^4.0.0

## Alternatives

- [@clerk/clerk-expo](https://npm.io/package/@clerk/clerk-expo.md) — 133.6K weekly downloads
- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads

## Recent versions

- 2.0.1 (latest) — 2015-07-15
- 2.0.0 — 2015-05-19
- 1.3.2 — 2015-02-16
- 1.3.1 — 2015-01-17
- 1.3.0 — 2015-01-16
- 1.2.7 — 2014-11-12
- 1.2.6 — 2014-11-12
- 1.2.5 — 2014-11-01
- 1.2.4 — 2014-11-01
- 1.2.3 — 2014-10-28
- 1.2.2 — 2014-10-14
- 1.2.1 — 2014-10-14
- 1.2.0 — 2014-10-14
- 1.1.4 — 2014-09-26
- 1.1.3 — 2014-09-20
- … 13 more at https://npm.io/package/just-login-core/versions

## README

just-login-core
===============

[![Build Status](https://travis-ci.org/coding-in-the-wild/just-login-core.svg)](https://travis-ci.org/coding-in-the-wild/just-login-core)

Handles tokens for just-login.

# Example

```js
var JustLoginCore = require('just-login-core')
var db = require('level-mem')()
var core = JustLoginCore(db)
```

# API

```js
var Core = require('just-login-core')
```

## `var core = JustLoginCore(db[, options])`

- `db` is expecting a levelup database.
- `options` is an object that holds the (**gasp**) options!
	- `tokenGenerator` is expecting a function that returns an unique string each time it is called. This is used for token generation. Defaults to a UUID generator.
	- `tokenTtl` is a number in milliseconds of a token's Time To Live (TTL). Defaults to 5 minutes.
	- `tokenTtlCheckIntervalMs` is a number in milliseconds of the ttl's check interval. (See [tiny-level-ttl][tinyttl], `checkInterval`.) Defaults to 10 seconds.
- Returns `core`.

## `core`

It emits some [events](#core-events) and has a few methods:

## `core.beginAuthentication(sessionId, contactAddress[, cb])`

Starts the authentication process by emitting the 'authentication initiated' event with a token and the contact address.

Something else must listen for the event, and send a message to the user. See [`core` events](#core-events) for more information.

- `sessionId` is a string of the session id that is trying to get authenticated.
- `contactAddress` is string of the user's contact info, (usually an email address).
- `cb` is a function with the following arguments:
	- `err` is an Error object or null.
	- `authReqInfo` is an object with the authentication request information. The object is identical to the object emitted in the event, with the following properties:
		- `contactAddress` is a string with the contact address.
		- `token` is a string of the token.
- Emits `core.on('authentication initiated', function (authReqInfo) { ... })`

```js
core.beginAuthentication('session id', 'fake@example.com', function (err, authReqInfo) {
	if (!err) {
		console.log(authReqInfo.token) //logs the token
		console.log(authReqInfo.contactAddress) //logs: "fake@example.com"
	}
})
```

## `core.authenticate(token[, cb])`

Authenticates the token, and calls back with the session id and contact address associated with that token. Then the token and it's associated data is deleted. A token can only be authenticated once.

- `token` is a string of the token that is trying to get authenticated.
- `cb` is a function with the following arguments:
	- `err` is an Error object or null.
	- `credentials` is null is the user is not authenticated, and is an object if they are authenticated:
		-  `contactAddress` is a string of their contact address.
		-  `sessionId` is a string of their session id.
- Emits `core.on('authenticated', function (credentials) { ... })`

```js
core.authenticate('the token', function(err, credentials) {
	if (!err) {
		console.log(credentials.contactAddress + ' is now logged in! Congratulations!')
	} else {
		console.log('Sorry, for some reason you are not logged in.')
	}
})
```

# `core` events

### `authentication initiated`

Emitted when `beginAuthentication()` is called. (Which should be when the user clicks the "login" button.)

```js
core.on('authentication initiated', function (authReqInfo) {
	console.log(authReqInfo.contactAddress)
	console.log(authReqInfo.token)
})
```

_(You can use [just-login-emailer][jlemailer] to catch this event.)_

### `authenticated`

Emitted when `core.authenticate()` is successful.

```js
core.on('authenticated', function (credentials) {
	console.log(credentials.contactAddress)
	console.log(credentials.sessionId)
})
```

# Install

Install with [npm](http://nodejs.org):

	npm install just-login-core

# License

[VOL](http://veryopenlicense.com/)


[beginauth]: #corebeginauthenticationsessionid-contactaddress-cb
[auth]: #coreauthenticatetoken-cb
[tinyttl]: https://github.com/ArtskydJ/tiny-level-ttl#ttldb-opts
[checkint]: https://github.com/tehshrike/expire-unused-keys#timeoutms-db-checkintervalms
[jlemailer]: https://github.com/coding-in-the-wild/just-login-emailer

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