Skip to content

Commit 4cd42a4

Browse files
Merge pull request #60 from InsForge/auth-fix
Auth SDK update for OSS v2.0.2
2 parents 9cfcb94 + fad8004 commit 4cd42a4

5 files changed

Lines changed: 164 additions & 103 deletions

File tree

README.md

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ const insforge = createClient({
4646
const { data, error } = await insforge.auth.signUp({
4747
email: 'user@example.com',
4848
password: 'securePassword123',
49-
name: 'John Doe' // optional
49+
name: 'John Doe', // optional
50+
redirectTo: 'http://localhost:3000/sign-in' // optional, recommended for link-based verification
5051
});
5152

5253
// Sign in with email/password
@@ -61,8 +62,8 @@ await insforge.auth.signInWithOAuth({
6162
redirectTo: 'http://localhost:3000/dashboard'
6263
});
6364

64-
// Get current user
65-
const { data: user } = await insforge.auth.getCurrentUser();
65+
// Get current user (call this during browser app startup)
66+
const { data: currentUser } = await insforge.auth.getCurrentUser();
6667

6768
// Get any user's profile by ID (public endpoint)
6869
const { data: profile, error } = await insforge.auth.getProfile('user-id-here');
@@ -78,6 +79,51 @@ const { data: updatedProfile, error } = await insforge.auth.setProfile({
7879
await insforge.auth.signOut();
7980
```
8081

82+
### Email Verification And Password Reset
83+
84+
```javascript
85+
// Resend a verification email
86+
await insforge.auth.resendVerificationEmail({
87+
email: 'user@example.com',
88+
redirectTo: 'http://localhost:3000/sign-in' // optional, recommended for link-based verification
89+
});
90+
91+
// Verify email with a 6-digit code
92+
await insforge.auth.verifyEmail({
93+
email: 'user@example.com',
94+
otp: '123456'
95+
});
96+
97+
// Send password reset email
98+
await insforge.auth.sendResetPasswordEmail({
99+
email: 'user@example.com',
100+
redirectTo: 'http://localhost:3000/reset-password' // optional, recommended for link-based reset
101+
});
102+
103+
// Code-based reset flow: exchange the code, then reset the password
104+
const { data: resetToken } = await insforge.auth.exchangeResetPasswordToken({
105+
email: 'user@example.com',
106+
code: '123456'
107+
});
108+
109+
if (resetToken) {
110+
await insforge.auth.resetPassword({
111+
newPassword: 'newSecurePassword123',
112+
otp: resetToken.token
113+
});
114+
}
115+
```
116+
117+
For link-based verification and password reset, users click the emailed browser links:
118+
- `GET /api/auth/email/verify-link`
119+
- `GET /api/auth/email/reset-password-link`
120+
121+
Those backend endpoints validate the token first, then redirect the browser to your `redirectTo` URL.
122+
123+
- Verification links redirect with `insforge_status=success|error`, `insforge_type=verify_email`, and optional `insforge_error`
124+
- Recommended: use your sign-in page as the verification `redirectTo`, then show a confirmation message and ask the user to sign in with email and password
125+
- Reset links redirect with `token` when ready, plus `insforge_status=ready|error`, `insforge_type=reset_password`, and optional `insforge_error`
126+
81127
### Database Operations
82128

83129
```javascript
@@ -201,14 +247,14 @@ const insforge = createClient({
201247
The SDK is written in TypeScript and provides full type definitions:
202248

203249
```typescript
204-
import { createClient, InsForgeClient, User, Session } from '@insforge/sdk';
250+
import { createClient, InsForgeClient } from '@insforge/sdk';
205251

206252
const insforge: InsForgeClient = createClient({
207253
baseUrl: 'http://localhost:7130'
208254
});
209255

210256
// Type-safe API calls
211-
const response: { data: User | null; error: Error | null } =
257+
const response =
212258
await insforge.auth.getCurrentUser();
213259
```
214260

SDK-REFERENCE.md

Lines changed: 85 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ The SDK automatically detects and handles OAuth callback parameters when initial
4040

4141
**How it works:**
4242
1. User calls `signInWithOAuth()` and is redirected to OAuth provider
43-
2. After authentication, provider redirects back to your app with tokens in URL
44-
3. SDK automatically detects these parameters on initialization
45-
4. Session is saved and URL is cleaned - no manual handling needed!
43+
2. After authentication, InsForge redirects back to your app with an `insforge_code` in the URL
44+
3. SDK automatically exchanges that code for a session on initialization
45+
4. Session is saved and the URL is cleaned - no manual handling needed
4646

4747
**Example:**
4848
```javascript
@@ -52,11 +52,12 @@ const insforge = createClient({
5252
});
5353

5454
// If the URL contains OAuth callback parameters like:
55-
// ?access_token=xxx&user_id=yyy&email=user@example.com&name=John
55+
// ?insforge_code=...
5656
// The SDK will:
57+
// - Exchange the code for a session
5758
// - Save the session in memory
5859
// - Set the auth token for API calls
59-
// - Clean the URL (remove sensitive parameters)
60+
// - Clean the URL
6061

6162
// You can then immediately use authenticated methods:
6263
const { data } = await insforge.auth.getCurrentUser();
@@ -69,13 +70,23 @@ const { data } = await insforge.auth.getCurrentUser();
6970
await insforge.auth.signUp({
7071
email: 'user@example.com',
7172
password: 'password123',
72-
name: 'John Doe' // optional
73+
name: 'John Doe', // optional
74+
redirectTo: 'http://localhost:3000/sign-in' // optional, recommended for link-based verification
7375
})
7476
// Response: { data: { user, accessToken }, error }
7577
// user: { id, email, name, emailVerified, createdAt, updatedAt }
7678
// accessToken: JWT token string
7779
```
7880

81+
If the backend uses link-based email verification, the emailed link opens:
82+
83+
```text
84+
GET /api/auth/email/verify-link?token=...
85+
```
86+
87+
InsForge validates the token first, then redirects the browser to your `redirectTo` URL.
88+
Recommended: use your sign-in page as `redirectTo`, then show a success message and ask the user to sign in with email and password.
89+
7990
### `signInWithPassword()`
8091
```javascript
8192
await insforge.auth.signInWithPassword({
@@ -99,9 +110,10 @@ await insforge.auth.signInWithOAuth({
99110

100111
// AUTOMATIC OAuth Callback Detection (v0.0.14+):
101112
// When users are redirected back from OAuth provider, the SDK automatically:
102-
// 1. Detects OAuth parameters (access_token, user_id, email, name) in URL
103-
// 2. Saves the session in memory
104-
// 3. Cleans the URL of sensitive parameters
113+
// 1. Detects insforge_code in the URL
114+
// 2. Exchanges the code for a session
115+
// 3. Saves the session in memory
116+
// 4. Cleans the URL
105117
// No manual handling needed - just initialize the client!
106118
```
107119

@@ -115,12 +127,15 @@ await insforge.auth.signOut()
115127
### `getCurrentUser()`
116128
```javascript
117129
await insforge.auth.getCurrentUser()
118-
// Response: { data: { user, profile }, error }
119-
// user: { id, email, role } // Auth info from API
120-
// profile: { id, nickname, avatar_url, bio, birthday, ... } // Profile from users table
130+
// Response: { data: { user }, error }
131+
// user: { id, email, emailVerified, providers, createdAt, updatedAt, profile, metadata }
121132
// Returns null if not authenticated
122133
```
123134

135+
For browser apps, call `getCurrentUser()` during startup. The SDK will use the httpOnly refresh cookie automatically when it can refresh the session.
136+
137+
For `isServerMode: true`, call `refreshSession({ refreshToken })` explicitly when you need to refresh an expired access token.
138+
124139
### `getProfile()`
125140
```javascript
126141
await insforge.auth.getProfile(userId)
@@ -149,6 +164,64 @@ await insforge.auth.getPublicAuthConfig()
149164
// This is a public endpoint that doesn't require authentication
150165
```
151166

167+
### `resendVerificationEmail()`
168+
```javascript
169+
await insforge.auth.resendVerificationEmail({
170+
email: 'user@example.com',
171+
redirectTo: 'http://localhost:3000/sign-in' // optional, recommended for link-based verification
172+
})
173+
// Response: { data: { success, message }, error }
174+
```
175+
176+
### `verifyEmail()`
177+
```javascript
178+
await insforge.auth.verifyEmail({
179+
email: 'user@example.com',
180+
otp: '123456'
181+
})
182+
// Response: { data: { user, accessToken, csrfToken?, refreshToken? }, error }
183+
// POST /api/auth/email/verify is code-only
184+
// Browser link verification uses GET /api/auth/email/verify-link
185+
// Verification redirect params:
186+
// - insforge_status=success|error
187+
// - insforge_type=verify_email
188+
// - insforge_error (only on error)
189+
```
190+
191+
### `sendResetPasswordEmail()`
192+
```javascript
193+
await insforge.auth.sendResetPasswordEmail({
194+
email: 'user@example.com',
195+
redirectTo: 'http://localhost:3000/reset-password' // optional, recommended for link-based reset
196+
})
197+
// Response: { data: { success, message }, error }
198+
```
199+
200+
### `exchangeResetPasswordToken()`
201+
```javascript
202+
await insforge.auth.exchangeResetPasswordToken({
203+
email: 'user@example.com',
204+
code: '123456'
205+
})
206+
// Response: { data: { token, expiresAt }, error }
207+
```
208+
209+
### `resetPassword()`
210+
```javascript
211+
await insforge.auth.resetPassword({
212+
newPassword: 'newSecurePassword123',
213+
otp: 'reset-token'
214+
})
215+
// Response: { data: { message }, error }
216+
// Browser reset links use GET /api/auth/email/reset-password-link first,
217+
// then your app submits the new password with POST /api/auth/email/reset-password.
218+
// Reset redirect params:
219+
// - token (present only when ready)
220+
// - insforge_status=ready|error
221+
// - insforge_type=reset_password
222+
// - insforge_error (only on error)
223+
```
224+
152225
## Error Handling
153226

154227
### Auth/Storage/AI Errors (InsForgeError)

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@insforge/sdk",
3-
"version": "1.2.0",
3+
"version": "1.2.2",
44
"description": "Official JavaScript/TypeScript client for InsForge Backend-as-a-Service platform",
55
"main": "./dist/index.js",
66
"module": "./dist/index.mjs",
@@ -50,7 +50,7 @@
5050
"url": "https://github.com/InsForge/insforge-sdk-js/issues"
5151
},
5252
"dependencies": {
53-
"@insforge/shared-schemas": "^1.1.44",
53+
"@insforge/shared-schemas": "^1.1.46",
5454
"@supabase/postgrest-js": "^1.21.3",
5555
"socket.io-client": "^4.8.1"
5656
},

0 commit comments

Comments
 (0)