@dataunion/authentication
v1.2.0
Published
Authenticate users so they can use the DataUnion SDK
Downloads
5
Maintainers
Readme
@dataunion/authentication
Introduction
In just five minutes, you can configure your app to use DataUnion's API endpoints! @dataunion-authentication is a JavaScript plugin for simplifying the DataUnion authentication process.
Click here to get started with our examples in React and Vanilla JS.
If you want to add more examples (i.e. Angular, Vue, React Native) please create a pull request.
Get Started 🌱
Step 1 to using any DataUnion API is injecting web3. You can inject web3 into your project manually, or you can use our function injectWeb3()
, found on the DataUnionWeb3
class.
Step 2 to using any DataUnion API is logging your user in by getting them access tokens, which is done with the fullLogin()
function on the duAuth
import (duAuth
is an instance of the class DataUnionAuth
).
Step 3 is setting a refresh cycle so that the user's token never expires, which is done with the refresh()
function on the duAuth
class.
The following code is all that's needed to log a user in:
import duAuth, { duWeb3Injecter } from '@dataunion/authentication';
var accessToken;
var refreshToken;
// duWeb3Injecter is an instance of the class, 'DataUnionWeb3'
// this function makes window.web3 defined.
var ethAddress = await duWeb3Injecter.enableEthereumAndGetAddress();
// Log the user in
if (window.web3 != undefined) {
var loginTokens = await duAuth.fullLogin(ethAddress);
accessToken = loginTokens.accessToken;
refreshToken = loginTokens.refreshToken;
}
// refresh their token every 15 minutes
setTimeout(() => {
var newTokens = await duAuth.refresh(refreshToken);
accessToken = newTokens.accessToken;
}, 900000)
There you have it! Your logged in user with an automatically refreshing login. You can now use accessToken
to access the entire DataUnion SDK.
You will need to handle the security of the user's tokens on your app.
See examples of this library in simple apps below.
Examples 🧜♀️
Documentation 🌸
For the expanded docs, click here. If you have further questions do not hesitate to join our Discord and Telegram and reach out to the technical team. We are happy to help!
@dataunion/authentication contains the following classes:
import { DataUnionAuth, DataUnionWeb3, ApiCalls } from '@dataunion/authentication'
var customBackendUrl = 'https://crab.dataunion.app/';
var logErrors = true;
const duAuth = new DataUnionAuth(logErrors);
const apiCalls = new ApiCalls(customBackendUrl);
const duWeb3 = new DataUnionWeb3();
@dataunion/authentication contains the following functions under DataUnionAuth()
, for controlling user log in:
duAuth.registerNewAddress(ethAddress)
- Calls/register
API, returnsnonce
.duAuth.getNonceForExistingAddress(ethAddress)
- Calls/get-nonce
API, returnsnonce
.duAuth.registerOrGetNonceAuto(ethAddress, registerFirst)
- Calls/register
, if that fails, calls/get-nonce
. You can choose to try/register
or/get-nonce
first, using theregisterFirst
boolean parameter. Returnsnonce
.duAuth.getSignature(ethAddress, nonce)
- calls the web3 sign method. Requires web3 to be initialized in the browser;window.web3
must be defined. Returnssignature
.duAuth.getJWTToken(signature)
- Calls/login
API with the given signature. Returnstokens
.duAuth.fullLogin(ethAddress)
- Calls all the necessary above APIs; automates the login process, for the givenethAddress
. Returnstokens
.duAuth.refresh(refreshToken)
- Calls/refresh
API with the givenrefreshToken
. Returnstokens
, provided therefreshToken
is valid.
@dataunion/authentication contains the following functions under ApiCalls()
, for calling authentication APIs directly:
apiCalls.register(ethAddress, logErrors=false)
- Calls/register
API. Returns anonce
.apiCalls.getNonce(ethAddress, logErrors=false)
- Calls/get-nonce
API. Returns anonce
.apiCalls.sign(ethAddress, nonce, logErrors=false)
- Callswindow.web3.eth.personal
API. Requires thatwindow.web3
is initialized. Returns asignature
.apiCalls.login(ethAddress, signature, logErrors=false)
- Calls/login
API. Returnstokens
.apiCalls.refresh(refreshToken, logErrors=false)
- Calls/refresh
API. Returnstokens
.
@dataunion/authentication contains the following functions under DataUnionWeb3()
, for initializing Web3:
duWeb3.enableEthereumAndGetAddress()
- Injects web3 through thewindow.ethereum
provider, returns checksum address if the user is logged in with Metamask.
Pull Requests - Click here 📝
If you want to add new examples (i.e. for injecting the library into other JavaScript frameworks), we will add you to the credits section. Feel free to open a pull request and explain briefly what your example does.
Publishing New Version 🤸
If you have permission and are logged in to the npmjs registry, publish a new version with the following commands:
npm run-script build
ornpm run-script build-windows
cd dist
- Remove the
"files"
option fromdist/package.json
and change the version to the next number. npm publish