Skip to main content
Version: 1.x

Cookie Authentication

The SecuritySchemeCookie type is used for cookie-based authentication, where the secret is placed in a cookie. You can't specify the cookie name. The server must set the cookie.

See the OpenAPI Cookie Authentication ๐Ÿ”— specification for more information.

type SecuritySchemeCookie = {
/** Where the secret should be placed. */
in: 'cookie';
/** Refresh interval in milliseconds. */
refreshInterval?: number;
};
  • Location: Specifies that the secret is placed in a cookie.
  • Refresh Interval: (Optional) The interval (in milliseconds) at which the key should be refreshed.

Exampleโ€‹

import { ReactNode } from 'react';
import { requestFn, QraftContext } from '@openapi-qraft/react';
import { QraftSecureRequestFn } from '@openapi-qraft/react/Unstable_QraftSecureRequestFn';
import { createAPIClient } from './api'; // generated by OpenAPI Qraft CLI

const qraft = createAPIClient();

const App = ({ children }: { children: ReactNode }) => {
return (
<QraftSecureRequestFn
requestFn={requestFn}
securitySchemes={{
cookieAuth: async ({isRefreshing}) => {
await qraft.auth.cookieAuth(
isRefreshing
? { refresh: true }
: {
username: 'UNSECURE_TEST_USERNAME',
password: 'UNSECURE_TEST_PASSWORD',
},
);

return {
in: 'cookie',
refreshInterval: 3600_000, // 1 hour in milliseconds (optional)
};
},
}}
>
{(secureRequestFn) => (
// When using `secureRequestFn`, all requests that require the `cookieAuth` security scheme
// will ensure the necessary cookies for authentication are set by the server.
// The server is responsible for setting and updating the authentication cookie.
//
// The initial request will send the provided username and password to the server,
// which will respond by setting the authentication cookie. For subsequent requests,
// if the `isRefreshing` flag is true, a refresh request will be sent to update the cookie.
<QraftContext.Provider value={{ requestFn: secureRequestFn }}>
{children}
</QraftContext.Provider>
)}
</QraftSecureRequestFn>
);
};

export default App;