# mushroomjs-auth

> Extension Authentication for mushroomjs

Latest version **1.1.0** (published 2023-02-02) · ISC license · 0 weekly downloads

## Install

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

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2023-02-02 |
| First published | 2020-01-10 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 32.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | suco2007 |
| Maintainers | suco2007 |
| Keywords | mushroom, auth, authentication |

## Links

- npm: https://www.npmjs.com/package/mushroomjs-auth
- npm.io page: https://npm.io/package/mushroomjs-auth

## Dependencies (1)

- [mushroomjs](https://npm.io/package/mushroomjs.md) ^1.1.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

- 1.1.0 (latest) — 2023-02-02
- 1.1.0-alpha.7 (alpha) — 2022-07-24
- 1.1.0-alpha.6 — 2022-01-10
- 1.1.0-alpha.5 — 2021-12-14
- 1.1.0-alpha.4 — 2021-04-29
- 1.1.0-alpha.3 — 2021-04-13
- 1.1.0-alpha.2 — 2021-04-09
- 1.1.0-alpha.1 — 2021-04-09
- 1.0.4 — 2020-04-06
- 1.0.3 — 2020-03-27
- 1.0.2 — 2020-01-13
- 1.0.1 — 2020-01-13
- 1.0.0 — 2020-01-10

## README

# Install

    npm install mushroomjs-auth

Or

    yarn add mushroomjs-auth

# Dependent on

    mushroomjs

# Import

    import mushroom from "mushroomjs-auth";

Or

    import mushroom from "mushroomjs";
    import "mushroomjs-auth";

# Usage

## Login

    const remember = true; // true - no expired, false - will auto expired after token_time_to_live minute(s) inactive
    const token = await mushroom.$auth.loginAsync("your account", "your password", remember);

See more about [token_time_to_live](#authtoken_time_to_live)

## Logout

    // invalid session at this device of client
    await mushroom.$auth.logoutAsync({ mode: LogoutMode.InvalidClientSession });
    // invalid session at this device of client and other devices of client that use the same token (shared token)
    await mushroom.$auth.logoutAsync({ mode: LogoutMode.invalidServerSession });
    // invalid all sessions of user
    await mushroom.$auth.logoutAsync({ mode: LogoutMode.invalidServerSession });

## Status

    const status = await mushroom.$auth.statusAsync();

## Me

    const me = await mushroom.$auth.meAsync();

## Register

    const result = await mushroom.$auth.registerAsync("your account", "your password");

## Activate

    await mushroom.$auth.activateAsync("your account", "activation code");

## Recover password

    await mushroom.$auth.recoverPasswordAsync("your account");

## Reset password

    await mushroom.$auth.resetPasswordAsync("your account", "your code", "your new password");

## Change password

    await mushroom.$auth.changePasswordAsync("your account", "your old password", "your new password")

# Events

    mushroom._on("$auth.ready", () => { /* handle ready event here */ });
    mushroom._on("$auth.loggedin", () => { /* handle loggedin event here */ });
    mushroom._on("$auth.loggedout", () => { /* handle loggedout event here */ });

# State

`mushroom.$auth.isReady` will be `true` right before `ready` event is fired

# Manually set token

    const permanent = true; // true - no expired, false - will auto expired after token_time_to_live minute(s) inactive
    await mushroom.$auth.setTokenAync("your token", permanent);

# Settings

## auth.token_time_to_live

Settings the number of minute(s) that token will be expired after inactive (when parameter `remember` is `false` in `loginAsync` method).

    mushroom.$setting.set("auth.token_time_to_live", 5); // default is 30 minutes

# Override

To override the built-in methods above, define your custom methods like:

    import mushroom, { createRestfulAsyncFunction } from "mushroomjs";

    mushroom.$auth.registerAsync = (account, password, type) => createRestfulAsyncFunction<RegisterResponse>({
        name: "$auth.register", // Identify your method
        method: "POST",
        blankBody: false,
        url: mushroom.using() + "custom register url"
    })({ user: account, pwd: password, type, x: 1, y: 2 });

# Release notes

## 1.1.0

New features:

* Support TypeScript
* Compatible with `mushroomjs@1.1.*`
* Add setting `auth.token_time_to_live`

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