/auth
Search
K
Comment on page

checkSession

This function checks to see if a user has an active session.

Usage

import React, { useEffect, useState } from 'react';
import { useSlashAuth } from '@slashauth/slashauth-react';
export const CheckSession = () => {
const { checkSession } = useSlashAuth();
const [hasSession, setHasSession] = useState(false);
useEffect(() => {
checkSession().then((session) => setHasSession(session));
}, [checkSession]);
return <div>{`Does user have a session? ${hasSession}`}</div>;
}

Return Value

boolean

Configuration

ignoreCache (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 CheckSession = () => {
const { checkSession } = useSlashAuth();
const [hasSession, setHasSession] = useState(false);
useEffect(() => {
checkSession({ ignoreCache: true }).then((session) =>
setHasSession(session)
);
}, [checkSession]);
return <div>{`Does user have a session? ${hasSession}`}</div>;
};

timeoutInSeconds (optional)

A maximum number of seconds to wait before declaring the background /authorize call as failed for timeout Defaults to 60s.
import React, { useEffect, useState } from 'react';
import { useSlashAuth } from '@slashauth/slashauth-react';
export const CheckSession = () => {
const { checkSession } = useSlashAuth();
const [hasSession, setHasSession] = useState(false);
useEffect(() => {
checkSession({ timeoutInSeconds: 30 }).then((session) =>
setHasSession(session)
);
}, [checkSession]);
return <div>{`Does user have a session? ${hasSession}`}</div>;
};

detailedResponse (optional)

If you need to send custom parameters to the Authorization Server, make sure to use the original parameter name.
import React, { useEffect, useState } from 'react';
import { useSlashAuth } from '@slashauth/slashauth-react';
export const CheckSession = () => {
const { checkSession } = useSlashAuth();
const [hasSession, setHasSession] = useState(false);
useEffect(() => {
checkSession({ detailedResponse: true }).then((session) =>
setHasSession(session)
);
}, [checkSession]);
return <div>{`Does user have a session? ${hasSession}`}</div>;
};
Last modified 10mo ago