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

Development #974

Merged
merged 4 commits into from
Nov 28, 2023
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [[v2.24.1]](https://github.com/multiversx/mx-sdk-dapp/pull/973)] - 2023-11-28
- [Fixed logout redirect loop](https://github.com/multiversx/mx-sdk-dapp/pull/973)

## [[v2.24.0]](https://github.com/multiversx/mx-sdk-dapp/pull/972)] - 2023-11-24
- [Full SSR support](https://github.com/multiversx/mx-sdk-dapp/pull/971)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@multiversx/sdk-dapp",
"version": "2.24.0",
"version": "2.24.1",
"description": "A library to hold the main logic for a dapp on the MultiversX blockchain",
"author": "MultiversX",
"license": "GPL-3.0-or-later",
Expand Down
32 changes: 12 additions & 20 deletions src/utils/logout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { getAccountProvider, getProviderType } from 'providers';
import { logoutAction } from 'reduxStore/commonActions';
import { store } from 'reduxStore/store';
import { LoginMethodsEnum } from 'types';
import { getIsLoggedIn } from 'utils/getIsLoggedIn';
import { getAddress } from './account';
import { getAddress, getWebviewToken } from './account';
import { preventRedirects, safeRedirect } from './redirect';
import { storage } from './storage';
import { localStorageKeys } from './storage/local';
Expand All @@ -28,46 +27,39 @@ const broadcastLogoutAcrossTabs = (address: string) => {
export async function logout(
callbackUrl?: string,
onRedirect?: (callbackUrl?: string) => void,
shouldAttemptRelogin = true
shouldAttemptRelogin = Boolean(getWebviewToken())
) {
const provider = getAccountProvider();
const providerType = getProviderType(provider);
const isLoggedIn = getIsLoggedIn();
const isWalletProvider = providerType === LoginMethodsEnum.wallet;

if (shouldAttemptRelogin && provider?.relogin != null) {
return await provider.relogin();
}

if (!isLoggedIn || !provider) {
redirectToCallbackUrl(callbackUrl, onRedirect);
return;
const url = addOriginToLocationPath(callbackUrl);

if (isWalletProvider) {
preventRedirects();
}

store.dispatch(logoutAction());

try {
const address = await getAddress();
broadcastLogoutAcrossTabs(address);
} catch (err) {
redirectToCallbackUrl(callbackUrl, onRedirect);
console.error('error fetching logout address', err);
return redirectToCallbackUrl(url, onRedirect);
}

if (isWalletProvider) {
preventRedirects();
if (!provider || providerType === LoginMethodsEnum.none) {
return redirectToCallbackUrl(url, onRedirect);
}

store.dispatch(logoutAction());

try {
const url = addOriginToLocationPath(callbackUrl);

if (providerType === LoginMethodsEnum.none) {
// logout does not exist in empty provider
return redirectToCallbackUrl(url, onRedirect);
}

if (isWalletProvider) {
// allow Redux clearing it's state before navigation
// allow Redux clearing its state before navigation
setTimeout(() => {
provider.logout({ callbackUrl: url });
});
Expand Down
3 changes: 1 addition & 2 deletions src/utils/window/addOriginToLocationPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import { getWindowLocation } from './getWindowLocation';

export const addOriginToLocationPath = (path = '') => {
const location = getWindowLocation();

const isHrefUrl = path.startsWith('http') || path.startsWith('www.');

const shouldNotChangePath =
!location.origin || path.includes(location.origin) || isHrefUrl;
!location.origin || path.startsWith(location.origin) || isHrefUrl;

if (shouldNotChangePath) {
return path;
Expand Down
Loading