Skip to content

Commit

Permalink
unify login and logout path redirect callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
juliancwirko committed Mar 30, 2024
1 parent c60ba9c commit 1839545
Show file tree
Hide file tree
Showing 11 changed files with 631 additions and 548 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### [0.18.0](https://github.com/useElven/core/releases/tag/v0.18.0) (2024-03-30)
- unify path calbacks for useLogin
- update dependencies

### [0.17.1](https://github.com/useElven/core/releases/tag/v0.17.1) (2024-02-28)
- fix the useTokenTransfer payload for Fungible ESDT
- fix typings where TypedValue was used
Expand Down
1,083 changes: 581 additions & 502 deletions package-lock.json

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@useelven/core",
"version": "0.17.1",
"version": "0.18.0",
"description": "Core React hooks for MultiversX DApps",
"license": "MIT",
"author": "Julian Ćwirko <julian.io>",
Expand Down Expand Up @@ -65,32 +65,32 @@
"prepublishOnly": "npm run build"
},
"dependencies": {
"@multiversx/sdk-core": "12.19.0",
"@multiversx/sdk-core": "12.19.1",
"@multiversx/sdk-extension-provider": "3.0.0",
"@multiversx/sdk-hw-provider": "6.4.0",
"@multiversx/sdk-native-auth-client": "1.0.7",
"@multiversx/sdk-network-providers": "2.3.0",
"@multiversx/sdk-native-auth-client": "1.0.8",
"@multiversx/sdk-network-providers": "2.4.3",
"@multiversx/sdk-wallet-connect-provider": "4.1.1",
"@multiversx/sdk-web-wallet-provider": "3.2.1",
"lodash.clonedeep": "4.5.0",
"swr": "2.2.5",
"valtio": "1.13.1"
"valtio": "1.13.2"
},
"devDependencies": {
"@types/lodash.clonedeep": "4.5.9",
"@types/node": "20.11.21",
"@types/react": "18.2.60",
"@typescript-eslint/eslint-plugin": "7.1.0",
"@typescript-eslint/parser": "7.1.0",
"@types/node": "20.12.2",
"@types/react": "18.2.73",
"@typescript-eslint/eslint-plugin": "7.4.0",
"@typescript-eslint/parser": "7.4.0",
"eslint": "8.57.0",
"eslint-config-prettier": "9.1.0",
"eslint-plugin-prettier": "5.1.3",
"eslint-plugin-react": "7.33.2",
"eslint-plugin-react": "7.34.1",
"eslint-plugin-react-hooks": "4.6.0",
"prettier": "3.2.5",
"rimraf": "5.0.5",
"tsup": "8.0.2",
"typescript": "5.3.3"
"typescript": "5.4.3"
},
"peerDependencies": {
"react": "^18.2.0"
Expand Down
16 changes: 6 additions & 10 deletions src/hooks/common-helpers/signAndSendTxOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { WalletProvider } from '@multiversx/sdk-web-wallet-provider';
import { DappProvider } from '../../types/network';
import { errorParse } from '../../utils/errorParse';
import { DAPP_INIT_ROUTE } from '../../config/network';
import { getCallbackUrl } from '../../utils/getCallbackUrl';

export interface TransactionCallbackParams {
transaction?: Transaction | null;
Expand Down Expand Up @@ -109,12 +110,9 @@ export const checkNeedsGuardianSigning = (
return true;
};

const getCallbackUrl = (ongoingTxId?: string, callbackUrl?: string) => {
const currentUrl = window?.location?.href;
const clbck =
callbackUrl && window
? `${window.location.origin}${callbackUrl}`
: currentUrl;
const getClbckUrl = (ongoingTxId?: string, callbackUrl?: string) => {
const clbck = getCallbackUrl(callbackUrl);

if (!ongoingTxId) return clbck;

const alteredCallbackUrl = new URL(clbck);
Expand All @@ -133,7 +131,7 @@ export const sendTxToGuardian = async (
`${walletAddress}${DAPP_INIT_ROUTE}`
);

const clbck = getCallbackUrl(ongoingTxId, callbackUrl);
const clbck = getClbckUrl(ongoingTxId, callbackUrl);

const alteredCallbackUrl = new URL(clbck);
alteredCallbackUrl.searchParams.set(
Expand Down Expand Up @@ -172,9 +170,7 @@ export const signAndSendTxOperations = async (
try {
if (dappProvider instanceof WalletProvider) {
await dappProvider.signTransaction(tx, {
callbackUrl: encodeURIComponent(
getCallbackUrl(ongoingTxId, callbackUrl)
),
callbackUrl: encodeURIComponent(getClbckUrl(ongoingTxId, callbackUrl)),
});
}
if (dappProvider instanceof ExtensionProvider) {
Expand Down
11 changes: 3 additions & 8 deletions src/hooks/common-helpers/useWebWalletXaliasLogin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { errorParse } from '../../utils/errorParse';
import { useConfig } from '../useConfig';
import { getLoginToken } from '../common-helpers/getLoginToken';
import { useEffect } from 'react';
import { getParamFromUrl } from 'src/utils/getParamFromUrl';
import { getParamFromUrl } from '../../utils/getParamFromUrl';
import { getCallbackUrl } from '../../utils/getCallbackUrl';

export const useWebWalletXaliasLogin = (
type: 'webwallet' | 'xalias',
Expand Down Expand Up @@ -40,14 +41,8 @@ export const useWebWalletXaliasLogin = (
}${DAPP_INIT_ROUTE}`
);

const callbackUrl: string =
typeof window !== 'undefined'
? encodeURIComponent(
`${window.location.origin}${params?.callbackUrl || '/'}`
)
: '/';
const providerLoginData = {
callbackUrl,
callbackUrl: getCallbackUrl(params?.callbackUrl),
token: loginToken,
};

Expand Down
13 changes: 5 additions & 8 deletions src/hooks/useExtensionLogin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { errorParse } from '../utils/errorParse';
import { getLoginToken } from './common-helpers/getLoginToken';
import { useNetwork } from './useNetwork';
import { getNativeAuthClient } from '../utils/getNativeAuthClient';
import { getCallbackUrl } from '../utils/getCallbackUrl';

export const useExtensionLogin = (params?: Login) => {
const { logout } = useLogout();
Expand Down Expand Up @@ -43,14 +44,8 @@ export const useExtensionLogin = (params?: Login) => {

setLoginInfoState('loginMethod', LoginMethodsEnum.extension);

const callbackUrl: string =
typeof window !== 'undefined'
? encodeURIComponent(
`${window.location.origin}${params?.callbackUrl}`
)
: '/';
const providerLoginData = {
callbackUrl,
callbackUrl: getCallbackUrl(params?.callbackUrl),
token: loginToken,
};

Expand Down Expand Up @@ -119,7 +114,9 @@ export const useExtensionLogin = (params?: Login) => {
setLoginInfoState('accessToken', accessToken);
}

optionalRedirect(params?.callbackUrl);
if (params?.callbackUrl) {
optionalRedirect(getCallbackUrl(params?.callbackUrl));
}
} catch (e) {
const err = errorParse(e);
setLoggingInState('error', `Error logging in ${err}`);
Expand Down
5 changes: 4 additions & 1 deletion src/hooks/useLedgerLogin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { errorParse } from '../utils/errorParse';
import { useNetwork } from './useNetwork';
import { getLoginToken } from './common-helpers/getLoginToken';
import { getNativeAuthClient } from '../utils/getNativeAuthClient';
import { getCallbackUrl } from '../utils/getCallbackUrl';

export const useLedgerLogin = (params?: Login) => {
const { logout } = useLogout();
Expand Down Expand Up @@ -102,7 +103,9 @@ export const useLedgerLogin = (params?: Login) => {

setLoginInfoState('expires', getNewLoginExpiresTimestamp());

optionalRedirect(params?.callbackUrl);
if (params?.callbackUrl) {
optionalRedirect(getCallbackUrl(params?.callbackUrl));
}
} catch (e) {
const err = errorParse(e);
setLoggingInState('error', `Error logging in ${err}`);
Expand Down
9 changes: 3 additions & 6 deletions src/hooks/useLogout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { DappProvider } from '../types/network';
import { errorParse } from '../utils/errorParse';
import { useLoggingIn } from './useLoggingIn';
import { useNetwork } from './useNetwork';
import { optionalRedirect } from '../utils/optionalRedirect';
import { getCallbackUrl } from '../utils/getCallbackUrl';

export interface Logout {
dappProvider?: DappProvider;
callbackUrl?: string;
redirectFn?: (callbackUrl?: string) => void;
}

export const useLogout = () => {
Expand All @@ -27,11 +28,7 @@ export const useLogout = () => {
await dappProvider.logout();

if (params?.callbackUrl) {
if (typeof params?.redirectFn === 'function') {
params?.redirectFn(params?.callbackUrl);
} else if (typeof window !== 'undefined') {
window.location.href = params?.callbackUrl;
}
optionalRedirect(getCallbackUrl(params?.callbackUrl));
}

setLoggingInState('loggedIn', false);
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/useMobileAppLogin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { useConfig } from './useConfig';
import { useNetwork } from './useNetwork';
import { getLoginToken } from './common-helpers/getLoginToken';
import { getNativeAuthClient } from '../utils/getNativeAuthClient';
import { getCallbackUrl } from '../utils/getCallbackUrl';

export interface PairingTypesStruct {
topic: string;
Expand Down Expand Up @@ -64,7 +65,7 @@ export const useMobileAppLogin = (params?: Login) => {

const handleOnLogout = () => {
logout({
callbackUrl: params?.callbackUrl,
callbackUrl: getCallbackUrl(params?.callbackUrl),
dappProvider: dappProviderRef?.current,
});
};
Expand Down
8 changes: 8 additions & 0 deletions src/utils/getCallbackUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const getCallbackUrl = (callbackUrl?: string) => {
const currentUrl = window?.location?.href;
const clbck =
callbackUrl && window
? `${window.location.origin}${callbackUrl}`
: currentUrl;
return clbck;
};
5 changes: 4 additions & 1 deletion src/utils/walletConnectCbs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { setAccountState, setLoginInfoState } from '../store/auth';
import { LoginMethodsEnum } from '../types/enums';
import { optionalRedirect } from '../utils/optionalRedirect';
import { errorParse } from './errorParse';
import { getCallbackUrl } from './getCallbackUrl';

export const WcOnLogin = async (
apiNetworkProvider?: ApiNetworkProvider,
Expand Down Expand Up @@ -41,5 +42,7 @@ export const WcOnLogin = async (

setLoginInfoState('loginMethod', LoginMethodsEnum.walletconnect);

optionalRedirect(callbackUrl);
if (callbackUrl) {
optionalRedirect(getCallbackUrl(callbackUrl));
}
};

0 comments on commit 1839545

Please sign in to comment.