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

[RFR] Fix missing query string after successful login #6129

Merged
Show file tree
Hide file tree
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
17 changes: 17 additions & 0 deletions cypress/integration/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,21 @@ describe('Authentication', () => {
ListPage.navigate();
cy.url().then(url => expect(url).to.contain('/#/posts'));
});

it('should redirect to initial url keeping query string', () => {
let urlBeforeLogout;

ListPage.navigate();
ListPage.addCommentableFilter();
cy.url().then(url => {
urlBeforeLogout = url;
});
ListPage.setAsNonLogged();
cy.reload();
LoginPage.login('login', 'password');
cy.url().then(urlAfterLogin => {
expect(urlAfterLogin).to.contain(urlBeforeLogout);
});
ListPage.commentableFilter().should('exist');
});
});
16 changes: 16 additions & 0 deletions cypress/support/ListPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export default url => ({
appLoader: '.app-loader',
displayedRecords: '.displayed-records',
filter: name => `.filter-field[data-source='${name}'] input`,
filterButton: name => `.filter-field[data-source='${name}']`,
filterMenuItems: `.new-filter-item`,
menuItems: `[role=menuitem]`,
filterMenuItem: source => `.new-filter-item[data-key="${source}"]`,
Expand Down Expand Up @@ -63,6 +64,15 @@ export default url => ({
return cy.get(this.elements.pageNumber(n)).click({ force: true });
},

addCommentableFilter() {
this.openFilters();
cy.get(this.elements.filterMenuItem('commentable')).click();
},

commentableFilter() {
return cy.get(this.elements.filterButton('commentable'));
},

setFilterValue(name, value, clearPreviousValue = true) {
cy.get(this.elements.filter(name));
if (clearPreviousValue) {
Expand All @@ -88,6 +98,12 @@ export default url => ({
cy.get(this.elements.logout).click();
},

setAsNonLogged() {
cy.window().then(win => {
win.localStorage.setItem('not_authenticated', true);
});
},

toggleSelectAll() {
cy.get(this.elements.selectAll).click();
},
Expand Down
5 changes: 4 additions & 1 deletion packages/ra-core/src/auth/Authenticated.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ describe('<Authenticated>', () => {
type: 'RA/CLEAR_STATE',
});
expect(history.location.pathname).toEqual('/login');
expect(history.location.state).toEqual({ nextPathname: '/' });
expect(history.location.state).toEqual({
nextPathname: '/',
nextSearch: '',
});
});
});
});
5 changes: 4 additions & 1 deletion packages/ra-core/src/auth/useAuthenticated.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ describe('useAuthenticated', () => {
type: 'RA/CLEAR_STATE',
});
expect(history.location.pathname).toEqual('/login');
expect(history.location.state).toEqual({ nextPathname: '/' });
expect(history.location.state).toEqual({
nextPathname: '/',
nextSearch: '',
});
});
});
});
6 changes: 4 additions & 2 deletions packages/ra-core/src/auth/useLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,20 @@ const useLogin = (): Login => {
const history = useHistory();
const dispatch = useDispatch();
const nextPathName = locationState && locationState.nextPathname;
const nextSearch = locationState && locationState.nextSearch;

const login = useCallback(
(params: any = {}, pathName) =>
authProvider.login(params).then(ret => {
dispatch(resetNotification());
const redirectUrl = pathName
? pathName
: nextPathName || defaultAuthParams.afterLoginUrl;
: nextPathName + nextSearch ||
defaultAuthParams.afterLoginUrl;
history.push(redirectUrl);
return ret;
}),
[authProvider, history, nextPathName, dispatch]
[authProvider, history, nextPathName, nextSearch, dispatch]
);

const loginWithoutProvider = useCallback(
Expand Down
1 change: 1 addition & 0 deletions packages/ra-core/src/auth/useLogout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const useLogout = (): Logout => {
) {
newLocation.state = {
nextPathname: history.location.pathname,
nextSearch: history.location.search,
};
}
if (redirectToParts[1]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ const AutocompleteArrayInput = (props: AutocompleteArrayInputProps) => {
});

// eslint-disable-next-line
const debouncedSetFilter = useCallback(debounce(setFilter || DefaultSetFilter, debounceDelay),
const debouncedSetFilter = useCallback(
debounce(setFilter || DefaultSetFilter, debounceDelay),
[setFilter, debounceDelay]
);

Expand Down