Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: cache sessions for multiple users #52

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 30 additions & 37 deletions src/commands/login.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,46 @@
let cachedUsername;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure this variable was ever set, so I removed it and everything consuming it. I believe the sessionStore is an expansion of its intended behavior.

let sessionStore = {};

Cypress.Commands.add('login', (credentials = {}) => {
const { username, password } = credentials;
const cachedUserIsCurrentUser = cachedUsername && cachedUsername === username;
const _credentials = {
username: username || Cypress.env('auth0Username'),
password: password || Cypress.env('auth0Password'),
};

const sessionCookieName = Cypress.env('auth0SessionCookieName');

/* https://github.com/auth0/nextjs-auth0/blob/master/src/handlers/login.ts#L70 */

try {
cy.getCookie(sessionCookieName).then(cookieValue => {
/* Skip logging in again if session already exists */

if (cookieValue && cachedUserIsCurrentUser) {
return true;
} else {
cy.clearCookies();

cy.getUserTokens(_credentials).then(response => {
const { accessToken, expiresIn, idToken, scope } = response;

cy.getUserInfo(accessToken).then(user => {
/* https://github.com/auth0/nextjs-auth0/blob/master/src/handlers/callback.ts#L44 */
/* https://github.com/auth0/nextjs-auth0/blob/master/src/handlers/callback.ts#L47 */
/* https://github.com/auth0/nextjs-auth0/blob/master/src/session/cookie-store/index.ts#L57 */

const payload = {
secret: Cypress.env('auth0CookieSecret'),
user,
idToken,
accessToken,
accessTokenScope: scope,
accessTokenExpiresAt: Date.now() + expiresIn,
createdAt: Date.now(),
};

/* https://github.com/auth0/nextjs-auth0/blob/master/src/session/cookie-store/index.ts#L73 */

cy.task('encrypt', payload).then(encryptedSession => {
cy._setAuth0Cookie(encryptedSession);
});
const session = sessionStore[username];
if (session) {
cy._setAuth0Cookie(session);
} else {
cy.getUserTokens(_credentials).then(response => {
const { accessToken, expiresIn, idToken, scope } = response;

cy.getUserInfo(accessToken).then(user => {
/* https://github.com/auth0/nextjs-auth0/blob/master/src/handlers/callback.ts#L44 */
/* https://github.com/auth0/nextjs-auth0/blob/master/src/handlers/callback.ts#L47 */
/* https://github.com/auth0/nextjs-auth0/blob/master/src/session/cookie-store/index.ts#L57 */

const payload = {
secret: Cypress.env('auth0CookieSecret'),
user,
idToken,
accessToken,
accessTokenScope: scope,
accessTokenExpiresAt: Date.now() + expiresIn,
createdAt: Date.now(),
};

/* https://github.com/auth0/nextjs-auth0/blob/master/src/session/cookie-store/index.ts#L73 */

cy.task('encrypt', payload).then(encryptedSession => {
sessionStore[username] = encryptedSession;
cy._setAuth0Cookie(encryptedSession);
});
});
}
});
});
}
} catch (error) {
// throw new Error(error);
}
Expand Down