Unverified Commit e8bbf148 authored by Andrei Zanouski's avatar Andrei Zanouski Committed by GitHub

Add example with cookies-next package (#31243)

* Add example with cookies-next package

* Update examples/with-cookies-next/package.json
Co-authored-by: 's avatarBalázs Orbán <info@balazsorban.com>

* add typescript and update cookies-next version

* Update examples/with-cookies-next/.gitignore
Co-authored-by: 's avatarBalázs Orbán <info@balazsorban.com>

* add @types/node in devDependencies

* update cookies-next

* Apply suggestions from code review

* change the function naming to be consistent with the idiom of working with a container of cookies and prettier-fix

* lint-fix
Co-authored-by: 's avatarBalázs Orbán <info@balazsorban.com>
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 98f4bdd1
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies
/node_modules
/.pnp
.pnp.js
# testing
/coverage
# next.js
/.next/
/out/
# production
/build
# misc
.DS_Store
*.pem
# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# local env files
.env.local
.env.development.local
.env.test.local
.env.production.local
# vercel
.vercel
# typescript
*.tsbuildinfo
# With cookies-next
In this example, we are getting, setting and removing cookies
This package works on client side and server side
- can be used on the client side, anywhere
- can be used for server side rendering in getServerSideProps
- can be used in API handlers
## Deploy your own
Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example) or preview live with [StackBlitz](https://stackblitz.com/github/vercel/next.js/tree/canary/examples/with-cookies-next)
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https://github.com/vercel/next.js/tree/canary/examples/with-cookies-next&project-name=with-cookies-next&repository-name=with-cookies-next)
## How to use
Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example:
```bash
npx create-next-app --example with-cookies-next with-cookies-next-app
# or
yarn create next-app --example with-cookies-next with-cookies-next-app
```
Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
/// <reference types="next" />
/// <reference types="next/image-types/global" />
// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
{
"private": true,
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
},
"dependencies": {
"cookies-next": "^2.1.1",
"next": "latest",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/node": "^18.0.0",
"@types/react": "^18.0.14",
"typescript": "^4.7.4"
}
}
import { getCookie, getCookies } from 'cookies-next'
export default async function getApiCookie(req, res) {
try {
const currentCookie = getCookie('api-cookie', { req, res })
const allCookies = getCookies({ req, res })
console.log('currentCookie: ', currentCookie)
console.log('allCookies: ', allCookies)
res.status(200).send('get api cookies')
} catch (error) {
res.status(400).send(error.message)
}
}
import { deleteCookie } from 'cookies-next'
export default async function setApiCookie(req, res) {
try {
deleteCookie('api-cookie', { req, res })
res.status(200).send('remove api cookies')
} catch (error) {
res.status(400).send(error.message)
}
}
import { setCookie } from 'cookies-next'
export default async function setApiCookie(req, res) {
try {
setCookie('api-cookie', 'mock-value', { req, res, maxAge: 60 * 60 * 24 })
res.status(200).send('set api cookies')
} catch (error) {
res.status(400).send(error.message)
}
}
import React from 'react'
import {
setCookie,
getCookies,
getCookie,
deleteCookie,
hasCookie,
} from 'cookies-next'
const Home = () => {
const handleSetCookie = () => setCookie('client-cookie', 'mock client value')
const handleCheckCookie = () => console.log(hasCookie('client-cookie'))
const handleGetCookie = () => console.log(getCookie('client-cookie'))
const handleGetCookies = () => console.log(getCookies())
const handleDeleteCookies = () => deleteCookie('client-cookie')
return (
<div>
<h1>Next Cookies</h1>
<button onClick={handleSetCookie}>Set Cookie</button>
<br />
<button onClick={handleCheckCookie}>Check Cookie</button>
<br />
<button onClick={handleGetCookie}>Get Cookie</button>
<br />
<button onClick={handleGetCookies}>Get All Cookies</button>
<br />
<button onClick={handleDeleteCookies}>Remove Cookies</button>
</div>
)
}
export default Home
import React from 'react'
import { getCookies, getCookie, setCookies, removeCookies } from 'cookies-next'
const SsrCookies = () => {
return <div>SSR Cookies</div>
}
export const getServerSideProps = ({ req, res }) => {
setCookies('ssr-cookie', 'mock-ssr-value', { req, res, maxAge: 60 * 6 * 24 })
getCookie('client-cookie', { req, res })
getCookies({ req, res })
removeCookies('client-cookie', { req, res })
return { props: {} }
}
export default SsrCookies
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"esModuleInterop": true,
"module": "esnext",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment