A frontend client for working with the Auther gem.
npm i @cadolabs/auther-client
or
yarn add @cadolabs/auther-client
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
})
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"
.
//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
}
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
}
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 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
}
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
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"
*/
}
- 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
Released under MIT License.