From 0abb68c62e27f1de8c01222d38b19b6450317ba3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Schubert?= <1551001+renet@users.noreply.github.com> Date: Thu, 7 Apr 2022 16:42:33 +0200 Subject: [PATCH 1/2] feat: add getLoginUrl function --- packages/auth/README.md | 16 ++++++++++++++++ packages/auth/src/index.tsx | 31 +++++++++++++++---------------- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/packages/auth/README.md b/packages/auth/README.md index ce92c29..6885f3d 100644 --- a/packages/auth/README.md +++ b/packages/auth/README.md @@ -102,6 +102,22 @@ Provides the `UserSession` object if the user is authenticated. ## Methods +### getLoginUrl + +▸ `Optional` **getLoginUrl**(`opts?`): `void` + +Function to generate the login URL. + +#### Parameters + +| Name | Type | +| :------ | :------ | +| `opts?` | [`LoginOptions`](#LoginOptions) | + +#### Returns + +`string` + ### login ▸ `Optional` **login**(`opts?`): `void` diff --git a/packages/auth/src/index.tsx b/packages/auth/src/index.tsx index 2e7966f..d265415 100644 --- a/packages/auth/src/index.tsx +++ b/packages/auth/src/index.tsx @@ -66,11 +66,6 @@ interface IdpResponse { code: string; } -interface IdpErrorResponse { - error: string; - error_description: string; -} - function isTokenResponse(args: unknown): args is TokenResponse { return isObject(args) && typeof args.access_token === 'string' && @@ -85,12 +80,6 @@ function isIdpResponse(args: unknown): args is IdpResponse { return isObject(args) && typeof args.code === 'string'; } -function isIdpErrorResponse(args: unknown): args is IdpErrorResponse { - return isObject(args) && - typeof args.error === 'string' && - typeof args.error_description === 'string'; -} - function isString(arg: unknown): arg is string { return typeof arg === 'string'; } @@ -130,6 +119,8 @@ export interface UserInfo { * The user context object is returned by the `useUser` hook. */ export interface UserContext { + /** Function to generate the login URL. */ + getLoginUrl?: (opts?: LoginOptions) => Promise; /** Provides the `UserInfo` object if the user is authenticated. */ info?: UserInfo; /** Provides the `UserSession` object if the user is authenticated. */ @@ -207,9 +198,9 @@ export function UserContextProvider({ const [userInfo, setUserInfo] = useState(); - const login = useCallback(async ( + const getLoginUrl = useCallback(async ( { entrypoint, redirect = true }: LoginOptions = {} - ): Promise => { + ) => { const newKey = await generateVerifier(); const encodedKey = base64encode(newKey); setKey(encodedKey); @@ -219,8 +210,7 @@ export function UserContextProvider({ } const challenge = base64encode(await sha256(encodedKey)); - document.location.href = - `https://${idpHost}/oauth2/authorize?` + + return `https://${idpHost}/oauth2/authorize?` + querystring.stringify({ client_id: clientId, domain_hint: domainHint, @@ -233,6 +223,14 @@ export function UserContextProvider({ }); }, [setKey, idpHost, clientId, domainHint, redirectUri, setEntrypoint, prompt]); + const login = useCallback(async ( + options: LoginOptions = {} + ): Promise => { + const loginUrl = await getLoginUrl(options); + + document.location.href = loginUrl; + }, [getLoginUrl]); + const logout = useCallback((): void => { clearSession(); setUserInfo(undefined); @@ -359,13 +357,14 @@ export function UserContextProvider({ { return { + getLoginUrl, info: userInfo, session, login, logout, authHeader, }; - }, [session, userInfo, login, logout, authHeader])} + }, [getLoginUrl, userInfo, session, login, logout, authHeader])} >{!autoLogin || userInfo ? children : null} ); } From bcf7e9b2a76b21742a80fdd17ac9b7324eddb92b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Schubert?= <1551001+renet@users.noreply.github.com> Date: Thu, 7 Apr 2022 16:44:46 +0200 Subject: [PATCH 2/2] docs: update README with correct return type for getLoginUrl --- packages/auth/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/auth/README.md b/packages/auth/README.md index 6885f3d..99fa367 100644 --- a/packages/auth/README.md +++ b/packages/auth/README.md @@ -104,7 +104,7 @@ Provides the `UserSession` object if the user is authenticated. ### getLoginUrl -▸ `Optional` **getLoginUrl**(`opts?`): `void` +▸ `Optional` **getLoginUrl**(`opts?`): `Promise` Function to generate the login URL. @@ -116,7 +116,7 @@ Function to generate the login URL. #### Returns -`string` +`Promise` ### login