|
| 1 | +## Installation |
| 2 | + |
| 3 | +```bash |
| 4 | +npm install thirdweb |
| 5 | +``` |
| 6 | + |
| 7 | +## Usage |
| 8 | + |
| 9 | +```typescript |
| 10 | +// Import everything |
| 11 | +import { Login } from "thirdweb"; |
| 12 | + |
| 13 | +// Or import specific parts |
| 14 | +import { login, createAuthHandler } from "thirdweb"; |
| 15 | +``` |
| 16 | + |
| 17 | +## Client-Side Authentication |
| 18 | + |
| 19 | +The client-side authentication system provides methods for handling user authentication in the browser. |
| 20 | + |
| 21 | +### Basic Usage |
| 22 | + |
| 23 | +```typescript |
| 24 | +import { Login } from "thirdweb/login"; |
| 25 | + |
| 26 | +// Email login |
| 27 | +const result = await Login.Client.login({ |
| 28 | + type: "email", |
| 29 | + |
| 30 | + client: thirdwebClient, |
| 31 | +}); |
| 32 | + |
| 33 | +// Handle OTP verification |
| 34 | +if (result.status === "requires_otp") { |
| 35 | + const account = await result.verifyOtp("123456"); |
| 36 | +} |
| 37 | + |
| 38 | +// Phone login |
| 39 | +const result = await Login.Client.login({ |
| 40 | + type: "phone", |
| 41 | + phoneNumber: "+1234567890", |
| 42 | + client: thirdwebClient, |
| 43 | +}); |
| 44 | + |
| 45 | +// Wallet login |
| 46 | +const result = await Login.Client.login({ |
| 47 | + type: "wallet", |
| 48 | + wallet: userWallet, |
| 49 | + chain: selectedChain, |
| 50 | + client: thirdwebClient, |
| 51 | +}); |
| 52 | + |
| 53 | +// JWT login |
| 54 | +const result = await Login.Client.login({ |
| 55 | + type: "jwt", |
| 56 | + jwt: "your-jwt-token", |
| 57 | + client: thirdwebClient, |
| 58 | +}); |
| 59 | +``` |
| 60 | + |
| 61 | +### Authentication Options |
| 62 | + |
| 63 | +```typescript |
| 64 | +type LoginOptions = { |
| 65 | + client: ThirdwebClient; |
| 66 | + options?: { |
| 67 | + sponsorGas?: boolean; // Enable gas sponsorship |
| 68 | + redirectUrl?: string; // Custom redirect URL |
| 69 | + passkeyDomain?: string; // Domain for passkey authentication |
| 70 | + storage?: AsyncStorage; // Custom storage implementation |
| 71 | + }; |
| 72 | + baseURL?: string; // Base URL for your authentication server |
| 73 | +}; |
| 74 | +``` |
| 75 | + |
| 76 | +### Authenticated Account Features |
| 77 | + |
| 78 | +Once authenticated, you get access to the following features: |
| 79 | + |
| 80 | +```typescript |
| 81 | +const account = await Login.Client.login({...}); |
| 82 | + |
| 83 | +// Get JWT token |
| 84 | +const jwt = await account.getJWT(); |
| 85 | + |
| 86 | +// Send a transaction |
| 87 | +await account.sendTransaction(transaction); |
| 88 | + |
| 89 | +// Send batch transactions |
| 90 | +await account.sendBatchTransaction([transaction1, transaction2]); |
| 91 | + |
| 92 | +// Sign messages |
| 93 | +await account.signMessage(message); |
| 94 | + |
| 95 | +// Sign typed data |
| 96 | +await account.signTypedData(typedData); |
| 97 | + |
| 98 | +// Logout |
| 99 | +await account.logout(); |
| 100 | +``` |
| 101 | + |
| 102 | +## Server-Side Authentication |
| 103 | + |
| 104 | +The server-side authentication system provides utilities for handling authentication on the server. |
| 105 | + |
| 106 | +### Basic Setup |
| 107 | + |
| 108 | +```typescript |
| 109 | +import { Login } from "thirdweb/login"; |
| 110 | + |
| 111 | +// Initialize the server-side authentication handler |
| 112 | +const authHandler = Login.Server.createAuthHandler({ |
| 113 | + // Your configuration options |
| 114 | +}); |
| 115 | +``` |
| 116 | + |
| 117 | +### Framework Integrations |
| 118 | + |
| 119 | +The package provides built-in integrations for popular frameworks: |
| 120 | + |
| 121 | +```typescript |
| 122 | +import { Login } from "thirdweb/login"; |
| 123 | + |
| 124 | +// Express.js integration |
| 125 | +app.all("/api/auth/*", Login.Server.toNodeHandler(authHandler)); |
| 126 | + |
| 127 | +// Next.js integration |
| 128 | +// /app/api/auth/[...all]/route.ts |
| 129 | +export const { GET, POST } = Login.Server.toNextJsHandler(authHandler); |
| 130 | + |
| 131 | + |
| 132 | +// Hono integration |
| 133 | +import { Hono } from "hono"; |
| 134 | +import { serve } from "@hono/node-server" |
| 135 | + |
| 136 | +const app = new Hono(); |
| 137 | + |
| 138 | +app.on(["GET", "POST"], "/api/auth/**", (c) => authHandler.handler(c.req.raw)); |
| 139 | + |
| 140 | +serve(app); |
| 141 | +``` |
| 142 | + |
| 143 | +### Required Endpoints |
| 144 | + |
| 145 | +The server implements the following endpoints: |
| 146 | + |
| 147 | +1. `/api/auth/payload` - Generate authentication payload |
| 148 | +2. `/api/auth/login` - Handle login requests |
| 149 | +3. `/api/auth/logout` - Handle logout requests |
| 150 | +4. `/api/auth/is-logged-in` - Verify authentication status |
| 151 | + |
| 152 | +(You can change the base path (`/api/auth`) by passing the `basePath` option to the `createAuthHandler` function.) |
| 153 | + |
| 154 | +### Server-Side Features |
| 155 | + |
| 156 | +- JWT validation and generation |
| 157 | +- Session management |
| 158 | +- Authentication state verification |
| 159 | +- Secure token storage |
| 160 | +- Rate limiting support |
| 161 | +- Framework integrations |
| 162 | + |
| 163 | +## Security Considerations |
| 164 | + |
| 165 | +- JWT tokens are automatically refreshed when expired |
| 166 | +- Sensitive operations require proper authentication |
| 167 | +- Session management is handled securely |
| 168 | +- Gas sponsorship can be enabled for better UX |
| 169 | + |
| 170 | +## Best Practices |
| 171 | + |
| 172 | +1. Always use HTTPS for authentication endpoints |
| 173 | +2. Implement proper error handling |
| 174 | +3. Use secure storage for sensitive data |
| 175 | +4. Implement rate limiting for authentication attempts |
| 176 | +5. Keep JWT tokens secure and handle them properly |
| 177 | + |
| 178 | +## Error Handling |
| 179 | + |
| 180 | +The login system provides detailed error messages for various scenarios: |
| 181 | +- Invalid credentials |
| 182 | +- Expired tokens |
| 183 | +- Network issues |
| 184 | +- Invalid authentication methods |
| 185 | + |
| 186 | +## TypeScript Support |
| 187 | + |
| 188 | +Full TypeScript support is included with proper type definitions for all authentication methods and responses. |
| 189 | + |
| 190 | +## API Reference |
| 191 | + |
| 192 | +### Client Exports |
| 193 | + |
| 194 | +- `login(options: LoginOptions)`: Initiates the login process |
| 195 | +- `LoginParams`: Type definition for login options |
| 196 | + |
| 197 | +### Server Exports |
| 198 | + |
| 199 | +- `createAuthHandler(options: CreateAuthHandlerOptions)`: Creates an authentication handler |
| 200 | +- `toNodeHandler(handler)`: Converts auth handler to Express.js middleware |
| 201 | +- `toNextJsHandler(handler)`: Converts auth handler to Next.js API route handler |
0 commit comments