@@ -48,6 +48,7 @@ This method returns the current session object. If no session has been establish
48
48
Calling this method will _ not_ establish a session and will _ not_ set a cookie for your visitors.
49
49
50
50
### Example usage in ` getServerSideProps() `
51
+ Fetching the currently logged in user - if any.
51
52
``` typescript
52
53
export async function getServerSideProps(context : GetServerSidePropsContext ){
53
54
const {user = null } = await getSessionData (context );
@@ -60,6 +61,7 @@ export async function getServerSideProps(context: GetServerSidePropsContext){
60
61
```
61
62
62
63
### Example usage in API routes
64
+ Return user data from an API endpoint, when logged in.
63
65
``` typescript
64
66
export default async function handler(req : NextApiRequest , res : NextApiResponse ){
65
67
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
79
81
cookie.
80
82
81
83
### Example usage in ` getServerSideProps() `
84
+ Log some actions of the user to modify the experience in other places.
82
85
``` typescript
83
86
export async function getServerSideProps(context : GetServerSidePropsContext ){
84
87
await setSessionData ({viewedPricingPage: true });
@@ -92,6 +95,7 @@ export async function getServerSideProps(context: GetServerSidePropsContext){
92
95
```
93
96
94
97
### Example usage in API routes
98
+ Place products in a cart and persist it in the session.
95
99
``` typescript
96
100
export default async function handler(req : NextApiRequest , res : NextApiResponse ){
97
101
const {cart = {}} = await getSessionData (req , res );
@@ -104,7 +108,39 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
104
108
}
105
109
```
106
110
111
+ <h2 id =" replaceSessionData " ><code >replaceSessionData([polymorph]): Promise< ; void> ; </code ></h2 >
107
112
## ` 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
+
108
144
## ` pluckSessionData() `
109
145
## ` destroySession() `
110
146
## ` getCSRFToken() `
0 commit comments