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

feat(useaccountmanager): add ability to input callback functions #472

Draft
wants to merge 39 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
dc0cf98
feat(useaccountmanager): add ability to input callback functions.
brandonlammey May 24, 2021
b6e8c48
test(useaccountmanager): testing function
brandonlammey May 24, 2021
7deea8f
test(useaccountmanager): testing function
brandonlammey May 24, 2021
c36ef1c
test(useaccountmanager): testing function
brandonlammey May 24, 2021
cbb20cb
test(useaccountmanager): testing function
brandonlammey May 25, 2021
290e6cc
test(useaccountmanager): testing function
brandonlammey May 25, 2021
66b50ff
test(useaccountmanager): testing function
brandonlammey May 25, 2021
a3fcc10
test(useaccountmanager): testing function
brandonlammey May 25, 2021
31810ea
test(useaccountmanager): testing function
brandonlammey May 25, 2021
519bec3
test(useaccountmanager): testing function
brandonlammey May 25, 2021
36d880f
test(useaccountmanager): testing function
brandonlammey May 25, 2021
c8e6d25
test(useaccountmanager): testing function
brandonlammey May 25, 2021
40c6f6b
test(useaccountmanager): testing function
brandonlammey May 25, 2021
887230a
test(useaccountmanager): testing function
brandonlammey May 25, 2021
580ef3d
test(useaccountmanager): testing function
brandonlammey May 25, 2021
00de813
test(useaccountmanager): testing function
brandonlammey May 25, 2021
ca4cc96
test(useaccountmanager): testing function
brandonlammey May 25, 2021
24eea56
test(useaccountmanager): testing function
brandonlammey May 27, 2021
c8d07b6
test(useaccountmanager): testing function
brandonlammey May 27, 2021
0acf130
test(useaccountmanager): testing function
brandonlammey May 27, 2021
264169d
test(useaccountmanager): testing function
brandonlammey May 27, 2021
78e624b
test(useaccountmanager): testing function
brandonlammey May 27, 2021
4aabf24
test(useaccountmanager): testing function
brandonlammey May 28, 2021
3dbca83
test(useaccountmanager): testing function
brandonlammey May 28, 2021
de37e98
test(useaccountmanager): testing function
brandonlammey May 28, 2021
7ee7e39
test(useaccountmanager): testing function
brandonlammey May 28, 2021
af853ce
test(useaccountmanager): testing function
brandonlammey May 28, 2021
957ff4b
test(useaccountmanager): testing function
brandonlammey May 28, 2021
9260367
test(useaccountmanager): testing function
brandonlammey May 28, 2021
3b64a71
test(useaccountmanager): testing function
brandonlammey May 28, 2021
8d292b3
test(useaccountmanager): testing function
brandonlammey May 28, 2021
4d6b376
test(useaccountmanager): testing function
brandonlammey May 28, 2021
e2b12c9
test(useaccountmanager): testing function
brandonlammey May 28, 2021
f8642f3
test(useaccountmanager): testing function
brandonlammey May 28, 2021
885f6b8
test(useaccountmanager): testing function
brandonlammey May 28, 2021
96a2fa5
test(useaccountmanager): testing function
brandonlammey May 28, 2021
69d5c88
test(useaccountmanager): testing function
brandonlammey May 28, 2021
8907dfe
test(useaccountmanager): testing function
brandonlammey May 28, 2021
811fe99
test(useaccountmanager): testing function
brandonlammey May 28, 2021
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
50 changes: 37 additions & 13 deletions src/ArcgisAccount/accountManagerUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ export const beginLogin = async (
type = 'OAuth2'
) => {
if (type === 'OAuth2') {
loginOAuth2(managerName, options, setAccountManagerState);
const response = await loginOAuth2(
managerName,
options,
setAccountManagerState
);
return response;
}
};

Expand All @@ -32,7 +37,7 @@ export const loginOAuth2 = async (
// https://github.com/Esri/arcgis-rest-js/blob/master/packages/arcgis-rest-auth/src/UserSession.ts#L303
// https://github.com/Esri/arcgis-rest-js/blob/master/packages/arcgis-rest-request/src/utils/encode-query-string.ts#L28

const portal = portalUrl ? portalUrl : 'https://www.arcgis.com/sharing';
const portal = portalUrl ? portalUrl : 'https://www.arcgis.com/sharing/rest';
const url = new URL(portal);

if (!url.pathname) {
Expand Down Expand Up @@ -62,8 +67,17 @@ export const loginOAuth2 = async (

const accountManager = getAccountManagerStorage(managerName);
setAccountManagerState(accountManager);

return { account, success: true };
} catch (e) {
console.error(`Error getting User Session (loginOAuth2). ${e}`);
//error.code === 'access_denied'
//error.name === 'ArcGISAuthError'
//error.message === 'access_denied: The user denied your request.&state=
console.warn({
note: 'Error getting User Session (loginOAuth2).',
error: e
});
return { error: e, success: false };
}
} else {
try {
Expand All @@ -76,16 +90,20 @@ export const loginOAuth2 = async (
params
});
} catch (e) {
console.error(`Error getting User Session (loginOAuth2). ${e}`);
console.warn({
note: 'Error getting User Session (loginOAuth2).',
error: e
});
}
}
return null;
};

/** Complete auth and return account with serialized portal and session */
export const completeLogin = async (options, type = 'OAuth2') => {
if (type === 'OAuth2') {
const account = await completeOAuth2(options);
return account;
const response = await completeOAuth2(options);
return response;
}
};

