authkit-react-native 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +37 -32
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
# AuthKit for React Native
|
|
2
2
|
|
|
3
|
-
WorkOS AuthKit integration for React Native
|
|
3
|
+
WorkOS AuthKit integration for React Native, built on Zustand and Expo modules.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
- 🔒 Secure token storage (Keychain/Keystore)
|
|
6
|
+
- 🔄 Automatic session restoration and token refresh
|
|
7
|
+
- 🔏 PKCE for added security
|
|
8
|
+
- 🧩 Zustand-based — no nested providers or layouts required
|
|
6
9
|
|
|
7
10
|
## Installation
|
|
8
11
|
|
|
@@ -10,7 +13,7 @@ For server-side auth proxy examples (Hono, Next.js), see the [`examples/`](examp
|
|
|
10
13
|
npx expo install authkit-react-native zustand expo-auth-session expo-secure-store expo-web-browser @react-native-async-storage/async-storage
|
|
11
14
|
```
|
|
12
15
|
|
|
13
|
-
##
|
|
16
|
+
## Usage
|
|
14
17
|
|
|
15
18
|
### Create the hook
|
|
16
19
|
|
|
@@ -22,9 +25,10 @@ import { createAuthStore } from "authkit-react-native";
|
|
|
22
25
|
import { useStore } from "zustand";
|
|
23
26
|
|
|
24
27
|
const authStore = createAuthStore({
|
|
25
|
-
authorizationEndpoint
|
|
26
|
-
|
|
27
|
-
|
|
28
|
+
// Provide authorizationEndpoint or clientId
|
|
29
|
+
authorizationEndpoint: "https://api.example.com/v1/auth/authorize",
|
|
30
|
+
tokenEndpoint: "https://api.example.com/v1/auth/token",
|
|
31
|
+
revocationEndpoint: "https://api.example.com/v1/auth/revoke",
|
|
28
32
|
});
|
|
29
33
|
|
|
30
34
|
export function useAuth() {
|
|
@@ -34,20 +38,32 @@ export function useAuth() {
|
|
|
34
38
|
|
|
35
39
|
### Protected routes
|
|
36
40
|
|
|
37
|
-
Because the store lives outside React, there's no provider to wrap your app in
|
|
41
|
+
Use `Stack.Protected` to gate routes based on auth state. Because the store lives outside React, there's no provider to wrap your app in — just call `useAuth()` in your root layout.
|
|
42
|
+
|
|
43
|
+
> Requires Expo SDK 53+ (Expo Router v5).
|
|
38
44
|
|
|
39
45
|
```tsx
|
|
40
|
-
// app/
|
|
46
|
+
// app/_layout.tsx
|
|
41
47
|
import { useAuth } from "@/hooks/useAuth";
|
|
42
|
-
import {
|
|
48
|
+
import { Loading } from "@/components/Loading";
|
|
49
|
+
import { Stack } from "expo-router";
|
|
43
50
|
|
|
44
|
-
export default function
|
|
51
|
+
export default function RootLayout() {
|
|
45
52
|
const { user, isLoading } = useAuth();
|
|
46
53
|
|
|
47
54
|
if (isLoading) return <Loading />;
|
|
48
|
-
if (!user) return <Redirect href="/sign-in" />;
|
|
49
55
|
|
|
50
|
-
return
|
|
56
|
+
return (
|
|
57
|
+
<Stack>
|
|
58
|
+
<Stack.Protected guard={!user}>
|
|
59
|
+
<Stack.Screen name="sign-in" />
|
|
60
|
+
</Stack.Protected>
|
|
61
|
+
|
|
62
|
+
<Stack.Protected guard={!!user}>
|
|
63
|
+
<Stack.Screen name="(app)" />
|
|
64
|
+
</Stack.Protected>
|
|
65
|
+
</Stack>
|
|
66
|
+
);
|
|
51
67
|
}
|
|
52
68
|
```
|
|
53
69
|
|
|
@@ -86,9 +102,7 @@ function SignOutButton() {
|
|
|
86
102
|
|
|
87
103
|
const handleSignOut = () => {
|
|
88
104
|
Alert.alert(
|
|
89
|
-
user
|
|
90
|
-
? `Are you sure you want to sign out as ${user.email}?`
|
|
91
|
-
: "Are you sure you want to sign out?",
|
|
105
|
+
`Are you sure you want to sign out as ${user.email}?`,
|
|
92
106
|
undefined,
|
|
93
107
|
[
|
|
94
108
|
{ text: "Cancel", style: "cancel" },
|
|
@@ -101,23 +115,6 @@ function SignOutButton() {
|
|
|
101
115
|
}
|
|
102
116
|
```
|
|
103
117
|
|
|
104
|
-
## Bare React Native (without Expo)
|
|
105
|
-
|
|
106
|
-
This package is built around Expo modules but works in any React Native app. Expo packages can be used in bare React Native projects by adding the Expo modules runtime:
|
|
107
|
-
|
|
108
|
-
1. Install the `expo` package:
|
|
109
|
-
```bash
|
|
110
|
-
npm install expo
|
|
111
|
-
npx install-expo-modules
|
|
112
|
-
```
|
|
113
|
-
2. Install the peer dependencies using `npx expo install` (handles native linking):
|
|
114
|
-
```bash
|
|
115
|
-
npx expo install expo-auth-session expo-secure-store expo-web-browser @react-native-async-storage/async-storage zustand
|
|
116
|
-
```
|
|
117
|
-
3. Ensure your app's URL scheme is configured for the OAuth redirect. In `app.json` or your native project's `Info.plist` / `AndroidManifest.xml`, register a deep link scheme (e.g., `myapp://`).
|
|
118
|
-
|
|
119
|
-
After that, usage is identical to the examples above.
|
|
120
|
-
|
|
121
118
|
## Configuration
|
|
122
119
|
|
|
123
120
|
| Option | Required | Default | Description |
|
|
@@ -129,3 +126,11 @@ After that, usage is identical to the examples above.
|
|
|
129
126
|
| `redirectUri` | No | `makeRedirectUri()` | OAuth redirect URI. |
|
|
130
127
|
| `storageKeyPrefix` | No | `"workos"` | Prefix for SecureStore/AsyncStorage keys. |
|
|
131
128
|
| `devMode` | No | `false` | Logs errors to the console when enabled. |
|
|
129
|
+
|
|
130
|
+
## Examples
|
|
131
|
+
|
|
132
|
+
See the [`examples/`](examples/) directory for complete, copy-paste-ready projects:
|
|
133
|
+
|
|
134
|
+
- **[Expo](examples/expo/)** — Minimal client app with `useAuth` hook
|
|
135
|
+
- **[Hono](examples/hono/)** — Auth proxy server on Cloudflare Workers
|
|
136
|
+
- **[Next.js](examples/nextjs/)** — Auth proxy server with App Router route handlers
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "authkit-react-native",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "WorkOS AuthKit integration for React Native — OAuth 2.0 PKCE auth with token storage, session restoration, and automatic refresh",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"zustand": ">=4"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
|
-
"@changesets/changelog-github": "^0.
|
|
57
|
+
"@changesets/changelog-github": "^0.7.0",
|
|
58
58
|
"@changesets/cli": "^2.30.0",
|
|
59
59
|
"@react-native-async-storage/async-storage": "^2.2.0",
|
|
60
60
|
"@vitest/coverage-v8": "^4.1.0",
|