@phila/sso-react 0.0.3 → 0.0.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.
Files changed (2) hide show
  1. package/README.md +153 -0
  2. package/package.json +2 -2
package/README.md ADDED
@@ -0,0 +1,153 @@
1
+ # @phila/sso-react
2
+
3
+ React adapter for `@phila/sso-core`. Provides context providers and hooks for Azure AD B2C authentication.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pnpm add @phila/sso-react @phila/sso-core @azure/msal-browser
9
+ ```
10
+
11
+ ## Quick Start (Vite + B2C)
12
+
13
+ The `B2CSSOProvider` reads `VITE_SSO_*` environment variables automatically:
14
+
15
+ ```env
16
+ VITE_SSO_CLIENT_ID=your-client-id
17
+ VITE_SSO_TENANT=YourTenant
18
+ VITE_SSO_AUTHORITY_DOMAIN=YourTenant.b2clogin.com
19
+ VITE_SSO_REDIRECT_URI=http://localhost:3000
20
+ ```
21
+
22
+ ```tsx
23
+ // App.tsx
24
+ import { B2CSSOProvider, useAuth } from "@phila/sso-react";
25
+
26
+ function AuthContent() {
27
+ const { isAuthenticated, userName, authReady, signIn, signOut } = useAuth();
28
+
29
+ if (!authReady) return <div>Loading...</div>;
30
+
31
+ if (isAuthenticated) {
32
+ return (
33
+ <div>
34
+ <p>Welcome, {userName}</p>
35
+ <button onClick={() => signOut()}>Sign out</button>
36
+ </div>
37
+ );
38
+ }
39
+
40
+ return <button onClick={() => signIn()}>Sign in</button>;
41
+ }
42
+
43
+ export default function App() {
44
+ return (
45
+ <B2CSSOProvider>
46
+ <AuthContent />
47
+ </B2CSSOProvider>
48
+ );
49
+ }
50
+ ```
51
+
52
+ ## Advanced Setup
53
+
54
+ For full control over the provider configuration:
55
+
56
+ ```tsx
57
+ import { SSOProvider } from "@phila/sso-react";
58
+ import { B2CProvider } from "@phila/sso-core";
59
+
60
+ function App() {
61
+ return (
62
+ <SSOProvider
63
+ config={{
64
+ provider: new B2CProvider({
65
+ clientId: "your-client-id",
66
+ b2cEnvironment: "YourTenant",
67
+ authorityDomain: "YourTenant.b2clogin.com",
68
+ redirectUri: "http://localhost:3000",
69
+ apiScopes: ["https://YourTenant.onmicrosoft.com/api/read"],
70
+ policies: {
71
+ signUpSignIn: "B2C_1A_SIGNUP_SIGNIN",
72
+ signInOnly: "B2C_1A_AD_SIGNIN_ONLY",
73
+ resetPassword: "B2C_1A_PASSWORDRESET",
74
+ },
75
+ }),
76
+ debug: true,
77
+ }}
78
+ >
79
+ <AuthContent />
80
+ </SSOProvider>
81
+ );
82
+ }
83
+ ```
84
+
85
+ ### `SSOProvider` Props
86
+
87
+ ```ts
88
+ interface SSOProviderProps {
89
+ config: SSOClientConfig;
90
+ children: ReactNode;
91
+ }
92
+ ```
93
+
94
+ ### `B2CSSOProvider` Props
95
+
96
+ ```ts
97
+ interface B2CSSOProviderProps {
98
+ children: ReactNode;
99
+ signInPolicy?: string; // default: "B2C_1A_AD_SIGNIN_ONLY"
100
+ resetPasswordPolicy?: string; // default: "B2C_1A_PASSWORDRESET"
101
+ debug?: boolean; // default: import.meta.env.DEV
102
+ }
103
+ ```
104
+
105
+ ## Hooks
106
+
107
+ ### `useAuth()`
108
+
109
+ Primary hook for authentication. Must be called within an `SSOProvider` or `B2CSSOProvider`.
110
+
111
+ **State:**
112
+
113
+ | Property | Type | Description |
114
+ | ----------------- | --------------------- | ------------------------------ |
115
+ | `isAuthenticated` | `boolean` | User is signed in |
116
+ | `isLoading` | `boolean` | Auth operation in progress |
117
+ | `user` | `AccountInfo \| null` | MSAL account info |
118
+ | `token` | `string \| null` | Current access token |
119
+ | `error` | `Error \| null` | Last auth error |
120
+ | `activePolicy` | `string \| null` | Active B2C policy |
121
+ | `authReady` | `boolean` | Initialization complete |
122
+ | `userName` | `string \| null` | Display name from token claims |
123
+
124
+ **Actions:**
125
+
126
+ | Method | Returns | Description |
127
+ | ------------------------------ | ------------------------- | -------------------------------- |
128
+ | `signIn(options?)` | `Promise<void>` | Start sign-in flow |
129
+ | `signInCityEmployee(options?)` | `Promise<void>` | Sign in with sign-in-only policy |
130
+ | `signOut(options?)` | `Promise<void>` | Sign out |
131
+ | `forgotPassword()` | `Promise<void>` | Start password reset |
132
+ | `acquireToken(options?)` | `Promise<string \| null>` | Get access token |
133
+
134
+ **Utilities:**
135
+
136
+ | Method | Returns | Description |
137
+ | --------------- | --------- | ------------------------------------------------------- |
138
+ | `hasRole(role)` | `boolean` | Check user role from `roles` or `extension_Roles` claim |
139
+
140
+ ### `useSSOClient()`
141
+
142
+ Returns the raw `SSOClient` instance for advanced use cases.
143
+
144
+ ```ts
145
+ const client = useSSOClient();
146
+ client.events.on("auth:tokenAcquired", token => {
147
+ /* ... */
148
+ });
149
+ ```
150
+
151
+ ## License
152
+
153
+ MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@phila/sso-react",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "type": "module",
5
5
  "description": "React adapter for @phila/sso-core",
6
6
  "main": "./dist/index.js",
@@ -25,7 +25,7 @@
25
25
  "author": "City of Philadelphia",
26
26
  "license": "MIT",
27
27
  "dependencies": {
28
- "@phila/sso-core": "0.0.3"
28
+ "@phila/sso-core": "0.0.4"
29
29
  },
30
30
  "peerDependencies": {
31
31
  "react": "^18.0.0 || ^19.0.0"