Getting Access Token and Logged-In User Info

The following guide only works with Cotter's NPM package that can be installed using:

yarn add cotter

<!-- or -->

npm i cotter --save

<!-- or -->

<script
    src="https://unpkg.com/cotter@0.3.17/dist/cotter.min.js"
    type="text/javascript"
></script>

This feature is not supported on imports using

<script src="https://js.cotter.app/lib/cotter.js" type="text/javascript"></script>

Get Access Token

After the user Authenticates, the SDK automatically stores the Access Token and Refresh Token for your convenience. To get the access token:

var cotter = new Cotter(API_KEY_ID)
cotter.tokenHandler.getAccessToken().then(tok => {
  console.log(tok)
})

This function automatically refreshes the access token if it's expired. The returned access token would be in the form of a CotterAccessToken object which contains both the string and the decoded token.

Get Logged-in User

After the user Authenticates, the SDK automatically stores the User Information. To get that information:

var cotter = new Cotter(API_KEY_ID)
var user = cotter.getLoggedInUser()
console.log(user)

Log Out

To log out and remove these data, do the following:

var cotter = new Cotter(API_KEY_ID)
await cotter.logOut()

Update Refresh Token (for Custom Claims)

version >= 0.3.24

If you used this API to get custom claims, and you received a new refresh_token , you can update the refresh token stored in the user's browser by calling this method:

var cotter = new Cotter(API_KEY_ID)
const resp = await cotter.tokenHandler.updateTokensWithRefreshToken(newRefreshTok)

console.log(resp)
// resp:
{
   "access_token": "eyJhbGciOiJFUzI1NiIsImtpZCI6IlNQQUNFX0pXVF9QVUJMSUM6ODAyOEFBQTMtRUMyRC00QkFBLUJFN0EtN0M4MzU5Q0NCOUY5IiwidHlwIjoiSldUIn0.eyJhZGRyZXNzIjoiSEVsbG9vIiwiYWdlIjoxMjUsImF1ZCI6IjQ3Nzc1YTllLWY1OTktNDk5OS1iNmVhLWY5MDEzMTVhZDMxYiIsImF1dGhlbnRpY2F0aW9uX21ldGhvZCI6Ik9UUCIsImNsaWVudF91c2VyX2lkIjoiZWNhZGJkMmMtNTZmOC00MDc4LWI0NWQtZjE3Nzg2ZWQ0OTllIiwiZXhwIjoxNjA0NjMxMzAyLCJpYXQiOjE2MDQ2Mjc3MDIsImlkZW50aWZpZXIiOiJwdXRyaWthcnV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Fq3PIpIAtkg3z2Sv6PfZSodeQ",
   "refresh_token": "75063:kisZxVuaTy9LrkzmzXTrmtV1dH7ACYJ951GR99fV86n1CTIOMB",
   "expires_in": 3600,
   "token_type": "Bearer",
   "auth_method": "OTP"
}

After calling this method, it will automatically update all the access_token, id_token and refresh_token with the new value.

This means, when you call cotter.tokenHandler.getAccessToken() next, you'll get an access token that has all the custom attributes.

Access tokens and id tokens that are generated using this refresh token and subsequently returned refresh token will always have the custom claims.

New Logins will not have the custom claims ​because we don't attach the custom claims to the user itself.

OAuth Token Specification

Last updated