Test authentication flows
Learn how to test authentication flows (sign-up, sign-in, sign-out) with Cypress for applications using Kinde authentication. Learn more about testing authentication flows with Kinde.
What you need
Link to this section- Completed the Cypress setup
- A test user and a running Kinde application.
Test sign-in flow
Link to this section-
Create a new test file:
Terminal window touch cypress/e2e/sign-in.cy.js -
Add the following to the
sign-in.cy.jsfile:cypress/e2e/sign-in.cy.js describe('Sign in Flows', () => {it('user can sign in with email and password', () => {cy.visit(Cypress.env('TEST_APP_URL'));cy.get('[data-testid="sign-in-button"]').click();// Wait for Kinde login pagecy.url().should('include', 'kinde.com');cy.origin(Cypress.env('KINDE_ISSUER_URL'), () => {// Enter credentialscy.get('input[name="p_email"]').type(Cypress.env('TEST_USER_EMAIL'));cy.get('button[type="submit"]').click();cy.get('input[name="p_password"]').type(Cypress.env('TEST_USER_PASSWORD'));cy.get('button[type="submit"]').click({ multiple: true });});// Wait for redirect back to appconst appUrl = new URL(Cypress.env('TEST_APP_URL'));cy.url().should('include', appUrl.hostname);cy.get('[data-testid="user-profile"]').should('be.visible');});});
Test sign-out flow
Link to this section-
Create a new test file:
Terminal window touch cypress/e2e/sign-out.cy.js -
Add the following to the
sign-out.cy.jsfile:cypress/e2e/sign-out.cy.js describe('Sign-out flows', () => {it('user can sign in and then sign out', () => {// First authenticatecy.visit(Cypress.env('TEST_APP_URL'));cy.get('[data-testid="sign-in-button"]').click();cy.origin(Cypress.env('KINDE_ISSUER_URL'), () => {cy.get('input[name="p_email"]').type(Cypress.env('TEST_USER_EMAIL'));cy.get('button[type="submit"]').click();cy.get('input[name="p_password"]').type(Cypress.env('TEST_USER_PASSWORD'));cy.get('button[type="submit"]').click({ multiple: true });});const appUrl = new URL(Cypress.env('TEST_APP_URL'));cy.url().should('include', appUrl.hostname);// Now sign outcy.get('[data-testid="sign-out-button"]').click();// Should show signed-out statecy.get('[data-testid="sign-in-button"]').should('be.visible');});});
Run the tests
Link to this section-
Use the following command:
Terminal window npx cypress open -
Select the test spec to run.
You should now see the Cypress tests passing in the browser.
Testing sign-up flows
Link to this sectionKinde requires OTP email verification when signing up for a new user. See the Testing passwordless flows guide for the detailed sign-up flow examples.