decentraland-auth
JavaScript client for the auth-service
Installation
$ npm i decentraland-auth
Usage
Create an Auth
instance, login and then get access tokens whenever you need them
Send signed request
await auth.login // GET // POST
Generate credentials for message
This library makes use of Buffer
, which is not present natively in the browser. There's a polyfill that is included by default by some bundlers (like webpack), but if you don't have it make sure to add it to your project: Buffer.
API
-
new Auth([options])
: Returns a new instance ofAuth
. It takes an optionaloptions
objects that can contain the following properties:-
ephemeralKeyTTL
: Time to live for the ephemeral key (in seconds). Default value is60 * 60 * 2
(2 hours). -
api
: An object with options for the underlyingAPI
instance:-
baseURL
: The base url of theauth-service
. Default value ishttps://auth.decentraland.zone/api/v1
. -
loginCallback
: The login callback url. It defaults to/callback
. -
logoutCallback
: The logout callback url. It defaults to/
.
-
-
-
auth.login([target])
: Returns a promise that will resolve once the user is logged in. The first time it's called it will prompt the user to login though a Popup. If atarget
dom node is provided, instead of a Popup it will insert an iframe inside the target node and use that. If the user closes the Popup the promise will reject. If the user session is still active this method might resolve without having to open a popup. -
auth.isLoggedIn()
: Returns a boolean telling wheter the user is logged in or not. -
auth.getAccessToken()
: It returns a promise that resolves to an access token. This access token has a short life so it is recommended to get a new token every time you need to use is instead of storing it. -
auth.getAccessTokenData()
: It returns a promise that resolves to the payload of the access token (basically the decoded JWT). -
auth.logout()
: It returns a promise that resolves once the user is logged out. After using this, the next time thelogin()
method is called it will prompt the user with the login flow. -
auth.createRequest(url, options?)
: It returns a promise that resolves to aRequest
object that can be used withfetch
. It takes a URL and the same options asfetch
. -
auth.createHeaders(url, options?)
: It returns a promise that resolves to an object containing the mandatory headers to be used in a signed request. It takes a URL and the same options asfetch
. -
auth.getUserToken()
: It returns a promise that resolves to theuserToken
. This token is the one used to generate theaccessToken
(s). -
auth.getEphemeralKey()
: Returns the instance of the ephemeral key. -
auth.dispose()
: It removes all the bindings on this instance. It does NOT perform a logout.