Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 01d71e6

Browse filesBrowse files
committed
Add docs for replaceSessionData()
1 parent 7c0df48 commit 01d71e6
Copy full SHA for 01d71e6

File tree

Expand file treeCollapse file tree

1 file changed

+36
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+36
-0
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
+36Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ This method returns the current session object. If no session has been establish
4848
Calling this method will _not_ establish a session and will _not_ set a cookie for your visitors.
4949

5050
### Example usage in `getServerSideProps()`
51+
Fetching the currently logged in user - if any.
5152
```typescript
5253
export async function getServerSideProps(context: GetServerSidePropsContext){
5354
const {user = null} = await getSessionData(context);
@@ -60,6 +61,7 @@ export async function getServerSideProps(context: GetServerSidePropsContext){
6061
```
6162

6263
### Example usage in API routes
64+
Return user data from an API endpoint, when logged in.
6365
```typescript
6466
export default async function handler(req: NextApiRequest, res: NextApiResponse){
6567
const {user = null} = await getSessionData(req, res);
@@ -79,6 +81,7 @@ of the session object will be preserved. Calling the method will establish a new
7981
cookie.
8082

8183
### Example usage in `getServerSideProps()`
84+
Log some actions of the user to modify the experience in other places.
8285
```typescript
8386
export async function getServerSideProps(context: GetServerSidePropsContext){
8487
await setSessionData({viewedPricingPage: true});
@@ -92,6 +95,7 @@ export async function getServerSideProps(context: GetServerSidePropsContext){
9295
```
9396

9497
### Example usage in API routes
98+
Place products in a cart and persist it in the session.
9599
```typescript
96100
export default async function handler(req: NextApiRequest, res: NextApiResponse){
97101
const {cart = {}} = await getSessionData(req, res);
@@ -104,7 +108,39 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
104108
}
105109
```
106110

111+
<h2 id="replaceSessionData"><code>replaceSessionData([polymorph]): Promise&lt;void&gt;</code></h2>
107112
## `replaceSessionData()`
113+
This method will replace the whole session object with a new one. This will overwrite/remove all existing session data, so
114+
be careful when using it.
115+
116+
### Example usage in `getServerSideProps()`
117+
Resets a multi-step form and all helper data. Still be careful with this!
118+
```typescript
119+
export async function getServerSideProps(context: GetServerSidePropsContext){
120+
await replaceSessionData({step: 1});
121+
122+
return {
123+
props: {
124+
}
125+
}
126+
}
127+
```
128+
129+
### Example usage in API routes
130+
A login example where any stale data from previous user sessions is reset.
131+
```typescript
132+
export default async function handler(req: NextApiRequest, res: NextApiResponse){
133+
const {username, password} = req.body;
134+
let result = login(username, password);
135+
if(result.user){
136+
await replaceSessionData({user: result.user});
137+
res.end("ok");
138+
return;
139+
}
140+
res.end(result.error);
141+
}
142+
```
143+
108144
## `pluckSessionData()`
109145
## `destroySession()`
110146
## `getCSRFToken()`

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.