# bs-auth0-session

> Session management for ReasonReact using Auth0 for login.

Latest version **1.0.4** (published 2018-11-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install bs-auth0-session
pnpm add bs-auth0-session
yarn add bs-auth0-session
bun add bs-auth0-session
```

## 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 | 1.0.4 |
| Published | 2018-11-25 |
| First published | 2018-11-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 25.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Matthew Scharley |
| Maintainers | mscharley |
| Keywords | reason, reasonml, ocaml, bucklescript, auth0 |

## Links

- npm: https://www.npmjs.com/package/bs-auth0-session
- Repository: https://github.com/mscharley/bs-auth0-session
- Homepage: https://github.com/mscharley/bs-auth0-session#readme
- Issues: https://github.com/mscharley/bs-auth0-session/issues
- npm.io page: https://npm.io/package/bs-auth0-session

## 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.0.4 (latest) — 2018-11-25
- 1.0.3 — 2018-11-24
- 1.0.2 — 2018-11-21
- 1.0.1 — 2018-11-21
- 1.0.0 — 2018-11-21

## README

# bs-auth0-session

[![npm](https://img.shields.io/npm/v/bs-auth0-session.svg)](https://www.npmjs.com/package/bs-auth0-session)

**Source:** [https://github.com/mscharley/bs-auth0-session](https://github.com/mscharley/bs-auth0-session)  
**Author:** Matthew Scharley  
**Contributors:** [See contributors on GitHub][gh-contrib]  
**Bugs/Support:** [Github Issues][gh-issues]  
**Copyright:** 2018  
**License:** [MIT license][license]  
**Status:** Active

## Synopsis

Session management for ReasonReact using Auth0 for login.

## Usage

This is a rather long example, but it should be complete and functional.

```reason
/* Util.re */
[@bs.val] [@bs.scope ("window", "location")]
external locationOrigin: string = "origin";

let urlToString = url =>
  (
    switch (url.React.Router.path) {
    | [] => "/"
    | _ =>
      url.React.Router.path->Belt.List.reduce("", (a, b) => a ++ "/" ++ b)
    }
  )
  ++ (url.search != "" ? "?" ++ url.search : "")
  ++ (url.hash != "" ? "#" ++ url.hash : "");

/* Router.re */
open BsAuth0Session;

type page =
  | Home
  | Callback
  | Logout
  | Login;

type state = {
  page: option(page),
  returnUrl: string,
};

type action =
  | Navigate(option(page));

let routerFn: ReasonReact.Router.url => option(page) =
  fun
  | {path: []} => Some(Home)
  | {path: ["oauth", "callback"]} => Some(Callback)
  | {path: ["logout"]} => Some(Logout)
  | {path: ["login"]} => Some(Login)
  | _ => None;

let component = ReasonReact.reducerComponent("Router");

let make = _children => {
  let router = (url, {ReasonReact.send}) => url->routerFn->Navigate;

  {
    ...component,
    initialState: _ => {
      let initialUrl = ReasonReact.Router.dangerouslyGetInitialUrl();
      {
        page: initialUrl->routerFn,
        returnUrl: initialUrl->Util.urlToString,
      }
    },
    didMount: self => {
      let watcherId = ReasonReact.Router.watchUrl(self.handle(router));
      self.onUnmount(_ => ReasonReact.Router.unwatchUrl(watcherId));
    },
    reducer: (action, state) =>
      switch (action) {
      | Navigate(page) => Update({...state, page})
      },
    render: ({state: {page, returnUrl}}) =>
      <SessionContext.Provider
        domain="my-domain.au.auth0.com"
        clientId="dJ9HRAcoottSxYnYourClientIdHere"
        callbackUrl={Util.locationOrigin ++ "/oauth/callback"}>
          <SessionContext.LoggedOutConsumer>
            ...{
                pending =>
                  switch (page, pending) {
                  | (Some(Logout), _) =>
                    <LogoutPage returnUrl={Util.locationOrigin ++ "/"} /> /* !! Provided !! */
                  | (Some(Callback), _) =>
                    <CallbackPage /> /* !! Provided !! */
                  | (_, true) => <LoadingPage />
                  | (_, false) =>
                    <Button
                      onClick={
                        _ev => session->Session.doLogin(~returnUrl, ())
                      }>
                      {ReasonReact.string("Login")}
                    </Button>
                  }
              }
          </SessionContext.LoggedOutConsumer>
          <SessionContext.LoggedInConsumer>
            ...{
                _session =>
                  switch (page) {
                  | Some(Logout) => <LogoutPage returnUrl={Util.locationOrigin ++ "/"} /> /* !! Provided !! */
                  | Some(Home) => <HomePage />
                  | Some(Callback) => React.null
                  | Some(Login) =>
                    React.Router.push("/");
                    React.null;
                  | None => <NotFoundPage />
                  }
              }
          </SessionContext.LoggedInConsumer>
      </SessionContext.Provider>,
  }
}
```

There's a whole suite of Consumers for all sorts of use-cases. There is also `SessionContext.Consumer` which is your escape hatch which exposes the raw session data that this library tracks.

Check [`SessionContext.rei`][sc-rei] for more details on the available consumers.

  [gh-contrib]: https://github.com/mscharley/bs-auth0-session/graphs/contributors
  [gh-issues]: https://github.com/mscharley/bs-auth0-session/issues
  [license]: https://github.com/mscharley/bs-auth0-session/blob/master/LICENSE
  [sc-rei]: https://github.com/mscharley/bs-auth0-session/blob/master/src/SessionContext.rei

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