godgpt-web-auth 0.1.0
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 +203 -0
- package/dist/index.cjs +1089 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +376 -0
- package/dist/index.d.ts +376 -0
- package/dist/index.js +1073 -0
- package/dist/index.js.map +1 -0
- package/package.json +78 -0
package/README.md
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
# godgpt-web-auth
|
|
2
|
+
|
|
3
|
+
A React authentication package with built-in support for **Google**, **Apple**, and **Email** sign-in.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🔐 **AuthProvider** - Context provider for managing auth state
|
|
8
|
+
- 🎨 **LoginPage** - Pre-built, customizable login UI
|
|
9
|
+
- 🪝 **useAuth** - Hook for accessing auth state and actions
|
|
10
|
+
- 🔄 **Token Management** - Automatic token storage and refresh
|
|
11
|
+
- 📦 **Zero Config** - Works out of the box with sensible defaults
|
|
12
|
+
- 🔌 **Pluggable Storage** - Use localStorage, sessionStorage, or custom adapters
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install godgpt-web-auth
|
|
18
|
+
# or
|
|
19
|
+
yarn add godgpt-web-auth
|
|
20
|
+
# or
|
|
21
|
+
pnpm add godgpt-web-auth
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Quick Start
|
|
25
|
+
|
|
26
|
+
### 1. Wrap your app with AuthProvider
|
|
27
|
+
|
|
28
|
+
```tsx
|
|
29
|
+
import { AuthProvider, LoginPage, useAuth } from "godgpt-web-auth";
|
|
30
|
+
import type { AuthConfig } from "godgpt-web-auth";
|
|
31
|
+
|
|
32
|
+
const authConfig: AuthConfig = {
|
|
33
|
+
backendUrl: process.env.NEXT_PUBLIC_AUTH_BACKEND_URL,
|
|
34
|
+
appId: "your-app-id",
|
|
35
|
+
google: {
|
|
36
|
+
clientId: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID,
|
|
37
|
+
},
|
|
38
|
+
apple: {
|
|
39
|
+
clientId: process.env.NEXT_PUBLIC_APPLE_CLIENT_ID, // Service ID for web
|
|
40
|
+
redirectUri: "https://your-domain.com",
|
|
41
|
+
},
|
|
42
|
+
email: {
|
|
43
|
+
enabled: true,
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
function App() {
|
|
48
|
+
return (
|
|
49
|
+
<AuthProvider
|
|
50
|
+
config={authConfig}
|
|
51
|
+
onLogout={() => {
|
|
52
|
+
// Handle logout (e.g., redirect to home)
|
|
53
|
+
window.location.href = "/";
|
|
54
|
+
}}
|
|
55
|
+
>
|
|
56
|
+
<AppContent />
|
|
57
|
+
</AuthProvider>
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### 2. Use the LoginPage component
|
|
63
|
+
|
|
64
|
+
```tsx
|
|
65
|
+
function AppContent() {
|
|
66
|
+
const { tokens, logout, isLoading, isAuthLoaded } = useAuth();
|
|
67
|
+
|
|
68
|
+
// Show loading while checking stored tokens
|
|
69
|
+
if (!isAuthLoaded) {
|
|
70
|
+
return <div>Loading...</div>;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Show login page if not authenticated
|
|
74
|
+
if (!tokens) {
|
|
75
|
+
return <LoginPage />;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// User is authenticated
|
|
79
|
+
return (
|
|
80
|
+
<div>
|
|
81
|
+
<h1>Welcome!</h1>
|
|
82
|
+
<button onClick={logout}>Logout</button>
|
|
83
|
+
</div>
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Exports
|
|
89
|
+
|
|
90
|
+
### Main Exports
|
|
91
|
+
|
|
92
|
+
| Export | Type | Description |
|
|
93
|
+
| -------------- | --------- | ------------------------------------------------------------------ |
|
|
94
|
+
| `AuthProvider` | Component | Context provider - wrap your app with this |
|
|
95
|
+
| `LoginPage` | Component | Pre-built login UI with all providers |
|
|
96
|
+
| `useAuth` | Hook | Access auth state: `tokens`, `logout`, `isLoading`, `isAuthLoaded` |
|
|
97
|
+
|
|
98
|
+
### Types
|
|
99
|
+
|
|
100
|
+
| Type | Description |
|
|
101
|
+
| ------------------ | ------------------------------------- |
|
|
102
|
+
| `AuthConfig` | Configuration object for AuthProvider |
|
|
103
|
+
| `AuthContextValue` | Return type of useAuth hook |
|
|
104
|
+
| `JWTData` | Token data structure |
|
|
105
|
+
| `AuthResult` | Result from sign-in attempts |
|
|
106
|
+
| `AuthProviderType` | `"google" \| "apple" \| "password"` |
|
|
107
|
+
|
|
108
|
+
### Advanced Exports
|
|
109
|
+
|
|
110
|
+
For custom implementations:
|
|
111
|
+
|
|
112
|
+
```tsx
|
|
113
|
+
// Individual sign-in strategies
|
|
114
|
+
import {
|
|
115
|
+
signInWithEmail,
|
|
116
|
+
signInWithGoogle,
|
|
117
|
+
signInWithApple,
|
|
118
|
+
} from "godgpt-web-auth";
|
|
119
|
+
|
|
120
|
+
// Storage adapters
|
|
121
|
+
import {
|
|
122
|
+
storageService,
|
|
123
|
+
createStorageService,
|
|
124
|
+
localStorageAdapter,
|
|
125
|
+
memoryStorageAdapter,
|
|
126
|
+
} from "godgpt-web-auth";
|
|
127
|
+
|
|
128
|
+
// Token services
|
|
129
|
+
import { exchangeToken, refreshToken } from "godgpt-web-auth";
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## Configuration
|
|
133
|
+
|
|
134
|
+
### AuthConfig
|
|
135
|
+
|
|
136
|
+
```tsx
|
|
137
|
+
interface AuthConfig {
|
|
138
|
+
/** Backend URL for token exchange */
|
|
139
|
+
backendUrl: string;
|
|
140
|
+
|
|
141
|
+
/** App identifier sent with token requests */
|
|
142
|
+
appId?: string;
|
|
143
|
+
|
|
144
|
+
/** Google OAuth configuration */
|
|
145
|
+
google?: {
|
|
146
|
+
clientId: string;
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
/** Apple Sign In configuration */
|
|
150
|
+
apple?: {
|
|
151
|
+
clientId: string; // Service ID (not App ID) for web
|
|
152
|
+
redirectUri?: string; // Required for production (HTTPS)
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
/** Email/password configuration */
|
|
156
|
+
email?: {
|
|
157
|
+
enabled: boolean;
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## useAuth Hook
|
|
163
|
+
|
|
164
|
+
```tsx
|
|
165
|
+
const {
|
|
166
|
+
tokens, // JWTData | null - Current tokens
|
|
167
|
+
logout, // () => Promise<void> - Clear tokens and call onLogout
|
|
168
|
+
isLoading, // boolean - Sign-in in progress
|
|
169
|
+
isAuthLoaded, // boolean - Initial token check complete
|
|
170
|
+
} = useAuth();
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
## OAuth Provider Setup
|
|
174
|
+
|
|
175
|
+
### Google
|
|
176
|
+
|
|
177
|
+
1. Go to [Google Cloud Console](https://console.cloud.google.com/apis/credentials)
|
|
178
|
+
2. Create OAuth 2.0 Client ID (Web application)
|
|
179
|
+
3. Add your domain to **Authorized JavaScript origins**
|
|
180
|
+
4. Add your domain to **Authorized redirect URIs**
|
|
181
|
+
|
|
182
|
+
### Apple
|
|
183
|
+
|
|
184
|
+
1. Go to [Apple Developer Portal](https://developer.apple.com/account/resources/identifiers/list/serviceId)
|
|
185
|
+
2. Create a **Service ID** (not App ID) for web
|
|
186
|
+
3. Enable "Sign In with Apple"
|
|
187
|
+
4. Configure domains and return URLs
|
|
188
|
+
5. **Note**: Apple requires HTTPS for redirect URIs
|
|
189
|
+
|
|
190
|
+
## Token Storage
|
|
191
|
+
|
|
192
|
+
By default, tokens are stored in `localStorage`. For custom storage:
|
|
193
|
+
|
|
194
|
+
```tsx
|
|
195
|
+
import { createStorageService, memoryStorageAdapter } from "godgpt-web-auth";
|
|
196
|
+
|
|
197
|
+
// Use memory storage (for SSR or testing)
|
|
198
|
+
const customStorage = createStorageService(memoryStorageAdapter);
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
## License
|
|
202
|
+
|
|
203
|
+
MIT
|