Skip to content

Commit

Permalink
refactor: remove boolean promise
Browse files Browse the repository at this point in the history
  • Loading branch information
gao-sun committed Aug 4, 2023
1 parent bb6ea91 commit 02fb881
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
30 changes: 16 additions & 14 deletions packages/capacitor/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { App } from '@capacitor/app';
import { Browser, type OpenOptions } from '@capacitor/browser';
import { Preferences } from '@capacitor/preferences';
import LogtoBaseClient, { type InteractionMode, type LogtoConfig } from '@logto/browser';
import LogtoBaseClient, {
LogtoClientError,
type InteractionMode,
type LogtoConfig,
} from '@logto/browser';

export type CapacitorConfig = {
/**
Expand Down Expand Up @@ -57,8 +61,7 @@ export default class CapacitorLogtoClient extends LogtoBaseClient {
* @param redirectUri The redirect URI that the user will be redirected to after the sign-in flow is completed.
* @param interactionMode The interaction mode to be used for the authorization request. Note it's not
* a part of the OIDC standard, but a Logto-specific extension. Defaults to `signIn`.
*
* @returns `true` if the user completes the sign-in flow and is redirected back to the app; `false` if the user closes the browser during the sign-in.
* @throws {@link LogtoClientError} If error happens during the sign-in flow or the user cancels the sign-in.
*
* @example
* ```ts
Expand All @@ -67,8 +70,8 @@ export default class CapacitorLogtoClient extends LogtoBaseClient {
* appId: 'your-app-id',
* });
*
* const success = await client.signIn('io.logto.example://callback'); // true or false
* console.log(await client.getIdTokenClaims()); // { sub: '123', ... } or throws error
* await client.signIn('io.logto.example://callback'); // throws if error happens
* console.log(await client.getIdTokenClaims()); // { sub: '123', ... }
* ```
*
* @remarks
Expand All @@ -79,14 +82,14 @@ export default class CapacitorLogtoClient extends LogtoBaseClient {
* @see {@link https://docs.logto.io/docs/recipes/integrate-logto/vanilla-js/#sign-in | Sign in} for more information.
* @see {@link InteractionMode}
*/
async signIn(redirectUri: string, interactionMode?: InteractionMode): Promise<boolean> {
return new Promise((resolve) => {
async signIn(redirectUri: string, interactionMode?: InteractionMode): Promise<void> {
return new Promise((resolve, reject) => {
const run = async () => {
const [browserHandle, appHandle] = await Promise.all([
// Handle the case where the user closes the browser during the sign-in.
Browser.addListener('browserFinished', async () => {
await Promise.all([browserHandle.remove(), appHandle.remove()]);
resolve(false);
reject(new LogtoClientError('user_cancelled'));
}),
// Handle the case where the user completes the sign-in and is redirected
// back to the app.
Expand All @@ -103,7 +106,7 @@ export default class CapacitorLogtoClient extends LogtoBaseClient {
browserHandle.remove(),
appHandle.remove(),
]);
resolve(true);
resolve();
}),
// Open the in-app browser to start the sign-in flow
super.signIn(redirectUri, interactionMode),
Expand All @@ -129,14 +132,13 @@ export default class CapacitorLogtoClient extends LogtoBaseClient {
* to ensure the app can be opened from the redirect URI.
*
* @param postLogoutRedirectUri The URI that the user will be redirected to after the sign-out flow is completed.
* @returns `true` if the user completes the sign-out flow and is redirected back to the app.
*
* @example
* ```ts
* await client.signOut('io.logto.example://callback'); // true
* await client.signOut('io.logto.example://callback');
* ```
*/
async signOut(postLogoutRedirectUri?: string): Promise<true> {
async signOut(postLogoutRedirectUri?: string): Promise<void> {
return new Promise((resolve) => {
const run = async () => {
const [handle] = await Promise.all([
Expand All @@ -146,11 +148,11 @@ export default class CapacitorLogtoClient extends LogtoBaseClient {
return;
}
await Promise.all([Browser.close(), handle.remove()]);
resolve(true);
resolve();
})
: Browser.addListener('browserFinished', async () => {
await handle.remove();
resolve(true);
resolve();
}),
super.signOut(postLogoutRedirectUri),
]);
Expand Down
1 change: 1 addition & 0 deletions packages/client/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const logtoClientErrorCodes = Object.freeze({
'sign_in_session.not_found': 'Sign-in session not found.',
not_authenticated: 'Not authenticated.',
fetch_user_info_failed: 'Unable to fetch user info. The access token may be invalid.',
user_cancelled: 'The user cancelled the action.',
});

export type LogtoClientErrorCode = keyof typeof logtoClientErrorCodes;
Expand Down
8 changes: 2 additions & 6 deletions packages/client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export default class LogtoClient {
* @see {@link https://docs.logto.io/docs/recipes/integrate-logto/vanilla-js/#sign-in | Sign in} for more information.
* @see {@link InteractionMode}
*/
async signIn(redirectUri: string, interactionMode?: InteractionMode): Promise<unknown> {
async signIn(redirectUri: string, interactionMode?: InteractionMode): Promise<void> {
const { appId: clientId, prompt, resources, scopes } = this.logtoConfig;
const { authorizationEndpoint } = await this.getOidcConfig();
const codeVerifier = this.adapter.generateCodeVerifier();
Expand All @@ -220,8 +220,6 @@ export default class LogtoClient {
this.setIdToken(null),
]);
await this.adapter.navigate(signInUri);

return true;
}

/**
Expand Down Expand Up @@ -307,7 +305,7 @@ export default class LogtoClient {
* If the `postLogoutRedirectUri` is not specified, the user will be redirected
* to a default page.
*/
async signOut(postLogoutRedirectUri?: string): Promise<unknown> {
async signOut(postLogoutRedirectUri?: string): Promise<void> {
const { appId: clientId } = this.logtoConfig;
const { endSessionEndpoint, revocationEndpoint } = await this.getOidcConfig();
const refreshToken = await this.getRefreshToken();
Expand All @@ -334,8 +332,6 @@ export default class LogtoClient {
this.adapter.storage.removeItem('accessToken'),
]);
await this.adapter.navigate(url);

return true;
}

protected async getSignInSession(): Promise<Nullable<LogtoSignInSessionItem>> {
Expand Down

0 comments on commit 02fb881

Please sign in to comment.