Skip to content

Commit 091a7d3

Browse files
committed
Refactor examples to encapsulate login flow
1 parent 06a8c6f commit 091a7d3

File tree

1 file changed

+15
-27
lines changed

1 file changed

+15
-27
lines changed

content/guides/end-to-end-testing/auth0-authentication.md

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -144,19 +144,11 @@ Next, we'll write a custom command called `loginToAuth0` to perform a login to
144144
```js
145145
// cypress/support/auth-provider-commands/auth0.ts
146146

147-
Cypress.Commands.add('loginToAuth0', (username: string, password: string) => {
148-
const log = Cypress.log({
149-
displayName: 'AUTH0 LOGIN',
150-
message: [`🔐 Authenticating | ${username}`],
151-
// @ts-ignore
152-
autoEnd: false,
153-
})
154-
log.snapshot('before')
155-
147+
function loginViaAuth0Ui(username: string, password: string) {
156148
// App landing page redirects to Auth0.
157149
cy.visit('/')
158150

159-
// Login on Auth0 (we'll add caching later).
151+
// Login on Auth0.
160152
cy.origin(
161153
Cypress.env('auth0_domain'),
162154
{ args: { username, password } },
@@ -169,6 +161,18 @@ Cypress.Commands.add('loginToAuth0', (username: string, password: string) => {
169161

170162
// Ensure Auth0 has redirected us back to the RWA.
171163
cy.url().should('equal', 'http://localhost:3000/')
164+
}
165+
166+
Cypress.Commands.add('loginToAuth0', (username: string, password: string) => {
167+
const log = Cypress.log({
168+
displayName: 'AUTH0 LOGIN',
169+
message: [`🔐 Authenticating | ${username}`],
170+
// @ts-ignore
171+
autoEnd: false,
172+
})
173+
log.snapshot('before')
174+
175+
loginViaAuth0Ui(username, password)
172176

173177
log.snapshot('after')
174178
log.end()
@@ -221,26 +225,10 @@ Cypress.Commands.add('loginToAuth0', (username: string, password: string) => {
221225
})
222226
log.snapshot('before')
223227

224-
const args = { username, password }
225228
cy.session(
226229
`auth0-${username}`,
227230
() => {
228-
// App landing page redirects to Auth0.
229-
cy.visit('/')
230-
231-
// Login on Auth0.
232-
cy.origin(
233-
Cypress.env('auth0_domain'),
234-
{ args },
235-
({ username, password }) => {
236-
cy.get('input#username').type(username)
237-
cy.get('input#password').type(password, { log: false })
238-
cy.contains('button[value=default]', 'Continue').click()
239-
}
240-
)
241-
242-
// Ensure Auth0 has redirected us back to the RWA.
243-
cy.url().should('equal', 'http://localhost:3000/')
231+
loginViaAuth0Ui(username, password)
244232
},
245233
{
246234
validate: () => {

0 commit comments

Comments
 (0)