Skip to content

tobbbe/authur

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

53 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

No description or website provided.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published