# How to create Auth0 session cookie programatically within SSR Next JS using nextjs-auth0?

**URL:** <https://community.auth0.com/t/how-to-create-auth0-session-cookie-programatically-within-ssr-next-js-using-nextjs-auth0/89899>\
**Category:** Get Help\
**Created:** [September 1, 2022, 9:39pm UTC](https://community.auth0.com/t/how-to-create-auth0-session-cookie-programatically-within-ssr-next-js-using-nextjs-auth0/89899 "2022-09-01T21:39:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnlondon](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/johnlondon/32/33352_2.png) [@johnlondon](https://community.auth0.com/u/johnlondon)\
**Post date:** [September 1, 2022, 9:39pm UTC](https://community.auth0.com/t/how-to-create-auth0-session-cookie-programatically-within-ssr-next-js-using-nextjs-auth0/89899/1 "2022-09-01T21:39:09Z")

</div>

I want to create a no-friction signup experience for users. I want the flow to look like (all actions are carried out using SSR):

1. User supplies email, clicks signup button.
2. Auth0 API is used to create a user with random password. Password is forgotten after this step.
3. Resource owner password flow is used to retrieve id and access tokens.
4. Tokens are used to create Auth0 cookie(s), this is returned to user and user is now logged in. (I believe if the cookie is \> 4kB then it is split).
5. Subsequent requests from client (whether SSR or client-side) are from a logged in user (albeit the user has not yet validated their email address).

The user still needs to verify their email, but otherwise is able to interact with the system without having to first verify their email.

I have got 1-3 working but I am not sure how to do (4). Is there an example of using `nextjs-auth0` (perhaps within here? [nextjs-auth0/src/auth0-session at main · auth0/nextjs-auth0 · GitHub](https://github.com/auth0/nextjs-auth0/tree/main/src/auth0-session)) to create a cookie that will be recognised by auth0?

---

<div class="post-metadata">

**Author:** ![johnlondon](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/johnlondon/32/33352_2.png) [@johnlondon](https://community.auth0.com/u/johnlondon)\
**Post date:** [September 1, 2022, 9:41pm UTC](https://community.auth0.com/t/how-to-create-auth0-session-cookie-programatically-within-ssr-next-js-using-nextjs-auth0/89899/2 "2022-09-01T21:41:14Z")

</div>

\*amend to flow above to fix typo: the random user password is forgotten after being used to retrieve tokens for the user within step (3), not step(2) as written above.

---

<div class="post-metadata">

**Author:** ![johnlondon](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/johnlondon/32/33352_2.png) [@johnlondon](https://community.auth0.com/u/johnlondon)\
**Post date:** [September 1, 2022, 11:00pm UTC](https://community.auth0.com/t/how-to-create-auth0-session-cookie-programatically-within-ssr-next-js-using-nextjs-auth0/89899/3 "2022-09-01T23:00:24Z")

</div>

For anyone coming to this later, I actually got a very basic version to work just like this inside an API route:

```auto
    import {getConfig} from "@auth0/nextjs-auth0/dist/config";  
    import {CookieStore} from "@auth0/nextjs-auth0/dist/auth0-session"; 
    import {SessionCache} from "@auth0/nextjs-auth0";
    import {TokenSet} from "openid-client";

export default async function handler(req, res) {
    console.log('inside api method');
    const { baseConfig, nextConfig } = getConfig();
    const cookieStore = new CookieStore(baseConfig);
    const sessionCache = new SessionCache(baseConfig, cookieStore);

        await fetch('https://MYAPI.us.auth0.com/oauth/token', {
            method: 'POST',
            mode: 'cors',
            cache: 'no-cache',
            credentials: 'same-origin',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            redirect: 'follow',
            referrerPolicy: 'no-referrer',
            body: new URLSearchParams({
                grant_type: 'password',
                username: 'myusername',
                password: 'mypassword',
                audience: 'https://<MY API>.us.auth0.com/api/v2/',
                scope: 'openid profile email',
                client_id: 'xxx',
                client_secret: 'xxx'
            })
        }).then(function (response) {
            return response.json();
        }).then((json) => {
                const session = sessionCache.fromTokenSet(new TokenSet(json));
                sessionCache.create(req, res, session);

                res.writeHead(302 , {
                    'Location' : '/some/redirect/url'
                });
                res.end();

            }
        )
            .catch(function (error) {
                res.writeHead(404 , {
                    'Location' : '/404' 
                });
                res.end();
            });
}

```

---

<div class="post-metadata">

**Author:** ![infinitz](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@infinitz](https://community.auth0.com/u/infinitz)\
**Post date:** [August 11, 2023, 5:46pm UTC](https://community.auth0.com/t/how-to-create-auth0-session-cookie-programatically-within-ssr-next-js-using-nextjs-auth0/89899/4 "2023-08-11T17:46:23Z")

</div>

Were you able to solve that `@auth0/nextjs-auth0` does not export these out from the package?

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [August 30, 2024, 6:01pm UTC](https://community.auth0.com/t/how-to-create-auth0-session-cookie-programatically-within-ssr-next-js-using-nextjs-auth0/89899/5 "2024-08-30T18:01:23Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
