0.2.18 • Published 4 years ago

authur v0.2.18

Weekly downloads
31
License
-
Repository
github
Last release
4 years ago

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

0.2.18

4 years ago

0.2.17

4 years ago

0.2.16

4 years ago

0.2.15

4 years ago

0.2.14

4 years ago

0.2.13

4 years ago

0.2.12

5 years ago

0.2.11

5 years ago

0.2.10

5 years ago