-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: auth with middleware (#776)
* refactor: auth with middleware * refactor: test and comment auth-guard * fix: lint * fix: lint * Update auth-guard.ts * feat: redirect to requested page after auth * test: add e2e test for login redirect * chore: remove unused code --------- Co-authored-by: Fabian Gerke <[email protected]>
- Loading branch information
1 parent
7786f44
commit 7004420
Showing
13 changed files
with
133 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { MyAccountPageObject } from '../../support/pageObjects/MyAccountPageObject'; | ||
import { paths } from '../../../utils/paths'; | ||
|
||
const guardedRoutes = [ | ||
paths.accountPersonalData, | ||
paths.accountBillingDetails, | ||
paths.accountShippingDetails, | ||
paths.accountMyOrders, | ||
paths.accountMyWishlist, | ||
paths.accountReturns, | ||
paths.accountNewReturn + '/1/accessKey' | ||
]; | ||
|
||
describe('Auth Guard', () => { | ||
beforeEach(() => { | ||
cy.clearCookie('pwa-session-id'); | ||
}); | ||
|
||
it('should redirect from accountPersonalData to login page if user is not authorized', () => { | ||
cy.visit(paths.accountPersonalData); | ||
cy.url().should('include', paths.authLogin); | ||
}); | ||
|
||
it('should redirect from accountBillingDetails to login page if user is not authorized', () => { | ||
cy.visit(paths.accountBillingDetails); | ||
cy.url().should('include', paths.authLogin); | ||
}); | ||
|
||
it('should redirect from accountShippingDetails to login page if user is not authorized', () => { | ||
cy.visit(paths.accountShippingDetails); | ||
cy.url().should('include', paths.authLogin); | ||
}); | ||
|
||
it('should redirect from accountMyOrders to login page if user is not authorized', () => { | ||
cy.visit(paths.accountMyOrders); | ||
cy.url().should('include', paths.authLogin); | ||
}); | ||
|
||
it('should redirect from accountMyWishlist to login page if user is not authorized', () => { | ||
cy.visit(paths.accountMyWishlist); | ||
cy.url().should('include', paths.authLogin); | ||
}); | ||
|
||
it('should redirect from accountReturns to login page if user is not authorized', () => { | ||
cy.visit(paths.accountReturns); | ||
cy.url().should('include', paths.authLogin); | ||
}); | ||
|
||
it('should redirect from accountNewReturn to login page if user is not authorized', () => { | ||
cy.visit(paths.accountNewReturn + '/1/accessKey'); | ||
cy.url().should('include', paths.authLogin); | ||
}); | ||
|
||
it('should allow access to authorized users', () => { | ||
const myAccount = new MyAccountPageObject(); | ||
|
||
cy.intercept('/plentysystems/doLogin').as('doLogin'); | ||
cy.visitAndHydrate(paths.authLogin); | ||
myAccount.successLogin(); | ||
|
||
cy.wait('@doLogin'); | ||
|
||
guardedRoutes.forEach(route => { | ||
cy.visitAndHydrate(route); | ||
cy.url().should('include', route); | ||
}); | ||
}); | ||
|
||
it('should redirect back to protected page after successful login', () => { | ||
const myAccount = new MyAccountPageObject(); | ||
|
||
cy.visit(paths.accountPersonalData); | ||
cy.url().should('include', `${paths.authLogin}?redirect=${paths.accountPersonalData}`); | ||
|
||
cy.intercept('/plentysystems/doLogin').as('doLogin'); | ||
myAccount.successLogin(); | ||
|
||
cy.wait('@doLogin'); | ||
cy.url().should('include', paths.accountPersonalData); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* This middleware is used to check if the user is authorized. | ||
* | ||
* Use this auth guard to protect routes that require the user to be logged in. | ||
* | ||
* If the user is not authorized, the user will be redirected to the login page. | ||
*/ | ||
|
||
export default defineNuxtRouteMiddleware(async (to) => { | ||
const { isAuthorized, getSession } = useCustomer(); | ||
const localePath = useLocalePath(); | ||
|
||
await getSession(); | ||
|
||
if (!isAuthorized.value) { | ||
const targetUrl = to.fullPath; | ||
return navigateTo({ | ||
path: localePath(paths.authLogin), | ||
query: { | ||
redirect: targetUrl, | ||
}, | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,6 @@ | |
definePageMeta({ | ||
layout: 'account', | ||
pageType: 'static', | ||
middleware: ['auth-guard'], | ||
}); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,5 +17,6 @@ | |
definePageMeta({ | ||
layout: 'account', | ||
pageType: 'static', | ||
middleware: ['auth-guard'], | ||
}); | ||
</script> |