Skip to content

Commit ca94430

Browse files
Merge pull request #92 from geturbackend/docs/sdk/v0.2.2
SDK Documentation Upgrade (v0.2.2 Parity)
2 parents 655f149 + 625a2c4 commit ca94430

7 files changed

Lines changed: 286 additions & 244 deletions

File tree

mintlify/docs/docs.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@
5454
"sdk/overview",
5555
"sdk/auth",
5656
"sdk/database",
57-
"sdk/storage"
57+
"sdk/storage",
58+
"sdk/schema",
59+
"sdk/mail"
5860
]
5961
},
6062
{

mintlify/docs/sdk/auth.mdx

Lines changed: 101 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,163 +1,185 @@
11
---
22
title: "Auth"
3-
description: "Create accounts, log in users, and retrieve the current session with the SDK auth module."
4-
keywords: ["auth", "authentication", "login", "signup", "sdk"]
3+
description: "Manage user accounts, sessions, and social authentication with the SDK auth module."
4+
keywords: ["auth", "authentication", "login", "signup", "sdk", "social auth", "oauth"]
55
---
66

7-
Access auth methods via `client.auth`. The module stores the session token from `login()` automatically, so you do not need to thread it through subsequent calls manually.
7+
You can access auth methods via `client.auth`. When you call `login()` or `refreshToken()`, the module stores the `accessToken` and uses it for subsequent authenticated requests such as `me()` or `updateProfile()`.
88

9-
## signUp
9+
## `signUp`
1010

1111
Create a new user account.
1212

1313
```typescript
1414
signUp(payload: SignUpPayload): Promise<AuthUser>
1515
```
1616

17-
**Parameters**
18-
19-
| Name | Type | Required | Description |
20-
|---|---|---|---|
21-
| `email` | `string` | Yes | User's email address. |
22-
| `password` | `string` | Yes | User's password. |
23-
| `name` | `string` | No | Display name. |
24-
25-
**Returns** `AuthUser`
26-
27-
```typescript
28-
interface AuthUser {
29-
_id: string;
30-
email: string;
31-
name?: string;
32-
[key: string]: unknown;
33-
}
34-
```
35-
3617
**Example**
3718

3819
```typescript
3920
const user = await client.auth.signUp({
4021
email: 'alice@example.com',
4122
password: 'secret123',
23+
username: 'alice_dev',
4224
name: 'Alice',
4325
});
44-
45-
console.log(user._id); // '507f1f77bcf86cd799439011'
4626
```
4727

4828
---
4929

50-
## login
30+
## `login`
5131

52-
Authenticate an existing user. The returned token is stored internally in the auth module and used automatically by `me()`.
32+
Authenticate an existing user. The returned `accessToken` is stored internally.
5333

5434
```typescript
5535
login(payload: LoginPayload): Promise<AuthResponse>
5636
```
5737

58-
**Parameters**
59-
60-
| Name | Type | Required | Description |
61-
|---|---|---|---|
62-
| `email` | `string` | Yes | User's email address. |
63-
| `password` | `string` | Yes | User's password. |
64-
6538
**Returns** `AuthResponse`
6639

6740
```typescript
6841
interface AuthResponse {
69-
token: string; // accessToken — store this securely
70-
user: AuthUser;
42+
accessToken?: string;
43+
token?: string; // Alias for accessToken (deprecated)
44+
expiresIn?: string;
45+
userId?: string;
46+
user?: AuthUser;
7147
}
7248
```
7349

7450
**Example**
7551

7652
```typescript
77-
const { token, user } = await client.auth.login({
53+
const { accessToken, user } = await client.auth.login({
7854
email: 'alice@example.com',
7955
password: 'secret123',
8056
});
81-
82-
console.log(token); // 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...'
83-
console.log(user.name); // 'Alice'
8457
```
8558

86-
<Note>
87-
Store `token` securely — for example in `localStorage` or an HTTP-only cookie — and pass it to authenticated API calls. Never expose it in URLs or logs.
88-
</Note>
59+
---
60+
61+
## `refreshToken`
62+
63+
Rotate the current access token.
64+
65+
- **Browser**: Call without arguments. It will automatically use the `refreshToken` stored in your HTTP-only cookies.
66+
- **Mobile/Node**: Pass the `refreshToken` string manually.
67+
68+
```typescript
69+
refreshToken(refreshToken?: string): Promise<AuthResponse>
70+
```
8971

9072
---
9173

92-
## me
74+
## `me`
9375

94-
Fetch the profile of the currently authenticated user. If you call `login()` first, you do not need to pass a token explicitly — the module uses the stored session token automatically.
76+
Fetch the profile of the currently authenticated user.
9577

9678
```typescript
9779
me(token?: string): Promise<AuthUser>
9880
```
9981

100-
**Parameters**
101-
102-
| Name | Type | Required | Description |
103-
|---|---|---|---|
104-
| `token` | `string` | No | Override the stored session token. |
82+
---
10583

106-
**Returns** `AuthUser`
84+
## `updateProfile`
10785

108-
**Example — using the stored token after login()**
86+
Update the authenticated user's profile fields.
10987

11088
```typescript
111-
await client.auth.login({ email: 'alice@example.com', password: 'secret123' });
112-
113-
const user = await client.auth.me();
114-
console.log(user.email); // 'alice@example.com'
89+
updateProfile(payload: UpdateProfilePayload, token?: string): Promise<{ message: string }>
11590
```
11691

117-
**Example — passing a token directly**
92+
**Example**
11893

11994
```typescript
120-
const token = localStorage.getItem('urbackend_token');
121-
const user = await client.auth.me(token);
95+
await client.auth.updateProfile({ name: 'Alice Smith' });
12296
```
12397

124-
<Note>
125-
Calling `me()` without a token and without a prior `login()` in the same client instance throws an `AuthError`. Always pass the token explicitly if the client is newly initialized (for example, on page reload).
126-
</Note>
98+
---
99+
100+
## `changePassword`
101+
102+
Change the authenticated user's password.
103+
104+
```typescript
105+
changePassword(payload: ChangePasswordPayload, token?: string): Promise<{ message: string }>
106+
```
127107

128108
---
129109

130-
## logout
110+
## Social auth
131111

132-
Clear the locally stored session token. This does not invalidate the token on the server.
112+
urBackend supports OAuth via GitHub and Google.
113+
114+
### `socialStart`
115+
116+
You receive a URL to initiate the OAuth flow. Redirect your user's browser to this URL.
117+
118+
```typescript
119+
socialStart(provider: 'github' | 'google'): string
120+
```
121+
122+
### `socialExchange`
123+
124+
Exchange the `rtCode` received at your callback URL for a refresh token.
133125

134126
```typescript
135-
logout(): void
127+
socialExchange(payload: SocialExchangePayload): Promise<SocialExchangeResponse>
136128
```
137129

138130
**Example**
139131

140132
```typescript
141-
client.auth.logout();
142-
// The stored session token is cleared.
143-
// Subsequent calls to me() will require a token to be passed explicitly.
133+
// At your /auth/callback page
134+
const urlParams = new URLSearchParams(window.location.search);
135+
const rtCode = urlParams.get('rtCode');
136+
const token = new URLSearchParams(window.location.hash.slice(1)).get('token');
137+
138+
if (!token || !rtCode) {
139+
throw new Error('Missing required OAuth callback parameters');
140+
}
141+
142+
const { refreshToken } = await client.auth.socialExchange({ token, rtCode });
144143
```
145144

146145
---
147146

148-
## Error handling
147+
## Account verification
148+
149+
Use these methods to handle email OTP flows.
150+
151+
| Method | Description |
152+
|---|---|
153+
| `verifyEmail(payload)` | Verify an account using the OTP sent to email. |
154+
| `resendVerificationOtp(payload)` | Request a new verification OTP. |
155+
| `requestPasswordReset(payload)` | Start the "forgot password" flow. |
156+
| `resetPassword(payload)` | Complete password reset using an OTP. |
157+
158+
---
159+
160+
## `publicProfile`
161+
162+
Fetch a public-safe profile for any user by their username. This does not return sensitive fields like email or provider IDs.
149163

150164
```typescript
151-
import urBackend, { AuthError } from '@urbackend/sdk';
165+
publicProfile(username: string): Promise<AuthUser>
166+
```
152167

153-
const client = urBackend({ apiKey: process.env.URBACKEND_API_KEY });
168+
---
154169

155-
try {
156-
await client.auth.login({ email: 'alice@example.com', password: 'wrong' });
157-
} catch (e) {
158-
if (e instanceof AuthError) {
159-
console.error('Login failed:', e.message);
160-
// e.statusCode is 401 or 403
161-
}
162-
}
170+
## `logout`
171+
172+
Call this to revoke your current session on the server and clear the local token.
173+
174+
```typescript
175+
logout(token?: string): Promise<{ success: boolean; message: string }>
163176
```
177+
178+
---
179+
180+
## Manual token management
181+
182+
If you need to manage tokens manually (for example, after social auth), you can use these helper methods:
183+
184+
- `getToken()`: Returns the current in-memory access token.
185+
- `setToken(token)`: Manually set the access token for the client.

0 commit comments

Comments
 (0)