Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tobbbe/authur
https://github.com/tobbbe/authur
oauth oauth2 umbraco
Last synced: 11 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/tobbbe/authur
- Owner: tobbbe
- Created: 2019-03-14T13:30:45.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-01-22T12:26:43.000Z (almost 5 years ago)
- Last Synced: 2024-10-20T08:11:51.449Z (15 days ago)
- Topics: oauth, oauth2, umbraco
- Language: JavaScript
- Size: 34.2 KB
- Stars: 6
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Super simple oauth2 helper. Works well with [umbraco-authu](https://github.com/mattbrailsford/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
```javascript
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
```javascript
const { ok, error } = await auth.authenticate({ username, password });
```## Subscribe to events
You can subscribe to auth state changes anywhere:```javascript
const unsubscribe = auth.onAuthStateChange(status => console.log('auth status changed to:', status));// later
unsubscribe();
```## Signout
```javascript
auth.signout();
```## Fetch helper
Just a wrapper around [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_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````javascript
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.```javascript
const token = await auth.getToken()
```## Check if user is authenticated
```javascript
const isAuthenticated = await auth.isAuthenticated()
```# Examples
[react-hooks](https://github.com/tobbbe/authur/blob/master/examples/react-hooks.js)
[react-redux](https://github.com/tobbbe/authur/blob/master/examples/react-redux.js)