|
1 | 1 | --- |
2 | 2 | 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"] |
5 | 5 | --- |
6 | 6 |
|
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()`. |
8 | 8 |
|
9 | | -## signUp |
| 9 | +## `signUp` |
10 | 10 |
|
11 | 11 | Create a new user account. |
12 | 12 |
|
13 | 13 | ```typescript |
14 | 14 | signUp(payload: SignUpPayload): Promise<AuthUser> |
15 | 15 | ``` |
16 | 16 |
|
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 | | - |
36 | 17 | **Example** |
37 | 18 |
|
38 | 19 | ```typescript |
39 | 20 | const user = await client.auth.signUp({ |
40 | 21 | email: 'alice@example.com', |
41 | 22 | password: 'secret123', |
| 23 | + username: 'alice_dev', |
42 | 24 | name: 'Alice', |
43 | 25 | }); |
44 | | - |
45 | | -console.log(user._id); // '507f1f77bcf86cd799439011' |
46 | 26 | ``` |
47 | 27 |
|
48 | 28 | --- |
49 | 29 |
|
50 | | -## login |
| 30 | +## `login` |
51 | 31 |
|
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. |
53 | 33 |
|
54 | 34 | ```typescript |
55 | 35 | login(payload: LoginPayload): Promise<AuthResponse> |
56 | 36 | ``` |
57 | 37 |
|
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 | | - |
65 | 38 | **Returns** `AuthResponse` |
66 | 39 |
|
67 | 40 | ```typescript |
68 | 41 | 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; |
71 | 47 | } |
72 | 48 | ``` |
73 | 49 |
|
74 | 50 | **Example** |
75 | 51 |
|
76 | 52 | ```typescript |
77 | | -const { token, user } = await client.auth.login({ |
| 53 | +const { accessToken, user } = await client.auth.login({ |
78 | 54 | email: 'alice@example.com', |
79 | 55 | password: 'secret123', |
80 | 56 | }); |
81 | | - |
82 | | -console.log(token); // 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...' |
83 | | -console.log(user.name); // 'Alice' |
84 | 57 | ``` |
85 | 58 |
|
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 | +``` |
89 | 71 |
|
90 | 72 | --- |
91 | 73 |
|
92 | | -## me |
| 74 | +## `me` |
93 | 75 |
|
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. |
95 | 77 |
|
96 | 78 | ```typescript |
97 | 79 | me(token?: string): Promise<AuthUser> |
98 | 80 | ``` |
99 | 81 |
|
100 | | -**Parameters** |
101 | | - |
102 | | -| Name | Type | Required | Description | |
103 | | -|---|---|---|---| |
104 | | -| `token` | `string` | No | Override the stored session token. | |
| 82 | +--- |
105 | 83 |
|
106 | | -**Returns** `AuthUser` |
| 84 | +## `updateProfile` |
107 | 85 |
|
108 | | -**Example — using the stored token after login()** |
| 86 | +Update the authenticated user's profile fields. |
109 | 87 |
|
110 | 88 | ```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 }> |
115 | 90 | ``` |
116 | 91 |
|
117 | | -**Example — passing a token directly** |
| 92 | +**Example** |
118 | 93 |
|
119 | 94 | ```typescript |
120 | | -const token = localStorage.getItem('urbackend_token'); |
121 | | -const user = await client.auth.me(token); |
| 95 | +await client.auth.updateProfile({ name: 'Alice Smith' }); |
122 | 96 | ``` |
123 | 97 |
|
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 | +``` |
127 | 107 |
|
128 | 108 | --- |
129 | 109 |
|
130 | | -## logout |
| 110 | +## Social auth |
131 | 111 |
|
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. |
133 | 125 |
|
134 | 126 | ```typescript |
135 | | -logout(): void |
| 127 | +socialExchange(payload: SocialExchangePayload): Promise<SocialExchangeResponse> |
136 | 128 | ``` |
137 | 129 |
|
138 | 130 | **Example** |
139 | 131 |
|
140 | 132 | ```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 }); |
144 | 143 | ``` |
145 | 144 |
|
146 | 145 | --- |
147 | 146 |
|
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. |
149 | 163 |
|
150 | 164 | ```typescript |
151 | | -import urBackend, { AuthError } from '@urbackend/sdk'; |
| 165 | +publicProfile(username: string): Promise<AuthUser> |
| 166 | +``` |
152 | 167 |
|
153 | | -const client = urBackend({ apiKey: process.env.URBACKEND_API_KEY }); |
| 168 | +--- |
154 | 169 |
|
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 }> |
163 | 176 | ``` |
| 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