casted-auth-js-sdk
v1.0.12
Published
This sdk contains modules/services which provide functionality that interacts with Alpha Kit services.
Downloads
21
Keywords
Readme
auth-js-sdk
This sdk contains modules/services which provide functionality that interacts with Alpha Kit services.
// IMPORT
import { Module } from 'auth-js-sdk'
import Module from 'auth-js-sdk/Module'
import authJsSdk from 'auth-js-sdk'
authJsSdk.Module
Auth
The Auth service contains convienent methods for interacting with the Authentication service. The Auth service also manages its own state through localstorage and getter/setter properties on the service. These state properties include accessToken
, refreshToken
, clientId
and userId
.
Usage
import { Auth } from 'auth-js-sdk'
// Instantiate a new Auth service
const client = new Auth({
baseURL: 'http://localhost:8081', // The URL of the Auth service in the backend
storagePrefix: 'auth_' // The prefix to use for the localstorage state items
})
// Properties
console.log(client.config) // The config passed into the constructor
// => <OBJECT> { baseURL: 'http://localhost:8081', storagePrefix: 'auth_' }
console.log(client.accessToken) // The accessToken managed by the Auth service, persisted in localStorage
// => <JWT> some.jwt.blah
console.log(client.refreshToken) // The refreshToken managed by the Auth service, persisted in localStorage
// => <JWT> some.jwt.blah
console.log(client.clientId) // The clientId managed by the Auth service, persisted in localStorage
// => <GUID> 17b9cb4a-e416-478a-b5d3-be1ac7ce000f
console.log(client.userId) // The userId managed by the Auth service, persisted in localStorage
// => <NUMBER> 1
// Methods
// Sends basic auth parameters (username, password) as login credentials to the Auth service.
// This will return (with a promise) the user as well as a flag which says whether or not there needs to be more verification.
// If no verification is needed, this will also return the access token and refresh token. Otherwise those tokens are excluded.
client.signInLocal(username, password).then((data) => {
console.log(data) // => { user, needsVerification, accessToken?, refreshToken? }
})
// Will clear out localStorage auth state as well as make a request to the Auth service to clear out any long term session data
client.signOut().then((response) => {
console.log(response) // => The http response object
})
// Calls the Auth service's /token endpoint to get a new access token from the given refreshToken.
// Will store the new accessToken in localStorage as well return it in a Promise
client.refresh().then((accessToken) => {
console.log(accessToken) // => <JWT> some.jwt.blah
})
// Create a new user with the given username/password as credentials, the given user object for the details and an optional key used for provisioning.
// Returns a Promise with the created user.
client.postUser(username, password, user, key).then((savedUser) => {
console.log(savedUser) // => { id: 2, first_name: 'john', last_name: 'smith', email: '...', phone: '...' }
})
// Creates and stores a temporary password reset code for the matching user, and sends an email with a link to reset their password.
client.sendResetEmail(email).then((response) => {
console.log(response) // => The http response object
})
// Sends the new password along with the user's id and temp reset code to the Auth service to update the user's password.
client.resetPassword(identifier, password, code).then((response) => {
console.log(response) // => The http response object
})
// Tells the Auth service to send a 2fa verification code to the user with the given id using the given delivery options.
// Delivery method should be one of 'sms' | 'email' | 'voice'.
// Delivery value should be the cooresponding identifier for the chosen method (such as the phone number).
client.sendVericationCode(userId, deliveryMethod, deliveryValue).then((response) => {
console.log(response) // => The http response object
})
// Will send the user's id and chosen delivery options as well the submitted verification code to check that they are verified.
// Returns a Promise with both the refreshToken and accessToken for the user if successful.
client.checkVerificationCode(userId, deliveryMethod, deliveryValue, code).then((tokens) => {
console.log(tokens) // => { accessToken, refreshToken }
})