@companyio/auth-react 0.1.2 → 0.1.4

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 ADDED
@@ -0,0 +1,24 @@
1
+ # @companyio/auth-react
2
+
3
+ React context for sharing an `AuthClient`, restoring sessions on startup, and reading the current user.
4
+
5
+ ## Use
6
+
7
+ ```tsx
8
+ import { AuthProvider, useAuth } from '@companyio/auth-react';
9
+
10
+ <AuthProvider client={authClient}>
11
+ <App />
12
+ </AuthProvider>;
13
+
14
+ function Account() {
15
+ const { user, isLoading, signOut } = useAuth();
16
+ return isLoading ? <span>Loading...</span> : <button onClick={signOut}>{user?.email}</button>;
17
+ }
18
+ ```
19
+
20
+ ## Build
21
+
22
+ ```bash
23
+ pnpm --filter @companyio/auth-react build
24
+ ```
package/package.json CHANGED
@@ -1,21 +1,34 @@
1
1
  {
2
2
  "name": "@companyio/auth-react",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "React provider and hooks for the shared authentication platform",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist",
16
+ "README.md"
17
+ ],
8
18
  "dependencies": {
9
- "react": "^18.2.0",
10
- "@companyio/auth-contracts": "0.1.2",
11
- "@companyio/auth-client": "0.1.2"
19
+ "@companyio/auth-client": "0.1.4",
20
+ "@companyio/auth-contracts": "0.1.4"
21
+ },
22
+ "peerDependencies": {
23
+ "react": "^18.2.0 || ^19.0.0"
12
24
  },
13
25
  "devDependencies": {
14
26
  "@types/react": "^18.2.0",
27
+ "react": "^18.2.0",
15
28
  "typescript": "^5.3.3"
16
29
  },
17
30
  "scripts": {
18
31
  "build": "tsc",
19
- "test": "exit 0"
32
+ "test": "tsc --noEmit"
20
33
  }
21
34
  }
package/src/index.tsx DELETED
@@ -1,42 +0,0 @@
1
- import { createContext, PropsWithChildren, useContext, useEffect, useState } from 'react';
2
- import { AuthClient } from '@companyio/auth-client';
3
- import { AuthSession, User } from '@companyio/auth-contracts';
4
-
5
- type AuthContextValue = {
6
- client: AuthClient;
7
- session: AuthSession | null;
8
- user: User | null;
9
- isLoading: boolean;
10
- signIn: (provider?: string) => Promise<void>;
11
- signOut: () => Promise<void>;
12
- };
13
-
14
- const AuthContext = createContext<AuthContextValue | null>(null);
15
-
16
- export const AuthProvider = ({ client, children }: PropsWithChildren<{ client: AuthClient }>) => {
17
- const [session, setSession] = useState<AuthSession | null>(client.session);
18
- const [isLoading, setIsLoading] = useState(true);
19
-
20
- useEffect(() => {
21
- const unsubscribe = client.subscribe(setSession);
22
- void client.restore().finally(() => setIsLoading(false));
23
- return unsubscribe;
24
- }, [client]);
25
-
26
- const value: AuthContextValue = {
27
- client,
28
- session,
29
- user: session?.user ?? null,
30
- isLoading,
31
- signIn: (provider) => client.signIn(provider),
32
- signOut: () => client.signOut(),
33
- };
34
-
35
- return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
36
- };
37
-
38
- export const useAuth = (): AuthContextValue => {
39
- const context = useContext(AuthContext);
40
- if (!context) throw new Error('useAuth must be used inside an AuthProvider.');
41
- return context;
42
- };
package/tsconfig.json DELETED
@@ -1,12 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.json",
3
- "compilerOptions": {
4
- "outDir": "./dist",
5
- "rootDir": "./src",
6
- "jsx": "react-jsx",
7
- "declaration": true,
8
- "declarationMap": true
9
- },
10
- "include": ["src"],
11
- "exclude": ["node_modules", "dist"]
12
- }