-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
UserModule#settings
endpoint (#231)
* Add 'UserModule#settings' endpoint * Add support for user settings via PnP-based implementation * Clear URL query upon settings callback * Update 'magic.user.settings' method name to 'magic.user.showSettings' * Add PnP-specific settings endpoint * Update yarn.lock
- Loading branch information
Showing
14 changed files
with
226 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import { createMagicInstance, getScriptData } from '../utils'; | ||
import { getScriptData } from '../utils/script-data'; | ||
import { createMagicInstance } from '../utils/magic-instance'; | ||
|
||
export async function logout(): Promise<void> { | ||
const { src, apiKey, redirectURI = window.location.origin } = getScriptData(); | ||
// In this context, `loginURI` and `redirectURI` are the same. | ||
// We simply need a location to redirect to after attempting to logout. | ||
const { src, apiKey, loginURI, redirectURI = window.location.origin } = getScriptData(); | ||
const magic = createMagicInstance(apiKey, src.origin); | ||
await magic.user.logout().catch(() => {}); | ||
window.location.href = redirectURI; | ||
window.location.href = loginURI || redirectURI; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { getScriptData } from '../utils/script-data'; | ||
import { createMagicInstance } from '../utils/magic-instance'; | ||
|
||
export async function settings(): Promise<void> { | ||
// In this context, `loginURI` and `redirectURI` have distinct purposes. | ||
// `loginURI` is the location we redirect to when calling into settings fails. | ||
// `redirectURI` is the location we redirect to after the user has dismissed the settings page. | ||
const { | ||
src, | ||
apiKey, | ||
redirectURI = `${window.location.origin}/callback`, | ||
loginURI = window.location.origin, | ||
} = getScriptData(); | ||
|
||
const magic = createMagicInstance(apiKey, src.origin); | ||
|
||
try { | ||
const prevUserMetadata = magic.pnp.encodeUserMetadata(await magic.user.getMetadata()); | ||
const currUserMetadata = magic.pnp.encodeUserMetadata(await magic.pnp.showSettings()); | ||
window.location.href = `${redirectURI}?prev_user_metadata=${prevUserMetadata}&curr_user_metadata=${currUserMetadata}`; | ||
} catch { | ||
window.location.href = loginURI; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import type { PNPMagicInstance } from './magic-instance'; | ||
|
||
export function dispatchReadyEvent(magic: PNPMagicInstance, data: any = {}) { | ||
const evt = new CustomEvent('@magic/ready', { detail: { magic, ...data } }); | ||
window.dispatchEvent(evt); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { PlugNPlayExtension } from '../pnp-extension'; | ||
import type { Magic, OAuthExtension } from '../types'; | ||
|
||
export function createMagicInstance(apiKey?: string, endpoint?: string): Magic<[PlugNPlayExtension, OAuthExtension]> { | ||
const extensions = removeFalsey([ | ||
new PlugNPlayExtension(), | ||
window.MagicOAuthExtension && new window.MagicOAuthExtension(), | ||
]); | ||
|
||
return new window.Magic(apiKey!, { | ||
endpoint, | ||
extensions, | ||
}); | ||
} | ||
|
||
export type PNPMagicInstance = ReturnType<typeof createMagicInstance>; | ||
|
||
function removeFalsey<T>(arr: T[]): Array<NonNullable<T>> { | ||
return arr.filter(Boolean) as unknown as Array<NonNullable<T>>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const allPossiblePNPScripts = document.querySelectorAll('script[data-magic-publishable-api-key]'); | ||
const thisScript = (document.currentScript ?? | ||
allPossiblePNPScripts[allPossiblePNPScripts.length - 1]) as HTMLScriptElement; | ||
|
||
export function getScriptData() { | ||
const src = new URL(thisScript.getAttribute('src')!); | ||
const apiKey = thisScript.dataset.magicPublishableApiKey; | ||
const debug = !!thisScript.dataset.debug; | ||
const redirectURI = getAbsoluteURL(thisScript.dataset.redirectUri); | ||
const loginURI = getAbsoluteURL(thisScript.dataset.loginUri); | ||
|
||
return { script: thisScript, src, apiKey, redirectURI, loginURI, debug }; | ||
} | ||
|
||
export type ScriptData = ReturnType<typeof getScriptData>; | ||
|
||
/** | ||
* Resolve `relativeURL` to an absolute path against current origin by | ||
* (naively) checking if `relativeURL` starts with "/". | ||
*/ | ||
function getAbsoluteURL(relativeURL?: string) { | ||
if (relativeURL?.startsWith('/')) return `${window.location.origin}${relativeURL}`; | ||
return relativeURL; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
packages/@magic-sdk/provider/test/spec/modules/user/showSettings.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import browserEnv from '@ikscodes/browser-env'; | ||
import { createMagicSDK, createMagicSDKTestMode } from '../../../factories'; | ||
import { isPromiEvent } from '../../../../src/util'; | ||
|
||
beforeEach(() => { | ||
browserEnv.restore(); | ||
}); | ||
|
||
test('Generate JSON RPC request payload with method `magic_auth_settings`', async () => { | ||
const magic = createMagicSDK(); | ||
magic.user.request = jest.fn(); | ||
|
||
magic.user.showSettings(); | ||
|
||
const requestPayload = magic.user.request.mock.calls[0][0]; | ||
expect(requestPayload.method).toBe('magic_auth_settings'); | ||
expect(requestPayload.params).toEqual([]); | ||
}); | ||
|
||
test('If `testMode` is enabled, testing-specific RPC method is used', async () => { | ||
const magic = createMagicSDKTestMode(); | ||
magic.user.request = jest.fn(); | ||
|
||
magic.user.showSettings(); | ||
|
||
const requestPayload = magic.user.request.mock.calls[0][0]; | ||
expect(requestPayload.method).toBe('magic_auth_settings_testing_mode'); | ||
expect(requestPayload.params).toEqual([]); | ||
}); | ||
|
||
test('method should return a PromiEvent', () => { | ||
const magic = createMagicSDK(); | ||
expect(isPromiEvent(magic.user.showSettings())).toBeTruthy(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.