GithubHelp home page GithubHelp logo

authur's Introduction

Super simple oauth2 helper. Works well with umbraco-authu. Can be used in a browser (localstorage) or react-native (AsyncStorage) etc by setting persistenceGet, persistenceSet and persistenceClear.

Requirements

  • ES6
  • async/await
  • fetch

Installation

npm install authur

Configuration

import auth from 'authur';

auth.initialize({
	origin: 'https://your-website.s1.umbraco.io',
	authPath: '/oauth/token',
	apiPath: '/umbraco/api',
	persistenceGet: key => localStorage.getItem(key),
	persistenceSet: (key, val) => localStorage.setItem(key, val),
	persistenceClear: (storageKey) => localStorage.removeItem(storageKey),
	debug: true,
	events: {
		onAuthStateChange: status => console.log('auth status changed to:', status))
	}
})

Login

const { ok, error } = await auth.authenticate({ username, password });

Subscribe to events

You can subscribe to auth state changes anywhere:

const unsubscribe = auth.onAuthStateChange(status => console.log('auth status changed to:', status));

// later
unsubscribe();

Signout

auth.signout();

Fetch helper

Just a wrapper around fetch that:

  • Appends a valid token to request and call signout() if 401 is returned from server
  • Defaults to 'GET' if no options are passed
  • Appends api path if it is set in auth.initialize
const resp = await auth.fetch('/news/list');
const content = await resp.json();

Get token

Returns a valid token if possible. Will automagiclly refresh if needed.

const token = await auth.getToken()

Check if user is authenticated

const isAuthenticated = await auth.isAuthenticated()

Examples

react-hooks
react-redux

authur's People

Contributors

tobbbe avatar

Stargazers

Gustaf Levin avatar Richard Scoop avatar Ulf Möllerström avatar Nick Hoàng avatar  avatar Martin Amsinck  avatar

Watchers

James Cloos avatar  avatar Ulf Möllerström avatar Gustaf Levin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.