
Research
Two Malicious Rust Crates Impersonate Popular Logger to Steal Wallet Keys
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.
next-cookies-universal
Advanced tools
An utility that can help you to handle the Cookies in NextJS App Route with every context (both Server or Client) πͺπ₯
An utility that can help you to handle the Cookies in NextJS App Route with every context (both Server or Client) πͺπ₯
All supported to NextJS App Route
You can see Live Demo here
npm i next-cookies-universal
yarn add next-cookies-universal
import Cookies from 'next-cookies-universal';
const ServerCookies = Cookies('server');
// or
const ClientCookies = Cookies('client');
'use client';
import Cookies from 'next-cookies-universal';
const MyClientComponent = () => {
const cookies = Cookies('client');
const handleClick = () => {
cookies.set('my_token', 'my_token_value');
};
const handleClickWithExpiry = () => {
// Set cookie with maxAge (expires in 1 hour)
cookies.set('my_token', 'my_token_value', {
maxAge: 60 * 60, // 1 hour in seconds
path: '/'
});
};
const handleClickWithExpiresDate = () => {
// Set cookie with specific expiration date
const expiryDate = new Date();
expiryDate.setDate(expiryDate.getDate() + 7); // 7 days from now
cookies.set('my_token', 'my_token_value', {
expires: expiryDate,
path: '/'
});
};
return (
<div>
<button onClick={handleClick}>
Click to set cookies
</button>
<button onClick={handleClickWithExpiry}>
Click to set cookies with maxAge
</button>
<button onClick={handleClickWithExpiresDate}>
Click to set cookies with expires date
</button>
</div>
);
};
import Cookies from 'next-cookies-universal';
const MyServerComponent = async() => {
const cookies = Cookies('server');
const myToken = cookies.get('my_token');
const data = await fetch('http://your.endpoint', {
headers: {
Authentication: `Bearer ${myToken}`
}
}).then(response => response.json());
return (
<div>
<p>Cookies Value: <strong>{myToken}</strong></p>
<code>
{JSON.stringify(data)}
</code>
</div>
);
};
Note: if you want to set cookies in Server, you not to allowed to set it on Server Component, you should do that in Server Actions.
import Cookies from 'next-cookies-universal';
const MyServerComponent = async() => {
const cookies = Cookies('server');
/** you should not to do like this!
* please read Server Actions reference if you want to set the cookies through Server.
*/
cookies.set('my_token', 'my_token_value');
const myToken = cookies.get('my_token');
return (
<div>
<p>Cookies Value: <strong>{myToken}</strong></p>
<code>
{JSON.stringify(data)}
</code>
</div>
);
};
import Cookies from 'next-cookies-universal';
async function setFromAction(formData: FormData) {
'use server';
const cookies = Cookies('server');
cookies.set('my_token', formData.get('cookie-value'));
}
function Form() {
return (
<div>
<form action={setFromAction}>
<input type="text" name="cookie-value" />
<div>
<button type="submit">
Set Your cookies
</button>
</div>
</form>
</div>
);
}
/** action.ts */
'use server';
export async function setFromAction(formData: FormData) {
const cookies = Cookies('server');
cookies.set('my_token', formData.get('cookie-value'));
}
/** Form.tsx */
'use client';
import { setFromAction } from './action.ts';
function Form() {
/** client logic */
return (
<div>
<form action={setFromAction}>
<input type="text" name="cookie-value" />
<div>
<button type="submit">
Set Your cookies
</button>
</div>
</form>
</div>
);
}
You can set cookies with various expiration options using the options
parameter:
'use client';
import Cookies from 'next-cookies-universal';
const cookies = Cookies('client');
// Set cookie that expires in 1 hour using maxAge
cookies.set('session_token', 'abc123', {
maxAge: 60 * 60, // 1 hour in seconds
path: '/'
});
// Set cookie that expires in 1 day using expires Date
const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
cookies.set('user_preference', 'dark_mode', {
expires: tomorrow, // expires tomorrow
path: '/',
secure: true,
sameSite: 'strict'
});
// Set cookie that expires in 1 year using maxAge
cookies.set('remember_me', 'true', {
maxAge: 365 * 24 * 60 * 60, // 1 year in seconds
path: '/'
});
// Set cookie with specific expiration date
const specificDate = new Date('2024-12-31T23:59:59Z');
cookies.set('campaign_banner', 'hidden', {
expires: specificDate, // expires on specific date
path: '/'
});
import Cookies from 'next-cookies-universal';
async function setTokenWithExpiry(formData: FormData) {
'use server';
const cookies = Cookies('server');
const token = formData.get('token');
// Set cookie with 7 days expiration using maxAge
cookies.set('auth_token', token, {
maxAge: 7 * 24 * 60 * 60, // 7 days in seconds
path: '/',
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
sameSite: 'strict'
});
// Set cookie with specific expiration date using expires
const sessionExpiry = new Date();
sessionExpiry.setHours(sessionExpiry.getHours() + 2); // 2 hours from now
cookies.set('session_id', 'session_123', {
expires: sessionExpiry,
path: '/',
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
sameSite: 'strict'
});
}
maxAge
option is automatically converted to an expires
Date object for compatibility with js-cookie
expires
option accepts a Date object directlymaxAge
and expires
directlymaxAge
for relative expiration (e.g., "expire in 1 hour")expires
for absolute expiration (e.g., "expire on December 31st")secure: true
and appropriate sameSite
settings in production/** parameter to initialize the Cookies() */
export type ICookiesContext = 'server'|'client';
/** both Cookies('client') and Cookies('server') implements this interface */
export interface IBaseCookies {
set<T = string>(
key: string,
value: T,
options?: ICookiesOptions
): void;
get<T = string>(key: string): T;
remove(key: string, options?: ICookiesOptions): void;
has(key: string): boolean;
clear(): void;
}
for ICookiesOptions
API, we use CookieSerializeOptions
from DefinetlyTyped
version
in package.json
is changed to newest version. Then run npm install
for synchronize it to package-lock.json
main
, you can publish the packages by creating new Relase here: https://github.com/gadingnst/next-cookies-universal/releases/newtag
, make sure the tag
name is same as the version
in package.json
.Publish Release
button, then wait the package to be published.next-cookies-universal
is freely distributable under the terms of the MIT license.
Feel free to open issues if you found any feedback or issues on next-cookies-universal
. And feel free if you want to contribute too! π
Built with β€οΈ by Sutan Gading Fadhillah Nasution on 2023
FAQs
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.
Research
A malicious package uses a QR code as steganography in an innovative technique.
Research
/Security News
Socket identified 80 fake candidates targeting engineering roles, including suspected North Korean operators, exposing the new reality of hiring as a security function.