npm package: @lonewolfdays/cookie-manager
Description: A comprehensive cookie management utility for Next.js applications
Author: Manas Saha Roy
Latest version: 2.0.0
Deprecated: Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

# Next.js Cookie Manager

A comprehensive cookie management utility for Next.js applications that works seamlessly across both client and server components.

## Installation

```bash
npm install @lonewolfdays/cookie-manager
# or
yarn add @lonewolfdays/cookie-manager
```

## Features

- Separate client and server-side cookie management
- TypeScript support
- Token-specific helpers
- Runtime checks to prevent misuse
- Modular architecture
- AES encryption for cookie values

## Usage

### Basic Cookie Operations

```typescript
import { ClientCookieManager, ServerCookieManager } from '@lonewolfdays/cookie-manager';

// Set encryption secret key (required)
ClientCookieManager.setSecretKey('your-secret-key');
ServerCookieManager.setSecretKey('your-secret-key');

// Client-side operations (in client components)
ClientCookieManager.setCookie('myCookie', 'value');
const value = ClientCookieManager.getCookie('myCookie');
ClientCookieManager.deleteCookie('myCookie');

// Server-side operations (in server components)
await ServerCookieManager.setCookie('myCookie', 'value');
const value = await ServerCookieManager.getCookie('myCookie');
await ServerCookieManager.deleteCookie('myCookie');
```

### Token Management

```typescript
// Client-side token operations
ClientCookieManager.setAccessToken('token');
const token = ClientCookieManager.getAccessToken();
ClientCookieManager.deleteAccessToken();

// Server-side token operations
await ServerCookieManager.setAccessToken('token');
const token = await ServerCookieManager.getAccessToken();
await ServerCookieManager.deleteAccessToken();
```

### Cookie Options

```typescript
import { CookieOptions } from '@lonewolfdays/cookie-manager';

const options: CookieOptions = {
  path: '/',
  maxAge: 60 * 60 * 24 * 7, // 1 week
  secure: true,
  httpOnly: true,
  sameSite: 'strict',
  domain: 'example.com'
};

ClientCookieManager.setCookie('myCookie', 'value', options);
```

### Using the Unified Interface (Optional)

```typescript
import { NextCookieManager } from '@lonewolfdays/cookie-manager';

// Set encryption secret key
NextCookieManager.client.setSecretKey('your-secret-key');
NextCookieManager.server.setSecretKey('your-secret-key');

// Client-side operations
NextCookieManager.client.setCookie('myCookie', 'value');

// Server-side operations
await NextCookieManager.server.setCookie('myCookie', 'value');
```

## Encryption

The package uses AES encryption to secure cookie values. You must set a secret key before using any cookie operations:

```typescript
// Set the secret key (do this once at app initialization)
ClientCookieManager.setSecretKey('your-secret-key');
ServerCookieManager.setSecretKey('your-secret-key');

// The same key must be used for both client and server
// to ensure proper encryption/decryption
```

### Security Considerations

1. Keep your secret key secure and never expose it in client-side code
2. Use environment variables to store the secret key
3. Use different keys for development and production
4. Rotate keys periodically for enhanced security

Example with environment variables:

```typescript
// In your Next.js app
ClientCookieManager.setSecretKey(process.env.COOKIE_SECRET_KEY!);
ServerCookieManager.setSecretKey(process.env.COOKIE_SECRET_KEY!);
```

## API Reference

### ClientCookieManager

- `setSecretKey(key: string)`
- `setCookie(name: string, value: string, options?: CookieOptions)`
- `getCookie(name: string): string | undefined`
- `deleteCookie(name: string, options?: CookieOptions)`
- `getAccessToken(): string | undefined`
- `getRefreshToken(): string | undefined`
- `setAccessToken(token: string, options?: CookieOptions)`
- `setRefreshToken(token: string, options?: CookieOptions)`
- `deleteAccessToken(options?: CookieOptions)`
- `deleteRefreshToken(options?: CookieOptions)`

### ServerCookieManager

- `setSecretKey(key: string)`
- `setCookie(name: string, value: string, options?: CookieOptions): Promise<void>`
- `getCookie(name: string): Promise<string | undefined>`
- `deleteCookie(name: string): Promise<void>`
- `getAccessToken(): Promise<string | undefined>`
- `getRefreshToken(): Promise<string | undefined>`
- `setAccessToken(token: string, options?: CookieOptions): Promise<void>`
- `setRefreshToken(token: string, options?: CookieOptions): Promise<void>`
- `deleteAccessToken(): Promise<void>`
- `deleteRefreshToken(): Promise<void>`

## License

MIT