<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Duncan Walker on Medium]]></title>
        <description><![CDATA[Stories by Duncan Walker on Medium]]></description>
        <link>https://medium.com/@sir.dunxalot?source=rss-7a3a5ac71740------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*YejWp6GRCyJld8DQ9YpM7A.jpeg</url>
            <title>Stories by Duncan Walker on Medium</title>
            <link>https://medium.com/@sir.dunxalot?source=rss-7a3a5ac71740------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 13:01:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sir.dunxalot/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Using Cypress with Next.js and Auth0]]></title>
            <link>https://levelup.gitconnected.com/using-cypress-with-next-js-and-auth0-2dbe7282dcf?source=rss-7a3a5ac71740------2</link>
            <guid isPermaLink="false">https://medium.com/p/2dbe7282dcf</guid>
            <category><![CDATA[auth0]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[cypress]]></category>
            <category><![CDATA[nextjs]]></category>
            <dc:creator><![CDATA[Duncan Walker]]></dc:creator>
            <pubDate>Tue, 26 May 2020 03:39:51 GMT</pubDate>
            <atom:updated>2020-06-24T21:27:23.111Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qTqk6Aqxu9QPnWhZxh1_BA.jpeg" /></figure><p>I spent three days trying to configure <a href="https://medium.com/u/47c842e55929">Cypress.io</a> to work with NextJS and <a href="https://medium.com/u/9ef9638b3092">Auth0</a>. The implementation was hard to figure out but ultimately simple in nature.</p><p>This guide will walk you through everything you need to know.</p><p><a href="https://gist.github.com/sir-dunxalot/8b8539a0baf4e76490991c3cd4c4401d">Simplified source code is available in this gist</a>.</p><h4>Edit:</h4><p>Since writing this article <a href="https://github.com/sir-dunxalot/cypress-nextjs-auth0">sir-dunxalot/cypress-nextjs-auth0</a> was released to streamline this integration process:</p><pre>yarn add cypress-nextjs-auth0 --dev</pre><p><a href="https://github.com/sir-dunxalot/cypress-nextjs-auth0">More info here</a>!</p><h3>Our goal</h3><p>Our goal is to create a one-line test helper that can be used in any Cypress test:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/47cc750a3736515747d9878cd59d9800/href">https://medium.com/media/47cc750a3736515747d9878cd59d9800/href</a></iframe><p>The Auth0 team maintains a great library for NextJS, <a href="https://github.com/auth0/nextjs-auth0">nextjs-auth0</a>. However, the <a href="https://github.com/auth0/nextjs-auth0">nextjs-auth0</a> library does not work with Cypress because <a href="https://docs.cypress.io/guides/references/best-practices.html#Visiting-external-sites">Cypress does not let you visit external sites during a test</a>. Think about it this way:</p><ol><li>The test user loads your app</li><li>The test user clicks ‘Log in’</li><li>The test user is taken to the Auth0 lock screen — but oh wait! We’re now on a different domain (or subdomain) so Cypress has blocked our request.</li></ol><p><strong>But fear not!</strong> By reading <a href="https://github.com/auth0/nextjs-auth0/blob/master/src/handlers/login.ts#L70">the source code of the nextjs-auth0 library</a>, we can learn how to authenticate in Cypress test suite.</p><p>Instead, we must login the test user using the Password grant.</p><h3>Setting up Auth0</h3><p>First, add the Auth0 Javascript library to our project:</p><pre>yarn add auth0-js --dev</pre><p>Second create a <strong>real user</strong> in your Auth0 dashboard. Write down their email and password, since you’ll need that info in a minute.</p><p>Third, <a href="https://auth0.com/docs/api-auth/tutorials/password-grant">enable the Resource Owner Password Grant in your Auth0 tenant by following these instructions</a>.</p><p>These are the only changes you need to make in Auth0 to support authentication from with a Cypress test.</p><h4>A note on security</h4><p>This walkthrough skips access token verification and reveals secrets, but I am assuming you are able to reduce security in your test environment. Auth0 recommends you use separate tenants for development, testing, and production, which further reduces the risk of putting test user credentials in cypress.env.json .</p><h3>Authenticating the test user</h3><p>Next, let’s create our login() helper for Cypress (helpers are often written in cypress/support/index.js ):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/95cc5b6cc899a8528d6d697dba585c40/href">https://medium.com/media/95cc5b6cc899a8528d6d697dba585c40/href</a></iframe><p>Note, we’re using <a href="https://docs.cypress.io/api/cypress-api/env.html#Syntax">Cypress environment variables</a>, which we set in cypress.env.json :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8e3421cf2b770930961a8101f4608b3a/href">https://medium.com/media/8e3421cf2b770930961a8101f4608b3a/href</a></iframe><p>Assuming you replace the information in cypress.env.json with your own values (noting that auth0CookieSecret is a random string you choose), your user test will be able to login inside a Cypress test.</p><p>Alright, we’re making some progress here…</p><p>But run a test using this helper and you’ll see that your Next.js app code won’t authenticate the test user. For example, if you have an API route being protected by the auth0/nextjs-auth0 package, the test user won’t be able to access that route:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/be2b21abaa8bf2d6fbbd2c5812d8a092/href">https://medium.com/media/be2b21abaa8bf2d6fbbd2c5812d8a092/href</a></iframe><p>So we need to not only authenticate the test user but also create a user session…</p><h3>Creating a user session</h3><p>We need to mimic the behaviour of the nextjs-auth0 library.</p><p>By reading <a href="https://github.com/auth0/nextjs-auth0/blob/master/src/handlers/login.ts#L70">the source code of the nextjs-auth0 library</a>, we understand that there are at leastfive steps to creating a user session in a Next.js app:</p><ol><li>Create a cookie, nameda0:state, to track the login-in-progress</li><li>Authenticate the user, retrieving an access token</li><li>Validate the access token (I am skipping this step since it’s out of scope for the walkthrough)</li><li>Use the access token to get the user’s info</li><li>Encrypt the user’s info to create the session object</li><li>Store the encrypted user session in a session cookie, named a0:session</li></ol><p>nextjs-auth0 uses Iron to encrypt the session, so let’s add that to our dependencies:</p><pre>yarn add @hapi/iron --dev</pre><p>Now, rather than calling our authentication helper directly, we need to call a helper that does the steps we just noted (e.g. storing an encrypted user session). My personal preference is to make our existing login() helper a private helper, _login(), and write a new login() helper, which will call _login() at some point.</p><p>Here are our updated helpers:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/11aa1b4782e8087d79f5ad0c2109e038/href">https://medium.com/media/11aa1b4782e8087d79f5ad0c2109e038/href</a></iframe><p>Note, I have skipped token verification here. If you want to verify the access token then I recommend looking at how <a href="https://github.com/panva/node-openid-client/">openid-client-node</a> and similar libraries validate tokens. For my use case, I did not deem validating access tokens necessary in my test environment.</p><h3>Trying it out</h3><p>Alright… This should work!</p><p>Let’s use our new login() helper in a test:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/47cc750a3736515747d9878cd59d9800/href">https://medium.com/media/47cc750a3736515747d9878cd59d9800/href</a></iframe><p>Alternatively, you could use the login() helper in Cypress’ before() hook:</p><p><a href="https://gist.github.com/sir-dunxalot/05133f7143d98bd63bcb57c8e2bcba59">https://gist.github.com/sir-dunxalot/05133f7143d98bd63bcb57c8e2bcba59</a></p><h3>Caching the test user</h3><p>Finally, we want to avoid unnecessary calls to Auth0 by caching the test user. You will quickly find your test suite failing due to Auth0 rate limits.</p><p>This might sound odd because we are already using cookies for persistance. However, you may notice that our helpers create the session cookie every time we call login(). Let’s create the session cookie only if it doesn’t already exist…</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/15494b7306ec04df057c2404fa65c63d/href">https://medium.com/media/15494b7306ec04df057c2404fa65c63d/href</a></iframe><h3>Et voilà!</h3><p>Our work is done.</p><p><a href="https://gist.github.com/sir-dunxalot/8b8539a0baf4e76490991c3cd4c4401d">Simplified source code is available in this gist</a>.</p><p>You can support multiple test users (assuming you have created the users in your Auth0 dashboard) by passing in credentials to the login() helper. Something like the following will do the trick:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/118fe23eabbc24470b668948155d53d7/href">https://medium.com/media/118fe23eabbc24470b668948155d53d7/href</a></iframe><h3>Troubleshooting</h3><p>If you’re still having trouble making this work, keep the following things in mind:</p><ul><li>You may need to add &quot;chromeWebSecurity&quot;: false to your cypress.json config file. This seems necessary in some scenarios but I’m not sure why.</li><li>You need to enable the password grant type in your Auth0 application&#39;s settings.</li><li>You need to set the default directory (in your Auth0 tenant settings) to Username-Password-Authentication, which is usually the name of the database connection you want to use for password grant authentication (the name will be shown in Auth0 Dashboard --&gt; Connections --&gt; Database).</li><li>The username and password combination should be a real account you created in your Auth0 dashboard (albeit one you create for testing only).</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2dbe7282dcf" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/using-cypress-with-next-js-and-auth0-2dbe7282dcf">Using Cypress with Next.js and Auth0</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>