Expand All @@ -97,7 +115,9 @@ export const completeOAuth2 = async ({
popup
}) => {
try {
const portal = portalUrl ? portalUrl : 'https://www.arcgis.com/sharing';
const portal = portalUrl
? portalUrl
: 'https://www.arcgis.com/sharing/rest';
const dSession = UserSession.completeOAuth2({
clientId,
portal,
Expand All @@ -114,12 +134,14 @@ export const completeOAuth2 = async ({
} else {
window.location.hash = '';
}
return account;
return { account, success: true };
} catch (e) {
console.error(
`Error getting User Session (completeOAuth). Error reading property may result from app redirecting before operation can read token hash in url. ${e}`
);
return null;
console.warn({
note:
'Error getting User Session (completeOAuth). Error reading property may result from app redirecting before operation can read token hash in url.',
error: e
});
return { error: e, success: false };
}
};

Expand Down Expand Up @@ -315,7 +337,9 @@ const createAccountObject = async ({ dSession, portal, clientId }) => {
};
} catch (e) {
throw new Error(
`Could not create account object. This could be due to UserSession.getUser(). ${e}`
`Could not create account object. This could be due to UserSession.getUser(). ${
e.message
}`
);
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/ArcgisAccount/doc/ArcgisAccount.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ import ArcgisAccount, {
const Example = () => {

// register an app of your own to create a unique clientId
const clientId = "dwecSqF6H1LcJ1n3"
const clientId = "Vxch59wud2if1Dqk"
const redirectUri = `${window.location.origin}/arcgis-account`

const options = {
Expand Down
90 changes: 68 additions & 22 deletions src/ArcgisAccount/useAccountManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,27 @@ const useAccountManager = (
options = {
clientId: null,
redirectUri: null,
portalUrl: 'https://www.arcgis.com/sharing',
portalUrl: 'https://www.arcgis.com/sharing/rest',
popup: false,
params: { force_login: false }
},
name = 'arcgis-account-manager'
name = 'arcgis-account-manager',
onAccountAdded = () => {
console.log('onAccountAdded');
},
onAccountRemoved = () => {
console.log('onAccountRemoved');
},
onAccountsUpdated = () => {
console.log('onAccountsUpdated');
},
onAuthCancelled = () => {
console.log('onAuthCancelled');
}
) => {
const [managerName] = useState(name);
const [managerOptions] = useState(options);
const [popupOpen, setPopupOpen] = useState(false);

const { accounts, status, active, order } = getAccountManagerStorage(
managerName
Expand All @@ -49,33 +62,55 @@ const useAccountManager = (
() => {
const { loading, authProps } = status || {};
if (loading) {
//occurs in popup
const completeAddAccount = async () => {
const account = await completeLogin(authProps);
if (account && account.key) {
const { success, error, account } =
(await completeLogin(authProps)) || {};

if (success) {
addAccountStorage(managerName, account);
}
//Update localStorage/ state

//update localStorage/ state
completeStatusStorage(managerName);
const accountManager = getAccountManagerStorage(managerName);
setAccountManagerState(accountManager);
};

//response.code === 'access_denied'
if (error && error.code === 'access_denied') {
onAuthCancelled();
}
if (success) {
onAccountAdded();
}
};
completeAddAccount();
}
},
[managerName, status]
);

useEffect(
() => {
console.log(popupOpen);
},
[popupOpen]
);

/** Add Account */
const addAccount = useCallback(
(options = null, setActive = true, type = 'OAuth2') => {
async (options = null, setActive = true, type = 'OAuth2') => {
// saving window.location.href (query params, etc) as originRoute
const originRoute = window.location.href;

const { clientId, redirectUri, portalUrl, popup, params } = options
? options || {}
: managerOptions || {};

if (popup) {
setPopupOpen(true);
}

//set localstorage status
beginStatusStorage(
managerName,
Expand All @@ -89,18 +124,28 @@ const useAccountManager = (
setActive
);
//begin login
beginLogin(
managerName,
{
clientId,
redirectUri,
portalUrl,
popup,
params
},
setAccountManagerState,
type
);
const { success, account, error } =
(await beginLogin(
managerName,
{
clientId,
redirectUri,
portalUrl,
popup,
params
},
setAccountManagerState,
type
)) || {};

setPopupOpen(false);
if (error && error.code === 'access_denied') {
onAuthCancelled();
}
if (success && account) {
console.log('here');
onAccountAdded();
}
},
[managerOptions, managerName]
);
Expand Down Expand Up @@ -165,7 +210,7 @@ const useAccountManager = (
const { username } = user || {};

if (appId && portalHostname) {
const portalUrl = `https://${portalHostname}/sharing`;
const portalUrl = `https://${portalHostname}/sharing/rest`;
const clientId = appId;
const { authProps } = accountManagerState.status || {};
const { redirectUri, popup } = authProps || { popup: false };
Expand Down Expand Up @@ -221,7 +266,7 @@ const useAccountManager = (
const { username } = user || {};

if (appId && portalHostname) {
const portalUrl = `https://${portalHostname}/sharing`;
const portalUrl = `https://${portalHostname}/sharing/rest`;
const clientId = appId;
const { authProps } = accountManagerState.status || {};
const { redirectUri, popup } = authProps || { popup: false };
Expand Down Expand Up @@ -360,6 +405,7 @@ const useAccountManager = (

return {
accountManagerState,
popupOpen,
addAccount,
logoutAccount,
logoutAllAccounts,
Expand Down Expand Up @@ -390,7 +436,7 @@ useAccountManager.defaultProps = {
options: {
clientId: null,
redirectUri: null,
portalUrl: 'https://www.arcgis.com/sharing',
portalUrl: 'https://www.arcgis.com/sharing/rest',
popup: false,
params: { force_login: false }
}
Expand Down