Skip to content

Commit 6f7a52c

Browse files
committed
chore: add comments
1 parent 1decb3c commit 6f7a52c

File tree

11 files changed

+84
-13
lines changed

11 files changed

+84
-13
lines changed

packages/rn-sample/App.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { StatusBar } from 'expo-status-bar';
66
import { useEffect, useState } from 'react';
77
import { Button, StyleSheet, Text, View } from 'react-native';
88

9+
// Replace with your own redirect URI
910
const redirectUri = 'io.logto://callback';
1011

1112
const Content = () => {
@@ -49,8 +50,8 @@ const App = () => {
4950
return (
5051
<LogtoProvider
5152
config={{
52-
endpoint: 'http://localhost:3002/',
53-
appId: 's5sc0ktp6fs0a8rwqod6h',
53+
endpoint: 'https://<your-logto-endpoint>',
54+
appId: '<your-app-id>',
5455
}}
5556
>
5657
<Content />

packages/rn-sample/babel.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = function(api) {
1+
module.exports = function (api) {
22
api.cache(true);
33
return {
44
presets: ['babel-preset-expo'],

packages/rn-sample/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { registerRootComponent } from 'expo';
66

77
import App from './App';
88

9-
// registerRootComponent calls AppRegistry.registerComponent('main', () => App);
9+
// `registerRootComponent` calls `AppRegistry.registerComponent('main', () => App);`
1010
// It also ensures that whether you load the app in Expo Go or in a native build,
1111
// the environment is set up appropriately
1212
registerRootComponent(App);

packages/rn-sample/metro.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
* @fileoverview https://docs.expo.dev/guides/monorepos/#modify-the-metro-config
33
*/
44

5+
const path = require('node:path');
6+
57
const { getDefaultConfig } = require('expo/metro-config');
6-
const path = require('path');
78

89
// Find the project and workspace directories
910
const projectRoot = __dirname;

packages/rn/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"name": "@logto/rn",
3+
"publishConfig": {
4+
"access": "public"
5+
},
36
"version": "0.1.0",
47
"main": "./lib/index.js",
58
"types": "./lib/index.d.ts",
@@ -45,9 +48,6 @@
4548
"extends": "@silverhand/react"
4649
},
4750
"prettier": "@silverhand/eslint-config/.prettierrc",
48-
"publishConfig": {
49-
"access": "public"
50-
},
5151
"dependencies": {
5252
"@logto/client": "3.0.0-alpha.2",
5353
"@logto/js": "4.0.0-alpha.2",

packages/rn/src/client.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export class LogtoClient extends StandardLogtoClient {
6262
break;
6363
}
6464
default: {
65-
throw new LogtoNativeClientError('auth_session_failed');
65+
throw new LogtoNativeClientError('navigation_purpose_not_supported');
6666
}
6767
}
6868
},
@@ -97,6 +97,20 @@ export class LogtoClient extends StandardLogtoClient {
9797
this.storage = storage;
9898
}
9999

100+
/**
101+
* Start the sign-in flow with the specified redirect URI. The URI must be registered in the
102+
* Logto Console. It uses `WebBrowser.openAuthSessionAsync` to open the browser and start the
103+
* sign-in flow.
104+
*
105+
* The user will be redirected to that URI after the sign-in flow is completed, and the client
106+
* will handle the callback to exchange the authorization code for the tokens.
107+
*
108+
* @param redirectUri The redirect URI that the user will be redirected to after the sign-in flow is completed.
109+
* @param interactionMode The interaction mode to be used for the authorization request. Note it's not
110+
* a part of the OIDC standard, but a Logto-specific extension. Defaults to `signIn`.
111+
*
112+
* @see {@link InteractionMode}
113+
*/
100114
override async signIn(redirectUri: string, interactionMode?: InteractionMode): Promise<void> {
101115
await super.signIn(redirectUri, interactionMode);
102116

@@ -106,4 +120,13 @@ export class LogtoClient extends StandardLogtoClient {
106120

107121
await this.handleSignInCallback(this.authSessionResult.url);
108122
}
123+
124+
/**
125+
* Revokes all the tokens and cleans up the storage. By default, it will NOT open the browser
126+
* to start the sign-out flow for better user experience, and the `postLogoutRedirectUri` will
127+
* be ignored.
128+
*/
129+
override async signOut(postLogoutRedirectUri?: string | undefined): Promise<void> {
130+
return super.signOut(postLogoutRedirectUri);
131+
}
109132
}

packages/rn/src/errors.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const logtoNativeClientErrorCodes = Object.freeze({
22
auth_session_failed: 'User failed to finish the authentication session.',
3+
navigation_purpose_not_supported: 'The navigation purpose is not supported.',
34
});
45

56
export type LogtoNativeClientErrorCode = keyof typeof logtoNativeClientErrorCodes;

packages/rn/src/hooks.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import { useCallback, useContext, useMemo } from 'react';
22

3+
// eslint-disable-next-line unused-imports/no-unused-imports -- use for JSDoc
4+
import type { LogtoClient } from './client';
35
import { LogtoContext } from './context';
46

7+
/**
8+
* A hook to use the Logto client instance, methods, and the authentication state. The
9+
* `LogtoContext` must be provided to make this hook work. It's recommended to use the
10+
* `LogtoProvider` component to wrap the root component of the app.
11+
*/
512
export const useLogto = () => {
613
const { client, isAuthenticated, setIsAuthenticated } = useContext(LogtoContext);
714

@@ -22,17 +29,34 @@ export const useLogto = () => {
2229

2330
const memorized = useMemo(
2431
() => ({
32+
/** The Logto client instance. */
2533
client,
34+
/**
35+
* If the user is authenticated.
36+
*
37+
* NOTE: It only checks the existence of ID token in the storage, so it's not guaranteed that the
38+
* tokens are valid.
39+
*/
2640
isAuthenticated,
41+
/** @see {@link LogtoClient.getRefreshToken} */
2742
getRefreshToken: client.getRefreshToken.bind(client),
43+
/** @see {@link LogtoClient.getAccessToken} */
2844
getAccessToken: client.getAccessToken.bind(client),
45+
/** @see {@link LogtoClient.getAccessTokenClaims} */
2946
getAccessTokenClaims: client.getAccessTokenClaims.bind(client),
47+
/** @see {@link LogtoClient.getOrganizationToken} */
3048
getOrganizationToken: client.getOrganizationToken.bind(client),
49+
/** @see {@link LogtoClient.getOrganizationTokenClaims} */
3150
getOrganizationTokenClaims: client.getOrganizationTokenClaims.bind(client),
51+
/** @see {@link LogtoClient.getIdToken} */
3252
getIdToken: client.getIdToken.bind(client),
53+
/** @see {@link LogtoClient.getIdTokenClaims} */
3354
getIdTokenClaims: client.getIdTokenClaims.bind(client),
55+
/** @see {@link LogtoClient.signIn} */
3456
signIn,
57+
/** @see {@link LogtoClient.signOut} */
3558
signOut,
59+
/** @see {@link LogtoClient.fetchUserInfo} */
3660
fetchUserInfo: client.fetchUserInfo.bind(client),
3761
}),
3862
[client, isAuthenticated, signIn, signOut]

packages/rn/src/polyfill.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/**
2+
* @fileoverview
3+
* This file is a polyfill for the `atob` and `btoa` functions. It is required for Logto to work in
4+
* Expo managed workflow (also React Native).
5+
*/
6+
17
/* eslint-disable @silverhand/fp/no-mutation, @typescript-eslint/no-unnecessary-condition */
28
import { btoaPolyfill, atobPolyfill } from 'js-base64';
39

packages/rn/src/provider.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import { LogtoClient, type LogtoNativeConfig } from './client.js';
44
import { LogtoContext } from './context.js';
55

66
export type LogtoProviderProps = {
7+
/** The configuration for the `LogtoClient`. */
78
readonly config: LogtoNativeConfig;
8-
/**
9-
* Whether to enable cache for well-known data. Use sessionStorage by default.
10-
* @default false
11-
*/
129
readonly children?: ReactNode;
1310
};
1411

12+
/**
13+
* A provider component to provide the `LogtoContext` which includes the `LogtoClient` instance
14+
* with the given configuration.
15+
*/
1516
export const LogtoProvider = ({ config, children }: LogtoProviderProps) => {
1617
const memorizedLogtoClient = useMemo(() => new LogtoClient(config), [config]);
1718
const [isAuthenticated, setIsAuthenticated] = useState(false);

0 commit comments

Comments
 (0)