Next.js Discord

Discord Forum

Next 13.5.4 cypress tests failing due to programmatic login (next-auth) not working

Unanswered
Sun bear posted this in #help-forum
Open in Discord
Sun bearOP
Hello, I have some tests where I programmatically log in, these tests were working on 13.2.4 however in 13.5.4 they're not, interestingly they work 100% of the time on my local machine when i do npm run build; npm run start;

This is my cypress test at the point i call an endpoint it fails with 401 - unauthorized


Cypress.Commands.add("login", (username = Cypress.env("APP_USERNAME"), password = Cypress.env("APP_PASSWORD")) => {
        cy.intercept("/api/auth/session").as("signIn");
        cy.visit("/signin");
        cy.wait("@signIn");
        let csrfTokenName = Cypress.config("baseUrl").startsWith(
                "http://localhost"
            ) ?
            "next-auth.csrf-token" :
            "__Host-next-auth.csrf-token";
            
        cy.getCookie(csrfTokenName)
            .should("exist")
            .then((cookie) => {
                let requestBody = {
                    redirect: false,
                    email: username,
                    password: password,
                    csrfToken: cookie.value.split("%7C")[0],
                    callbackUrl: `${Cypress.config("baseUrl")}/signin`,
                    json: true,
                };
                cy.request(
                    "POST",
                    "/api/auth/callback/credentials?",
                    requestBody
                ).then(() => {
                    //-- THIS IS WHERE IT FAILS with 401 - unauthorized
                    cy.request("POST", "/api/someapiendpoint");
                });
            });
    }
);

0 Replies