Skip to content

Commit

Permalink
Merge pull request #974 from multiversx/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
razvantomegea authored Nov 28, 2023
2 parents 296fad8 + 2efa001 commit 6f01e55
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 111 deletions.
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

0 comments on commit 6f01e55

Please sign in to comment.