Loading...
; + } + + const handleLogout = useCallback(() => { + sdk.logout(); + }, [sdk]); + + if (isAuthenticated) { + return ( + <> +Hello {user.name}
+ + > + ); + } + + returnYou are not logged in
; +}; +``` + +Note: `useSession` triggers a single request to the Descope backend to attempt to refresh the session. If you **don't** `useSession` on your app, the session will not be refreshed automatically. If your app does not require `useSession`, you can trigger the refresh manually by calling `refresh` from `useDescope` hook. Example: + +```js +const { refresh } = useDescope(); +useEffect(() => { + refresh(); +}, [refresh]); +``` + + +### Auto refresh session token +Descope SDK automatically refreshes the session token when it is about to expire. This is done in the background using the refresh token, without any additional configuration. +If you want to disable this behavior, you can pass `autoRefresh={false}` to the `AuthProvider` component. This will prevent the SDK from automatically refreshing the session token. + +**For more SDK usage examples refer to [docs](https://docs.descope.com/build/guides/client_sdks/)** + +### Session token server validation (pass session token to server API) + +When developing a full-stack application, it is common to have private server API which requires a valid session token: + + + +Note: Descope also provides server-side SDKs in various languages (NodeJS, Go, Python, etc). Descope's server SDKs have out-of-the-box session validation API that supports the options described bellow. To read more about session validation, Read [this section](https://docs.descope.com/build/guides/gettingstarted/#session-validation) in Descope documentation. + +There are 2 ways to achieve that: + +1. Using `getSessionToken` to get the token, and pass it on the `Authorization` Header (Recommended) +2. Passing `sessionTokenViaCookie` boolean prop to the `AuthProvider` component (Use cautiously, session token may grow, especially in cases of using authorization, or adding custom claim) + +#### 1. Using `getSessionToken` to get the token + +An example for api function, and passing the token on the `Authorization` header: + +```js +import { getSessionToken } from '@descope/react-sdk'; + +// fetch data using back +// Note: Descope backend SDKs support extracting session token from the Authorization header +export const fetchData = async () => { + const sessionToken = getSessionToken(); + const res = await fetch('/path/to/server/api', { + headers: { + Authorization: `Bearer ${sessionToken}`, + }, + }); + // ... use res +}; +``` + +An example for component that uses `fetchData` function from above + +```js +// Component code +import { fetchData } from 'path/to/api/file' +import { useCallback } from 'react' + +const Component = () => { + const onClick = useCallback(() => { + fetchData() + },[]) + return ( + {...} + { + // button that triggers an API that may use session token + + } + ) +} +``` + +Note that ff Descope project settings are configured to manage session token in cookies, the `getSessionToken` function will return an empty string. + +#### 2. Passing `sessionTokenViaCookie` boolean prop to the `AuthProvider` + +Passing `sessionTokenViaCookie` prop to `AuthProvider` component. Descope SDK will automatically store session token on the `DS` cookie. + +Note: Use this option if session token will stay small (less than 1k). Session token can grow, especially in cases of using authorization, or adding custom claims + +Example: + +```js +import { AuthProvider } from '@descope/react-sdk'; + +const AppRoot = () => { + return ( ++ {user?.name} +
++ {user?.email} +
+프로필 위젯 ID가 설정되지 않았습니다.
+