# Getting Access Token and Logged-In User Info

{% hint style="warning" %}
The following guide only works with [Cotter's NPM package](https://www.npmjs.com/package/cotter) that can be installed using:

```markup
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

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

{% endhint %}

## 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:

```javascript
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](https://github.com/cotterapp/cotter-token-js#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:

```javascript
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:

```javascript
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](/api-reference/oauth-tokens-api/requesting-custom-claims-on-your-access-token.md), and you received a new `refresh_token` , you can update the refresh token stored in the user's browser by calling this method:

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

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

After calling this metho&#x64;**,** it will automatically update all the `access_token`, `id_token` and `refresh_token` with the new value.&#x20;

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

{% hint style="info" %}
Access tokens and id tokens that are generated using this refresh token and [subsequently returned refresh token](/api-reference/oauth-tokens-api.md#refresh-token-rotation) 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.
{% endhint %}

## OAuth Token Specification

{% content-ref url="/pages/-M4HUyse-9aPxMqUoSRy" %}
[Cotter's OAuth 2.0 Tokens Specification](/getting-access-token/handling-authentication-with-cotter.md)
{% endcontent-ref %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.cotter.app/sdk-reference/web/getting-access-token-and-logged-in-user-info.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
