/auth
Search
K
Comment on page

getTokens

This function returns a user's access token.

Usage

import React, { useEffect, useState } from 'react';
import { useSlashAuth } from '@slashauth/slashauth-react';
export const GetTokens = () => {
const { getTokens } = useSlashAuth();
const [tokens, setTokens] = useState('');
useEffect(() => {
getTokens().then((tokens) => setTokens(tokens));
}, [getTokens]);
return <div>{`Tokens: ${tokens}`}</div>;
}}

Return Value

string

Configuration

ignoreCache (optional)

When True, ignores the cache and always sends a request to SlashAuth.
import React, { useEffect, useState } from 'react';
import { useSlashAuth } from '@slashauth/slashauth-react';
export const GetTokens = () => {
const { getTokens } = useSlashAuth();
const [tokens, setTokens] = useState('');
useEffect(() => {
getTokens({ ignoreCache: true }).then((tokens) => setTokens(tokens));
}, [getTokens]);
return <div>{`Tokens: ${tokens}`}</div>;
};

timeoutInSeconds (optional)

A maximum number of seconds to wait before declaring the background/authorize call as failed for timeout. Defaults to 60 seconds.
import React, { useEffect, useState } from 'react';
import { useSlashAuth } from '@slashauth/slashauth-react';
export const GetTokens = () => {
const { getTokens } = useSlashAuth();
const [tokens, setTokens] = useState('');
useEffect(() => {
getTokens({ timeoutInSeconds: 60 }).then((tokens) => setTokens(tokens));
}, [getTokens]);
return <div>{`Tokens: ${tokens}`}</div>;
};

detailedResponse (optional)

If true, the full response from the /oauth/token endpoint (or the cache, if the cache was used) is returned (minus refresh_token if one was issued). Otherwise, just the access token is returned.
import React, { useEffect, useState } from 'react';
import { useSlashAuth } from '@slashauth/slashauth-react';
export const GetTokens = () => {
const { getTokens } = useSlashAuth();
const [tokens, setTokens] = useState('');
useEffect(() => {
getTokens({ detailedResponse: true }).then((tokens) => setTokens(tokens));
}, [getTokens]);
return <div>{`Tokens: ${tokens}`}</div>;
};