@atls/react-native-kratos 0.0.1 → 1.0.1
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/CHANGELOG.md +1 -7
- package/README.md +169 -0
- package/dist/flows/index.d.ts +2 -2
- package/dist/flows/index.js +2 -18
- package/dist/flows/react-native-login.flow.d.ts +2 -1
- package/dist/flows/react-native-login.flow.js +6 -17
- package/dist/flows/react-native-registration.flow.d.ts +2 -1
- package/dist/flows/react-native-registration.flow.js +6 -13
- package/dist/hooks/index.d.ts +1 -1
- package/dist/hooks/index.js +1 -17
- package/dist/hooks/use-auth.hook.d.ts +1 -1
- package/dist/hooks/use-auth.hook.js +3 -13
- package/dist/index.d.ts +6 -3
- package/dist/index.js +4 -20
- package/dist/providers/auth-session.store.d.ts +30 -0
- package/dist/providers/auth-session.store.js +318 -0
- package/dist/providers/auth.provider.d.ts +11 -9
- package/dist/providers/auth.provider.js +33 -83
- package/dist/providers/index.d.ts +4 -1
- package/dist/providers/index.js +2 -17
- package/dist/providers/platform-auth.provider.d.ts +6 -0
- package/dist/providers/platform-auth.provider.js +4 -0
- package/dist/providers/platform-session-token.storage.d.ts +1 -0
- package/dist/providers/platform-session-token.storage.js +3 -0
- package/dist/providers/session-token.storage.d.ts +17 -0
- package/dist/providers/session-token.storage.js +33 -0
- package/package.json +39 -18
package/CHANGELOG.md
CHANGED
|
@@ -1,11 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
1
|
## 0.0.1 (2025-01-22)
|
|
4
2
|
|
|
5
|
-
|
|
6
3
|
### Features
|
|
7
4
|
|
|
8
|
-
|
|
9
|
-
* **ory:** init ([#2](https://github.com/atls/react-native/issues/2)) ([aca4a51](https://github.com/atls/react-native/commit/aca4a5185ff3c36b16ec830c3952e22f1f9925dc))
|
|
10
|
-
|
|
11
|
-
|
|
5
|
+
- **ory:** init ([#2](https://github.com/atls/react-native/issues/2)) ([aca4a51](https://github.com/atls/react-native/commit/aca4a5185ff3c36b16ec830c3952e22f1f9925dc))
|
package/README.md
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
# @atls/react-native-kratos
|
|
2
|
+
|
|
3
|
+
Username/password authentication and native session management for Expo 56,
|
|
4
|
+
React Native 0.85 and React 19. The package connects the shared
|
|
5
|
+
`@atls/react-kratos` flows to Expo SecureStore; your application owns its screens,
|
|
6
|
+
navigation and account/profile initialization.
|
|
7
|
+
|
|
8
|
+
## Install and configure
|
|
9
|
+
|
|
10
|
+
Install the adapter and Ory client, then install the native storage module with
|
|
11
|
+
your application's Expo CLI:
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
yarn add @atls/react-native-kratos @ory/kratos-client-fetch@26.2.0
|
|
15
|
+
npx expo install expo-secure-store
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Use a Kratos Public API endpoint reachable from the device, with password login
|
|
19
|
+
and registration enabled. Configure its identity schema for the traits your
|
|
20
|
+
registration screen collects. Never embed Kratos admin credentials in the app.
|
|
21
|
+
|
|
22
|
+
Create one SDK client and mount the providers above your authentication screens:
|
|
23
|
+
|
|
24
|
+
```tsx
|
|
25
|
+
import { Configuration } from '@ory/kratos-client-fetch'
|
|
26
|
+
import { FrontendApi } from '@ory/kratos-client-fetch'
|
|
27
|
+
|
|
28
|
+
import { AuthProvider } from '@atls/react-native-kratos'
|
|
29
|
+
import { SdkProvider } from '@atls/react-native-kratos'
|
|
30
|
+
|
|
31
|
+
const frontend = new FrontendApi(new Configuration({ basePath: kratosPublicUrl }))
|
|
32
|
+
|
|
33
|
+
export const App = () => (
|
|
34
|
+
<SdkProvider value={frontend}>
|
|
35
|
+
<AuthProvider>
|
|
36
|
+
<AuthenticationScreen />
|
|
37
|
+
</AuthProvider>
|
|
38
|
+
</SdkProvider>
|
|
39
|
+
)
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Keep the SDK instance stable across renders. `AuthProvider` waits for the initial
|
|
43
|
+
storage read before rendering its children. A credential awaiting server
|
|
44
|
+
validation is not an authenticated session: use `isAuthenticated` to choose the
|
|
45
|
+
signed-in screen, not the presence of `sessionToken`.
|
|
46
|
+
|
|
47
|
+
## Password login and registration
|
|
48
|
+
|
|
49
|
+
Wrap a login screen with `ReactNativeLoginFlow`. Its required `route` accepts
|
|
50
|
+
optional `params.aal` and `params.refresh`; use `route={{}}` for ordinary login.
|
|
51
|
+
Shared flow components provide values, submission state and Kratos validation
|
|
52
|
+
messages while you supply native UI:
|
|
53
|
+
|
|
54
|
+
```tsx
|
|
55
|
+
import { Button } from 'react-native'
|
|
56
|
+
import { TextInput } from 'react-native'
|
|
57
|
+
import { View } from 'react-native'
|
|
58
|
+
|
|
59
|
+
import { FlowInputNode } from '@atls/react-native-kratos'
|
|
60
|
+
import { FlowSubmit } from '@atls/react-native-kratos'
|
|
61
|
+
import { ReactNativeLoginFlow } from '@atls/react-native-kratos'
|
|
62
|
+
|
|
63
|
+
export const LoginScreen = () => (
|
|
64
|
+
<ReactNativeLoginFlow route={{}} onError={reportAuthError}>
|
|
65
|
+
<View>
|
|
66
|
+
<FlowInputNode name='identifier'>
|
|
67
|
+
{(node, value, onChange) => (
|
|
68
|
+
<TextInput autoCapitalize='none' value={value} onChangeText={onChange} />
|
|
69
|
+
)}
|
|
70
|
+
</FlowInputNode>
|
|
71
|
+
<FlowInputNode name='password'>
|
|
72
|
+
{(node, value, onChange) => (
|
|
73
|
+
<TextInput secureTextEntry value={value} onChangeText={onChange} />
|
|
74
|
+
)}
|
|
75
|
+
</FlowInputNode>
|
|
76
|
+
<FlowSubmit>
|
|
77
|
+
{({ onSubmit, submitting }) => (
|
|
78
|
+
<Button
|
|
79
|
+
title='Sign in'
|
|
80
|
+
disabled={submitting}
|
|
81
|
+
onPress={() => {
|
|
82
|
+
onSubmit({ method: 'password' }).catch(reportAuthError)
|
|
83
|
+
}}
|
|
84
|
+
/>
|
|
85
|
+
)}
|
|
86
|
+
</FlowSubmit>
|
|
87
|
+
</View>
|
|
88
|
+
</ReactNativeLoginFlow>
|
|
89
|
+
)
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Use `ReactNativeRegistrationFlow` for registration. Provide the fields required
|
|
93
|
+
by your identity schema and submit nested `traits`, rather than flat field names
|
|
94
|
+
such as `traits.username`. For a username schema, a child of the registration
|
|
95
|
+
flow can read `useValues()` and submit:
|
|
96
|
+
|
|
97
|
+
```tsx
|
|
98
|
+
onSubmit({
|
|
99
|
+
method: 'password',
|
|
100
|
+
traits: { username: values.getValue('traits.username') },
|
|
101
|
+
}).catch(reportAuthError)
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
The wrappers pass a successful native session to the auth provider and await
|
|
105
|
+
credential persistence. `useFlow()`, `FlowMessages` and `FlowNodeMessages` remain
|
|
106
|
+
available from the shared package for displaying flow and field validation
|
|
107
|
+
messages. No browser callback or `returnTo` is required for these password flows.
|
|
108
|
+
|
|
109
|
+
## Session API
|
|
110
|
+
|
|
111
|
+
Read the public state and actions with `useAuth()`. `AuthContext` is also exported
|
|
112
|
+
for consumers that use React's context API directly.
|
|
113
|
+
|
|
114
|
+
| Member | Application behavior |
|
|
115
|
+
| --------------------------------------- | ---------------------------------------------------------------------------------- |
|
|
116
|
+
| `isAuthenticated` | True only when the current confirmed session is active |
|
|
117
|
+
| `session` | Current server session, when available |
|
|
118
|
+
| `sessionToken` | Current known opaque credential; its presence alone is not proof of authentication |
|
|
119
|
+
| `error` | Storage or session validation failure reported by the provider |
|
|
120
|
+
| `setSession({ session, sessionToken })` | Persist and accept a native flow result; the wrappers normally call this for you |
|
|
121
|
+
| `setSession(undefined)` | Clear local authentication and storage without revoking a server session |
|
|
122
|
+
| `syncSession(): Promise<void>` | Wait for pending acceptance and revalidate the current credential |
|
|
123
|
+
| `logout(): Promise<void>` | Clear local authentication, remove its stored credential and attempt server logout |
|
|
124
|
+
|
|
125
|
+
Await actions and handle rejection. For example, a logout button can report an
|
|
126
|
+
incomplete logout and allow the user to retry:
|
|
127
|
+
|
|
128
|
+
```tsx
|
|
129
|
+
const { logout } = useAuth()
|
|
130
|
+
|
|
131
|
+
const signOut = async () => {
|
|
132
|
+
try {
|
|
133
|
+
await logout()
|
|
134
|
+
} catch (error) {
|
|
135
|
+
reportAuthError(error)
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
Explicit logout also covers a credential already read during pending restoration.
|
|
141
|
+
If remote logout fails after local clearing, another `logout()` call on the same
|
|
142
|
+
mounted provider retries it. Both local and remote failures are reported, using
|
|
143
|
+
an `AggregateError` if both fail. Retry before replacing or unmounting that
|
|
144
|
+
provider; remote retry state is not persisted across process restarts.
|
|
145
|
+
|
|
146
|
+
## Storage and recovery
|
|
147
|
+
|
|
148
|
+
SecureStore holds the raw opaque token under `user_session`. Older native JSON
|
|
149
|
+
values are read at the same key and migrated only after successful server
|
|
150
|
+
validation; stored session data is never trusted as authentication.
|
|
151
|
+
|
|
152
|
+
A confirmed inactive-session response clears the affected credential. Temporary
|
|
153
|
+
network or server failures retain the token and any previously confirmed session,
|
|
154
|
+
expose the error, and allow `syncSession()` to retry. A failed replacement write
|
|
155
|
+
does not replace the last committed credential. Late asynchronous results cannot
|
|
156
|
+
reauthenticate after logout or overwrite a newer accepted session. Switching
|
|
157
|
+
accounts does not automatically revoke another server session.
|
|
158
|
+
|
|
159
|
+
## Supported runtime
|
|
160
|
+
|
|
161
|
+
The native integration targets Expo 56, React Native 0.85 and React 19 with
|
|
162
|
+
`expo-secure-store` 56 and `@ory/kratos-client-fetch` 26.2. The client version does
|
|
163
|
+
not select or upgrade your Kratos server; verify your server's password and
|
|
164
|
+
session behavior separately.
|
|
165
|
+
|
|
166
|
+
Expo 50, web storage, browser/OIDC authentication and redirect-code exchange are
|
|
167
|
+
not supported by this native adapter. Shared `@atls/react-kratos@0.1.0` exports,
|
|
168
|
+
including `SdkProvider` and `useSdk`, remain available; their presence does not
|
|
169
|
+
add native support for browser-only flows.
|
package/dist/flows/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './react-native-login.flow';
|
|
2
|
-
export * from './react-native-registration.flow';
|
|
1
|
+
export * from './react-native-login.flow.js';
|
|
2
|
+
export * from './react-native-registration.flow.js';
|
package/dist/flows/index.js
CHANGED
|
@@ -1,18 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./react-native-login.flow"), exports);
|
|
18
|
-
__exportStar(require("./react-native-registration.flow"), exports);
|
|
1
|
+
export * from './react-native-login.flow.js';
|
|
2
|
+
export * from './react-native-registration.flow.js';
|
|
@@ -8,5 +8,6 @@ export interface ReactNativeLoginFlowProps {
|
|
|
8
8
|
};
|
|
9
9
|
};
|
|
10
10
|
children: ReactNode;
|
|
11
|
+
onError?: (error: unknown) => void;
|
|
11
12
|
}
|
|
12
|
-
export declare const ReactNativeLoginFlow: ({ children, route, }: ReactNativeLoginFlowProps) => ReactElement;
|
|
13
|
+
export declare const ReactNativeLoginFlow: ({ children, onError, route, }: ReactNativeLoginFlowProps) => ReactElement;
|
|
@@ -1,18 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { LoginNativeFlow } from '@atls/react-kratos';
|
|
3
|
+
import { useAuth } from '../hooks/index.js';
|
|
4
|
+
export const ReactNativeLoginFlow = ({ children, onError, route, }) => {
|
|
5
|
+
const { sessionToken, setSession } = useAuth();
|
|
6
|
+
return (_jsx(LoginNativeFlow, { aal: route.params?.aal, refresh: route.params?.refresh, sessionToken: sessionToken, onError: onError, onGenericError: onError, onSession: setSession, children: children }));
|
|
4
7
|
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.ReactNativeLoginFlow = void 0;
|
|
7
|
-
const react_kratos_1 = require("@atls/react-kratos");
|
|
8
|
-
const expo_auth_session_1 = require("expo-auth-session");
|
|
9
|
-
const react_1 = __importDefault(require("react"));
|
|
10
|
-
const hooks_1 = require("../hooks");
|
|
11
|
-
const ReactNativeLoginFlow = ({ children, route, }) => {
|
|
12
|
-
const { sessionToken, setSession } = (0, hooks_1.useAuth)();
|
|
13
|
-
return (react_1.default.createElement(react_kratos_1.LoginNativeFlow, { aal: route.params?.aal, refresh: route.params?.refresh, sessionToken: sessionToken, returnTo: (0, expo_auth_session_1.makeRedirectUri)({
|
|
14
|
-
preferLocalhost: true,
|
|
15
|
-
path: '/Callback',
|
|
16
|
-
}), onSession: setSession }, children));
|
|
17
|
-
};
|
|
18
|
-
exports.ReactNativeLoginFlow = ReactNativeLoginFlow;
|
|
@@ -2,5 +2,6 @@ import type { ReactNode } from 'react';
|
|
|
2
2
|
import type { ReactElement } from 'react';
|
|
3
3
|
export interface ReactNativeRegistrationFlowProps {
|
|
4
4
|
children: ReactNode;
|
|
5
|
+
onError?: (error: unknown) => void;
|
|
5
6
|
}
|
|
6
|
-
export declare const ReactNativeRegistrationFlow: ({ children, }: ReactNativeRegistrationFlowProps) => ReactElement;
|
|
7
|
+
export declare const ReactNativeRegistrationFlow: ({ children, onError, }: ReactNativeRegistrationFlowProps) => ReactElement;
|
|
@@ -1,14 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { RegistrationNativeFlow } from '@atls/react-kratos';
|
|
3
|
+
import { useAuth } from '../hooks/index.js';
|
|
4
|
+
export const ReactNativeRegistrationFlow = ({ children, onError, }) => {
|
|
5
|
+
const { setSession } = useAuth();
|
|
6
|
+
return (_jsx(RegistrationNativeFlow, { onError: onError, onGenericError: onError, onSession: setSession, children: children }));
|
|
4
7
|
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.ReactNativeRegistrationFlow = void 0;
|
|
7
|
-
const react_kratos_1 = require("@atls/react-kratos");
|
|
8
|
-
const react_1 = __importDefault(require("react"));
|
|
9
|
-
const hooks_1 = require("../hooks");
|
|
10
|
-
const ReactNativeRegistrationFlow = ({ children, }) => {
|
|
11
|
-
const { setSession } = (0, hooks_1.useAuth)();
|
|
12
|
-
return react_1.default.createElement(react_kratos_1.RegistrationNativeFlow, { onSession: setSession }, children);
|
|
13
|
-
};
|
|
14
|
-
exports.ReactNativeRegistrationFlow = ReactNativeRegistrationFlow;
|
package/dist/hooks/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from './use-auth.hook';
|
|
1
|
+
export * from './use-auth.hook.js';
|
package/dist/hooks/index.js
CHANGED
|
@@ -1,17 +1 @@
|
|
|
1
|
-
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./use-auth.hook"), exports);
|
|
1
|
+
export * from './use-auth.hook.js';
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { ContextAuth } from '../providers';
|
|
1
|
+
import type { ContextAuth } from '../providers/auth.provider.js';
|
|
2
2
|
export declare const useAuth: () => ContextAuth;
|
|
@@ -1,13 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const react_1 = require("react");
|
|
5
|
-
const providers_1 = require("../providers");
|
|
6
|
-
const useAuth = () => {
|
|
7
|
-
const auth = (0, react_1.useContext)(providers_1.AuthContext);
|
|
8
|
-
if (!auth) {
|
|
9
|
-
throw new Error('Missing <AuthProvider>');
|
|
10
|
-
}
|
|
11
|
-
return auth;
|
|
12
|
-
};
|
|
13
|
-
exports.useAuth = useAuth;
|
|
1
|
+
import { useContext } from 'react';
|
|
2
|
+
import { AuthContext } from '../providers/auth.provider.js';
|
|
3
|
+
export const useAuth = () => useContext(AuthContext);
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
export * from '@atls/react-kratos';
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
export
|
|
2
|
+
export { AuthContext, AuthProvider } from './providers/index.js';
|
|
3
|
+
export { ReactNativeLoginFlow, ReactNativeRegistrationFlow } from './flows/index.js';
|
|
4
|
+
export { useAuth } from './hooks/index.js';
|
|
5
|
+
export type { ContextAuth, SessionContext } from './providers/index.js';
|
|
6
|
+
export type { ReactNativeLoginFlowProps } from './flows/index.js';
|
|
7
|
+
export type { ReactNativeRegistrationFlowProps } from './flows/index.js';
|
package/dist/index.js
CHANGED
|
@@ -1,20 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("@atls/react-kratos"), exports);
|
|
18
|
-
__exportStar(require("./providers"), exports);
|
|
19
|
-
__exportStar(require("./flows"), exports);
|
|
20
|
-
__exportStar(require("./hooks"), exports);
|
|
1
|
+
export * from '@atls/react-kratos';
|
|
2
|
+
export { AuthContext, AuthProvider } from './providers/index.js';
|
|
3
|
+
export { ReactNativeLoginFlow, ReactNativeRegistrationFlow } from './flows/index.js';
|
|
4
|
+
export { useAuth } from './hooks/index.js';
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { FrontendApi } from '@ory/kratos-client-fetch';
|
|
2
|
+
import type { Session } from '@ory/kratos-client-fetch';
|
|
3
|
+
import type { SessionTokenStorage } from './session-token.storage.js';
|
|
4
|
+
export interface NativeSession {
|
|
5
|
+
session: Session;
|
|
6
|
+
sessionToken?: string;
|
|
7
|
+
}
|
|
8
|
+
export interface AuthSessionSnapshot {
|
|
9
|
+
error?: unknown;
|
|
10
|
+
generation: number;
|
|
11
|
+
initialized: boolean;
|
|
12
|
+
session?: Session;
|
|
13
|
+
sessionToken?: string;
|
|
14
|
+
}
|
|
15
|
+
export interface AuthSessionStore {
|
|
16
|
+
acceptSession: (session: NativeSession, expectedGeneration: number) => Promise<void>;
|
|
17
|
+
clearSession: (expectedGeneration: number) => Promise<void>;
|
|
18
|
+
getSnapshot: () => AuthSessionSnapshot;
|
|
19
|
+
initialize: () => Promise<void>;
|
|
20
|
+
logout: () => Promise<void>;
|
|
21
|
+
subscribe: (listener: () => void) => () => void;
|
|
22
|
+
syncSession: () => Promise<void>;
|
|
23
|
+
}
|
|
24
|
+
export type AuthSessionSdk = Pick<FrontendApi, 'performNativeLogout' | 'toSession'>;
|
|
25
|
+
interface AuthSessionStoreOptions {
|
|
26
|
+
sdk: AuthSessionSdk;
|
|
27
|
+
storage: SessionTokenStorage;
|
|
28
|
+
}
|
|
29
|
+
export declare const createAuthSessionStore: ({ sdk, storage, }: AuthSessionStoreOptions) => AuthSessionStore;
|
|
30
|
+
export {};
|
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
import { ResponseError } from '@ory/kratos-client-fetch';
|
|
2
|
+
const isInactiveSessionError = (error) => error instanceof ResponseError && error.response.status === 401;
|
|
3
|
+
export const createAuthSessionStore = ({ sdk, storage, }) => {
|
|
4
|
+
const listeners = new Set();
|
|
5
|
+
const failedLogoutTokens = new Set();
|
|
6
|
+
let pendingAcceptance;
|
|
7
|
+
const pendingAcceptanceTokens = new Map();
|
|
8
|
+
let persistedCredential;
|
|
9
|
+
let snapshot = {
|
|
10
|
+
generation: 0,
|
|
11
|
+
initialized: false,
|
|
12
|
+
};
|
|
13
|
+
let storageQueue = Promise.resolve();
|
|
14
|
+
const emit = (nextSnapshot) => {
|
|
15
|
+
snapshot = nextSnapshot;
|
|
16
|
+
for (const listener of listeners) {
|
|
17
|
+
listener();
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
const persistedSnapshot = (generation, error) => ({
|
|
21
|
+
...persistedCredential,
|
|
22
|
+
...(error === undefined ? {} : { error }),
|
|
23
|
+
generation,
|
|
24
|
+
initialized: true,
|
|
25
|
+
});
|
|
26
|
+
const enqueueStorageOperation = async (operation) => {
|
|
27
|
+
const result = storageQueue.then(operation, operation);
|
|
28
|
+
storageQueue = result.then(() => undefined, () => undefined);
|
|
29
|
+
return result;
|
|
30
|
+
};
|
|
31
|
+
const clearInactiveSession = async (sessionToken, expectedGeneration) => {
|
|
32
|
+
if (snapshot.generation !== expectedGeneration ||
|
|
33
|
+
(snapshot.sessionToken && snapshot.sessionToken !== sessionToken)) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const clearedGeneration = expectedGeneration + 1;
|
|
37
|
+
emit({
|
|
38
|
+
generation: clearedGeneration,
|
|
39
|
+
initialized: true,
|
|
40
|
+
});
|
|
41
|
+
try {
|
|
42
|
+
await enqueueStorageOperation(async () => {
|
|
43
|
+
const storedSession = await storage.read();
|
|
44
|
+
if (storedSession?.sessionToken === sessionToken) {
|
|
45
|
+
await storage.delete();
|
|
46
|
+
if (persistedCredential?.sessionToken === sessionToken) {
|
|
47
|
+
persistedCredential = undefined;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
if (snapshot.generation === clearedGeneration) {
|
|
54
|
+
emit({
|
|
55
|
+
...snapshot,
|
|
56
|
+
error,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
throw error;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
const validateSession = async (sessionToken, expectedGeneration) => {
|
|
63
|
+
try {
|
|
64
|
+
const session = await sdk.toSession({ xSessionToken: sessionToken });
|
|
65
|
+
if (snapshot.generation !== expectedGeneration ||
|
|
66
|
+
(snapshot.sessionToken && snapshot.sessionToken !== sessionToken)) {
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
if (persistedCredential?.sessionToken === sessionToken) {
|
|
70
|
+
persistedCredential = {
|
|
71
|
+
session,
|
|
72
|
+
sessionToken,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
emit({
|
|
76
|
+
generation: expectedGeneration,
|
|
77
|
+
initialized: true,
|
|
78
|
+
session,
|
|
79
|
+
sessionToken,
|
|
80
|
+
});
|
|
81
|
+
return session;
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
if (snapshot.generation !== expectedGeneration ||
|
|
85
|
+
(snapshot.sessionToken && snapshot.sessionToken !== sessionToken)) {
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
if (isInactiveSessionError(error)) {
|
|
89
|
+
await clearInactiveSession(sessionToken, expectedGeneration);
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
emit({
|
|
93
|
+
...snapshot,
|
|
94
|
+
error,
|
|
95
|
+
initialized: true,
|
|
96
|
+
sessionToken,
|
|
97
|
+
});
|
|
98
|
+
throw error;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
const restoreStoredSession = async (propagateError) => {
|
|
102
|
+
const expectedGeneration = snapshot.generation;
|
|
103
|
+
let storedSession;
|
|
104
|
+
try {
|
|
105
|
+
storedSession = await enqueueStorageOperation(async () => storage.read());
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
if (snapshot.generation === expectedGeneration) {
|
|
109
|
+
emit({
|
|
110
|
+
...snapshot,
|
|
111
|
+
error,
|
|
112
|
+
initialized: true,
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
if (propagateError) {
|
|
116
|
+
throw error;
|
|
117
|
+
}
|
|
118
|
+
return undefined;
|
|
119
|
+
}
|
|
120
|
+
if (snapshot.generation !== expectedGeneration) {
|
|
121
|
+
return undefined;
|
|
122
|
+
}
|
|
123
|
+
if (!storedSession) {
|
|
124
|
+
persistedCredential = undefined;
|
|
125
|
+
emit({
|
|
126
|
+
...snapshot,
|
|
127
|
+
error: undefined,
|
|
128
|
+
initialized: true,
|
|
129
|
+
});
|
|
130
|
+
return undefined;
|
|
131
|
+
}
|
|
132
|
+
persistedCredential = {
|
|
133
|
+
...(persistedCredential?.sessionToken === storedSession.sessionToken &&
|
|
134
|
+
persistedCredential.session
|
|
135
|
+
? { session: persistedCredential.session }
|
|
136
|
+
: {}),
|
|
137
|
+
sessionToken: storedSession.sessionToken,
|
|
138
|
+
};
|
|
139
|
+
emit(persistedSnapshot(expectedGeneration));
|
|
140
|
+
let session;
|
|
141
|
+
try {
|
|
142
|
+
session = await validateSession(storedSession.sessionToken, expectedGeneration);
|
|
143
|
+
}
|
|
144
|
+
catch (error) {
|
|
145
|
+
if (propagateError) {
|
|
146
|
+
throw error;
|
|
147
|
+
}
|
|
148
|
+
return undefined;
|
|
149
|
+
}
|
|
150
|
+
if (!session ||
|
|
151
|
+
!storedSession.requiresMigration ||
|
|
152
|
+
snapshot.generation !== expectedGeneration ||
|
|
153
|
+
snapshot.sessionToken !== storedSession.sessionToken) {
|
|
154
|
+
return session;
|
|
155
|
+
}
|
|
156
|
+
try {
|
|
157
|
+
await enqueueStorageOperation(async () => storage.write(storedSession.sessionToken, session));
|
|
158
|
+
persistedCredential = {
|
|
159
|
+
session,
|
|
160
|
+
sessionToken: storedSession.sessionToken,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
catch (error) {
|
|
164
|
+
if (snapshot.generation === expectedGeneration &&
|
|
165
|
+
snapshot.sessionToken === storedSession.sessionToken) {
|
|
166
|
+
emit({
|
|
167
|
+
...snapshot,
|
|
168
|
+
error,
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
if (propagateError) {
|
|
172
|
+
throw error;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return session;
|
|
176
|
+
};
|
|
177
|
+
const synchronizeSession = async () => {
|
|
178
|
+
const acceptance = pendingAcceptance;
|
|
179
|
+
if (acceptance) {
|
|
180
|
+
await acceptance;
|
|
181
|
+
return synchronizeSession();
|
|
182
|
+
}
|
|
183
|
+
const { generation, sessionToken } = snapshot;
|
|
184
|
+
if (sessionToken) {
|
|
185
|
+
await validateSession(sessionToken, generation);
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
await restoreStoredSession(true);
|
|
189
|
+
}
|
|
190
|
+
if (pendingAcceptance || snapshot.generation !== generation) {
|
|
191
|
+
return synchronizeSession();
|
|
192
|
+
}
|
|
193
|
+
return undefined;
|
|
194
|
+
};
|
|
195
|
+
return {
|
|
196
|
+
acceptSession: async ({ session, sessionToken }, expectedGeneration) => {
|
|
197
|
+
if (!sessionToken) {
|
|
198
|
+
throw new Error('Missing session token');
|
|
199
|
+
}
|
|
200
|
+
if (snapshot.generation !== expectedGeneration) {
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
const previousSnapshot = snapshot;
|
|
204
|
+
const reservedGeneration = expectedGeneration + 1;
|
|
205
|
+
emit({
|
|
206
|
+
...previousSnapshot,
|
|
207
|
+
generation: reservedGeneration,
|
|
208
|
+
});
|
|
209
|
+
const acceptance = enqueueStorageOperation(async () => storage.write(sessionToken, session)).then(() => {
|
|
210
|
+
persistedCredential = {
|
|
211
|
+
session,
|
|
212
|
+
sessionToken,
|
|
213
|
+
};
|
|
214
|
+
if (snapshot.generation === reservedGeneration && pendingAcceptance === acceptance) {
|
|
215
|
+
emit({
|
|
216
|
+
generation: reservedGeneration,
|
|
217
|
+
initialized: true,
|
|
218
|
+
session,
|
|
219
|
+
sessionToken,
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
}, (error) => {
|
|
223
|
+
if (snapshot.generation === reservedGeneration && pendingAcceptance === acceptance) {
|
|
224
|
+
emit(persistedSnapshot(reservedGeneration, error));
|
|
225
|
+
}
|
|
226
|
+
throw error;
|
|
227
|
+
});
|
|
228
|
+
pendingAcceptance = acceptance;
|
|
229
|
+
pendingAcceptanceTokens.set(acceptance, sessionToken);
|
|
230
|
+
try {
|
|
231
|
+
await acceptance;
|
|
232
|
+
}
|
|
233
|
+
finally {
|
|
234
|
+
pendingAcceptanceTokens.delete(acceptance);
|
|
235
|
+
if (pendingAcceptance === acceptance) {
|
|
236
|
+
pendingAcceptance = undefined;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
},
|
|
240
|
+
clearSession: async (expectedGeneration) => {
|
|
241
|
+
if (snapshot.generation !== expectedGeneration) {
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
const clearedGeneration = expectedGeneration + 1;
|
|
245
|
+
emit({
|
|
246
|
+
generation: clearedGeneration,
|
|
247
|
+
initialized: true,
|
|
248
|
+
});
|
|
249
|
+
try {
|
|
250
|
+
await enqueueStorageOperation(async () => {
|
|
251
|
+
await storage.delete();
|
|
252
|
+
persistedCredential = undefined;
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
catch (error) {
|
|
256
|
+
if (snapshot.generation === clearedGeneration) {
|
|
257
|
+
emit({
|
|
258
|
+
...snapshot,
|
|
259
|
+
error,
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
throw error;
|
|
263
|
+
}
|
|
264
|
+
},
|
|
265
|
+
getSnapshot: () => snapshot,
|
|
266
|
+
initialize: async () => {
|
|
267
|
+
await restoreStoredSession(false);
|
|
268
|
+
},
|
|
269
|
+
logout: async () => {
|
|
270
|
+
const sessionTokens = new Set();
|
|
271
|
+
if (snapshot.sessionToken)
|
|
272
|
+
sessionTokens.add(snapshot.sessionToken);
|
|
273
|
+
if (persistedCredential?.sessionToken)
|
|
274
|
+
sessionTokens.add(persistedCredential.sessionToken);
|
|
275
|
+
for (const token of pendingAcceptanceTokens.values())
|
|
276
|
+
sessionTokens.add(token);
|
|
277
|
+
for (const token of failedLogoutTokens)
|
|
278
|
+
sessionTokens.add(token);
|
|
279
|
+
emit({
|
|
280
|
+
generation: snapshot.generation + 1,
|
|
281
|
+
initialized: true,
|
|
282
|
+
});
|
|
283
|
+
const localOperation = enqueueStorageOperation(async () => {
|
|
284
|
+
await storage.delete();
|
|
285
|
+
persistedCredential = undefined;
|
|
286
|
+
});
|
|
287
|
+
const remoteOperations = [...sessionTokens].map(async (sessionToken) => {
|
|
288
|
+
try {
|
|
289
|
+
await sdk.performNativeLogout({
|
|
290
|
+
performNativeLogoutBody: {
|
|
291
|
+
session_token: sessionToken,
|
|
292
|
+
},
|
|
293
|
+
});
|
|
294
|
+
failedLogoutTokens.delete(sessionToken);
|
|
295
|
+
}
|
|
296
|
+
catch (error) {
|
|
297
|
+
failedLogoutTokens.add(sessionToken);
|
|
298
|
+
throw error;
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
const results = await Promise.allSettled([localOperation, ...remoteOperations]);
|
|
302
|
+
const errors = results.flatMap((result) => result.status === 'rejected' ? [result.reason] : []);
|
|
303
|
+
if (errors.length === 1) {
|
|
304
|
+
throw errors[0];
|
|
305
|
+
}
|
|
306
|
+
if (errors.length > 1) {
|
|
307
|
+
throw new AggregateError(errors, 'Logout failed');
|
|
308
|
+
}
|
|
309
|
+
},
|
|
310
|
+
subscribe: (listener) => {
|
|
311
|
+
listeners.add(listener);
|
|
312
|
+
return () => {
|
|
313
|
+
listeners.delete(listener);
|
|
314
|
+
};
|
|
315
|
+
},
|
|
316
|
+
syncSession: synchronizeSession,
|
|
317
|
+
};
|
|
318
|
+
};
|
|
@@ -1,20 +1,22 @@
|
|
|
1
|
-
import type { Session } from '@
|
|
1
|
+
import type { Session } from '@ory/kratos-client-fetch';
|
|
2
2
|
import type { ReactElement } from 'react';
|
|
3
3
|
import type { ReactNode } from 'react';
|
|
4
|
+
import type { NativeSession } from './auth-session.store.js';
|
|
5
|
+
import type { SessionTokenStorage } from './session-token.storage.js';
|
|
4
6
|
import React from 'react';
|
|
5
|
-
export type SessionContext = {
|
|
6
|
-
sessionToken?: string;
|
|
7
|
-
session: Session;
|
|
8
|
-
} | undefined;
|
|
9
7
|
export interface ContextAuth {
|
|
8
|
+
error?: unknown;
|
|
9
|
+
isAuthenticated: boolean;
|
|
10
|
+
logout: () => Promise<void>;
|
|
10
11
|
session?: Session;
|
|
11
12
|
sessionToken?: string;
|
|
12
|
-
isAuthenticated: boolean;
|
|
13
13
|
setSession: (session: SessionContext) => Promise<void>;
|
|
14
|
+
syncSession: () => Promise<void>;
|
|
14
15
|
}
|
|
16
|
+
export type SessionContext = NativeSession | undefined;
|
|
15
17
|
export declare const AuthContext: React.Context<ContextAuth>;
|
|
16
|
-
interface AuthProviderProps {
|
|
18
|
+
export interface AuthProviderProps {
|
|
17
19
|
children: ReactNode;
|
|
20
|
+
storage: SessionTokenStorage;
|
|
18
21
|
}
|
|
19
|
-
export declare const AuthProvider: ({ children }: AuthProviderProps) => ReactElement | null;
|
|
20
|
-
export {};
|
|
22
|
+
export declare const AuthProvider: ({ children, storage }: AuthProviderProps) => ReactElement | null;
|
|
@@ -1,87 +1,37 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
-
};
|
|
28
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.AuthProvider = exports.AuthContext = void 0;
|
|
30
|
-
const react_kratos_1 = require("@atls/react-kratos");
|
|
31
|
-
const async_storage_1 = __importDefault(require("@react-native-async-storage/async-storage"));
|
|
32
|
-
const SecureStore = __importStar(require("expo-secure-store"));
|
|
33
|
-
const react_native_1 = require("react-native");
|
|
34
|
-
const react_1 = require("react");
|
|
35
|
-
const react_2 = require("react");
|
|
36
|
-
const react_3 = require("react");
|
|
37
|
-
const react_4 = require("react");
|
|
38
|
-
const react_5 = __importDefault(require("react"));
|
|
39
|
-
const USER_SESSION_NAME = 'user_session';
|
|
40
|
-
exports.AuthContext = (0, react_1.createContext)({
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useSdk } from '@atls/react-kratos';
|
|
3
|
+
import { createContext } from 'react';
|
|
4
|
+
import { useEffect } from 'react';
|
|
5
|
+
import { useMemo } from 'react';
|
|
6
|
+
import { useSyncExternalStore } from 'react';
|
|
7
|
+
import { createAuthSessionStore } from './auth-session.store.js';
|
|
8
|
+
const defaultAuth = {
|
|
41
9
|
isAuthenticated: false,
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
setSessionContext({ session, sessionToken: auth.sessionToken });
|
|
67
|
-
}
|
|
68
|
-
catch (error) {
|
|
69
|
-
console.log(error);
|
|
70
|
-
setSessionContext(undefined);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
setInitialized(true);
|
|
74
|
-
};
|
|
75
|
-
getAuthenticatedSession();
|
|
76
|
-
}, [sdk]);
|
|
77
|
-
if (!initialized) {
|
|
10
|
+
logout: async () => undefined,
|
|
11
|
+
setSession: async () => undefined,
|
|
12
|
+
syncSession: async () => undefined,
|
|
13
|
+
};
|
|
14
|
+
export const AuthContext = createContext(defaultAuth);
|
|
15
|
+
export const AuthProvider = ({ children, storage }) => {
|
|
16
|
+
const sdk = useSdk();
|
|
17
|
+
const store = useMemo(() => createAuthSessionStore({ sdk, storage }), [sdk, storage]);
|
|
18
|
+
const snapshot = useSyncExternalStore(store.subscribe, store.getSnapshot, store.getSnapshot);
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
store.initialize().catch(() => undefined);
|
|
21
|
+
}, [store]);
|
|
22
|
+
const value = useMemo(() => ({
|
|
23
|
+
error: snapshot.error,
|
|
24
|
+
isAuthenticated: snapshot.session?.active === true,
|
|
25
|
+
logout: async () => store.logout(),
|
|
26
|
+
session: snapshot.session,
|
|
27
|
+
sessionToken: snapshot.sessionToken,
|
|
28
|
+
setSession: async (session) => session
|
|
29
|
+
? store.acceptSession(session, snapshot.generation)
|
|
30
|
+
: store.clearSession(snapshot.generation),
|
|
31
|
+
syncSession: store.syncSession,
|
|
32
|
+
}), [snapshot, store]);
|
|
33
|
+
if (!snapshot.initialized) {
|
|
78
34
|
return null;
|
|
79
35
|
}
|
|
80
|
-
return (
|
|
81
|
-
session: sessionContext?.session,
|
|
82
|
-
sessionToken: sessionContext?.sessionToken,
|
|
83
|
-
isAuthenticated: Boolean(sessionContext?.sessionToken),
|
|
84
|
-
setSession,
|
|
85
|
-
} }, children));
|
|
36
|
+
return _jsx(AuthContext.Provider, { value: value, children: children });
|
|
86
37
|
};
|
|
87
|
-
exports.AuthProvider = AuthProvider;
|
|
@@ -1 +1,4 @@
|
|
|
1
|
-
export
|
|
1
|
+
export { AuthContext } from './auth.provider.js';
|
|
2
|
+
export { PlatformAuthProvider as AuthProvider } from './platform-auth.provider.js';
|
|
3
|
+
export type { ContextAuth, SessionContext } from './auth.provider.js';
|
|
4
|
+
export type { PlatformAuthProviderProps as AuthProviderProps } from './platform-auth.provider.js';
|
package/dist/providers/index.js
CHANGED
|
@@ -1,17 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./auth.provider"), exports);
|
|
1
|
+
export { AuthContext } from './auth.provider.js';
|
|
2
|
+
export { PlatformAuthProvider as AuthProvider } from './platform-auth.provider.js';
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { AuthProvider } from './auth.provider.js';
|
|
3
|
+
import { sessionTokenStorage } from './platform-session-token.storage.js';
|
|
4
|
+
export const PlatformAuthProvider = ({ children }) => (_jsx(AuthProvider, { storage: sessionTokenStorage, children: children }));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const sessionTokenStorage: import("./session-token.storage.js").SessionTokenStorage;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Session } from '@ory/kratos-client-fetch';
|
|
2
|
+
interface SecureStoreAdapter {
|
|
3
|
+
deleteItemAsync: (key: string) => Promise<void>;
|
|
4
|
+
getItemAsync: (key: string) => Promise<string | null>;
|
|
5
|
+
setItemAsync: (key: string, value: string) => Promise<void>;
|
|
6
|
+
}
|
|
7
|
+
export interface SessionTokenStorage {
|
|
8
|
+
delete: () => Promise<void>;
|
|
9
|
+
read: () => Promise<StoredSessionToken | undefined>;
|
|
10
|
+
write: (sessionToken: string, session: Session) => Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
export interface StoredSessionToken {
|
|
13
|
+
requiresMigration: boolean;
|
|
14
|
+
sessionToken: string;
|
|
15
|
+
}
|
|
16
|
+
export declare const createSessionTokenStorage: (secureStore: SecureStoreAdapter) => SessionTokenStorage;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
const SESSION_KEY = 'user_session';
|
|
2
|
+
const parseStoredSessionToken = (value) => {
|
|
3
|
+
if (!value) {
|
|
4
|
+
return undefined;
|
|
5
|
+
}
|
|
6
|
+
try {
|
|
7
|
+
const storedSession = JSON.parse(value);
|
|
8
|
+
if (typeof storedSession === 'object' && storedSession !== null) {
|
|
9
|
+
if ('sessionToken' in storedSession && typeof storedSession.sessionToken === 'string') {
|
|
10
|
+
return {
|
|
11
|
+
requiresMigration: true,
|
|
12
|
+
sessionToken: storedSession.sessionToken,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
return undefined;
|
|
16
|
+
}
|
|
17
|
+
return {
|
|
18
|
+
requiresMigration: false,
|
|
19
|
+
sessionToken: value,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return {
|
|
24
|
+
requiresMigration: false,
|
|
25
|
+
sessionToken: value,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
export const createSessionTokenStorage = (secureStore) => ({
|
|
30
|
+
delete: async () => secureStore.deleteItemAsync(SESSION_KEY),
|
|
31
|
+
read: async () => parseStoredSessionToken(await secureStore.getItemAsync(SESSION_KEY)),
|
|
32
|
+
write: async (sessionToken) => secureStore.setItemAsync(SESSION_KEY, sessionToken),
|
|
33
|
+
});
|
package/package.json
CHANGED
|
@@ -1,8 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atls/react-native-kratos",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"license": "BSD-3-Clause",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
"./package.json": "./package.json",
|
|
8
|
+
".": {
|
|
9
|
+
"import": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
5
14
|
"main": "dist/index.js",
|
|
15
|
+
"typings": "dist/index.d.ts",
|
|
6
16
|
"files": [
|
|
7
17
|
"dist"
|
|
8
18
|
],
|
|
@@ -12,30 +22,41 @@
|
|
|
12
22
|
"postpack": "rm -rf dist"
|
|
13
23
|
},
|
|
14
24
|
"dependencies": {
|
|
15
|
-
"@atls/react-kratos": "0.0
|
|
25
|
+
"@atls/react-kratos": "0.1.0"
|
|
16
26
|
},
|
|
17
27
|
"devDependencies": {
|
|
18
|
-
"@
|
|
19
|
-
"@
|
|
20
|
-
"@
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
"react": "
|
|
24
|
-
"
|
|
25
|
-
"
|
|
28
|
+
"@ory/kratos-client-fetch": "26.2.0",
|
|
29
|
+
"@testing-library/dom": "10.4.1",
|
|
30
|
+
"@testing-library/react": "16.1.0",
|
|
31
|
+
"@types/node": "^22.10.1",
|
|
32
|
+
"@types/react": "19.2.14",
|
|
33
|
+
"@types/react-dom": "19.2.3",
|
|
34
|
+
"expo": "56.0.9",
|
|
35
|
+
"expo-secure-store": "56.0.4",
|
|
36
|
+
"global-jsdom": "25.0.0",
|
|
37
|
+
"jsdom": "25.0.1",
|
|
38
|
+
"react": "19.2.3",
|
|
39
|
+
"react-dom": "19.2.3",
|
|
40
|
+
"react-native": "0.85.3"
|
|
26
41
|
},
|
|
27
42
|
"peerDependencies": {
|
|
28
|
-
"@
|
|
29
|
-
"expo
|
|
30
|
-
"expo-secure-store": "^
|
|
31
|
-
"react": "^
|
|
32
|
-
"react-native": "^0.
|
|
43
|
+
"@ory/kratos-client-fetch": "^26.2.0",
|
|
44
|
+
"expo": "^56.0.0",
|
|
45
|
+
"expo-secure-store": "^56.0.4",
|
|
46
|
+
"react": "^19.2.3",
|
|
47
|
+
"react-native": "^0.85.3"
|
|
33
48
|
},
|
|
34
49
|
"publishConfig": {
|
|
35
50
|
"access": "public",
|
|
51
|
+
"exports": {
|
|
52
|
+
"./package.json": "./package.json",
|
|
53
|
+
".": {
|
|
54
|
+
"import": "./dist/index.js",
|
|
55
|
+
"types": "./dist/index.d.ts",
|
|
56
|
+
"default": "./dist/index.js"
|
|
57
|
+
}
|
|
58
|
+
},
|
|
36
59
|
"main": "dist/index.js",
|
|
37
60
|
"typings": "dist/index.d.ts"
|
|
38
|
-
}
|
|
39
|
-
"typecheckSkipLibCheck": true,
|
|
40
|
-
"typings": "dist/index.d.ts"
|
|
61
|
+
}
|
|
41
62
|
}
|