Skip to content

Cado-Labs/auther-client

Repository files navigation

@cadolabs/auther-client · Supported by Cado Labs

A frontend client for working with the Auther gem.

Getting Started

Installation

npm i @cadolabs/auther-client

or

yarn add @cadolabs/auther-client

Configure

Create an AutherClient instance before initializing your application.

import { AutherClient } from "@cadolabs/auther-client"

const auth = AutherClient.init({
  autherUrl: "<AUTHER_DOMAIN>",
  redirectUri: "<CALLBACK_URL>",
  appcode: "<APP_CODE>", // your app unique id
})

Usage

Logging In

To redirect to the auther login

auth.login()

And after that you redirect to the callback route <CALLBACK_URL> with query string authorization_code="12345".

Get tokens

//async/await
try {
  const authorizationCode = "12345"

  const response = await auth.fetchTokens(authorizationCode) // return Promise
  const tokens = response.json()

  const { accessToken, refreshToken } = tokens
  ...
} catch (error) {
  throw Error(error.message) // invalid.authorization_code
}

Update tokens

const refreshToken = "refresh_token"

//async/await
try {
  const response = await auth.updateTokens(refreshToken) // return Promise
  const tokens = response.json()

  const { accessToken, refreshToken } = tokens
} catch (error) {
  throw Error(error.message) // invalid.access_token
}

Loggout In

To make a request to revoke tokens

const accessToken = "access_token"

//async/await
try {
  await auth.logout(accessToken)
  ...
} catch (error) {
  throw Error(error.message) // invalid.access_token
}

Authentication

Authentication method for verifying access and refresh tokens and scheduling tokens refreshing.

const getTokens = () => {
  const accessToken = localStorage.getItem("accessToken")
  const refreshToken = localStorage.getItem("refreshToken")

  return { accessToken, refreshToken }
}

const saveTokens = ({ accessToken, refreshToken }) => {
  localStorage.setItem("accessToken", accessToken)
  localStorage.setItem("refreshToken", refreshToken)
}

//async/await
try {
  await auth.authentication({ getTokens, saveTokens })
  ...
} catch (error) {
  throw Error(error.message) // token.not_found
}

Additional usage

Decode token

To decode the token

import { decode } from "@cadolabs/auther-client"

const testToken = "eyJhbGc*.NHVaY*.i8ZJd8_-RU8V" // headers.payload.signature

const decodedToken = decode(testToken)

console.log(decodedToken)

/* console prints:
 * {
 *   header: {
 *     alg: "RS256",
 *     typ: "JWT"
 *   },
 *   payload: {
 *     sub: "1234567890",
 *     name: "John Doe",
 *     iat: 1516239022,
 *     exp: 1516239022,
 * },
 *   signature: "i8ZJd8_-RU8V",
 * }
*/

Return the decoded object with parameters

Verify token

Checks the token for errors or expired time. If there are no problems, return it back

import { verify } from "@cadolabs/auther-client"

const testToken = "eyJhbGc*.NHVaY*.i8ZJd8_-RU8V" // headers.payload.signature

try {
  const verifiedToken = verify(testToken)

  console.log(verifiedToken)

  /* console prints if no problems:
  * "eyJhbGc*.NHVaY*.i8ZJd8_-RU8V"
  */
} catch (error) {
  console.log(error.message)
  /* console prints if token is expired:
  * "token.expired"
  */
}

Contributing

  • Fork it ( https://github.com/Cado-Labs/auther-client )
  • Create your feature branch (git checkout -b feature/my-new-feature)
  • Commit your changes (git commit -am '[feature_context] Add some feature')
  • Push to the branch (git push origin feature/my-new-feature)
  • Create new Pull Request

License

Released under MIT License.

Supporting

Supported by Cado Labs

Authors

Sergey Andreev

About

Client for working with Auther

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •