Socket
Socket
Sign inDemoInstall

bs-auth0-session

Package Overview
Dependencies
65
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    bs-auth0-session

Session management for ReasonReact using Auth0 for login.


Version published
Maintainers
1
Install size
25.8 kB
Created

Readme

Source

bs-auth0-session

npm

Source: https://github.com/mscharley/bs-auth0-session
Author: Matthew Scharley
Contributors: See contributors on GitHub
Bugs/Support: Github Issues
Copyright: 2018
License: MIT 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.

/* 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 for more details on the available consumers.

Keywords

FAQs

Last updated on 25 Nov 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc