|
| 1 | +# my-auth-package |
| 2 | + |
| 3 | +A flexible and decoupled authentication package for React applications. Provides ready-to-use authentication components, context, and API utilities. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- 🔒 Complete authentication flow (login, register, password reset, email verification) |
| 8 | +- 🔌 Fully configurable endpoints and base URL |
| 9 | +- 🎨 Customizable UI components |
| 10 | +- 📦 TypeScript support |
| 11 | +- 🛠️ Various token storage options (localStorage, sessionStorage, etc.) |
| 12 | +- 🔄 Token refresh handling |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +```bash |
| 17 | +npm install my-auth-package |
| 18 | +# or |
| 19 | +yarn add my-auth-package |
| 20 | +``` |
| 21 | + |
| 22 | +## Quick Start |
| 23 | + |
| 24 | +1. Configure the authentication service: |
| 25 | + |
| 26 | +```jsx |
| 27 | +import { configureAuth } from 'my-auth-package'; |
| 28 | + |
| 29 | +configureAuth({ |
| 30 | + baseUrl: 'https://api.myapp.com', |
| 31 | + tokenStorage: 'localStorage', |
| 32 | + // Optionally override default endpoints |
| 33 | + endpoints: { |
| 34 | + login: '/custom/login', |
| 35 | + // ... |
| 36 | + }, |
| 37 | +}); |
| 38 | +``` |
| 39 | + |
| 40 | +2. Wrap your application with the `AuthProvider`: |
| 41 | + |
| 42 | +```jsx |
| 43 | +import { AuthProvider } from 'my-auth-package'; |
| 44 | + |
| 45 | +function App() { |
| 46 | + return ( |
| 47 | + <AuthProvider |
| 48 | + onLoginSuccess={user => console.log('Logged in:', user)} |
| 49 | + onLogoutSuccess={() => console.log('Logged out')} |
| 50 | + > |
| 51 | + <YourApplication /> |
| 52 | + </AuthProvider> |
| 53 | + ); |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +3. Use the authentication components in your app: |
| 58 | + |
| 59 | +```jsx |
| 60 | +import { LoginForm, RegisterForm, PasswordReset } from 'my-auth-package'; |
| 61 | + |
| 62 | +function AuthPage() { |
| 63 | + const [view, setView] = useState('login'); |
| 64 | + |
| 65 | + return ( |
| 66 | + <div> |
| 67 | + {view === 'login' && ( |
| 68 | + <LoginForm |
| 69 | + onSuccess={() => console.log('Login successful')} |
| 70 | + onError={error => console.error('Login failed:', error)} |
| 71 | + /> |
| 72 | + )} |
| 73 | + |
| 74 | + {view === 'register' && ( |
| 75 | + <RegisterForm |
| 76 | + onSuccess={() => console.log('Registration successful')} |
| 77 | + /> |
| 78 | + )} |
| 79 | + |
| 80 | + {view === 'reset' && ( |
| 81 | + <PasswordReset onSuccess={() => console.log('Reset email sent')} /> |
| 82 | + )} |
| 83 | + </div> |
| 84 | + ); |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +4. Use the auth hook to access authentication state and methods: |
| 89 | + |
| 90 | +```jsx |
| 91 | +import { useAuth } from 'my-auth-package'; |
| 92 | + |
| 93 | +function UserProfile() { |
| 94 | + const { user, isAuthenticated, logout } = useAuth(); |
| 95 | + |
| 96 | + if (!isAuthenticated) { |
| 97 | + return <p>Please log in</p>; |
| 98 | + } |
| 99 | + |
| 100 | + return ( |
| 101 | + <div> |
| 102 | + <h1>Welcome, {user.name}!</h1> |
| 103 | + <button onClick={logout}>Log out</button> |
| 104 | + </div> |
| 105 | + ); |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +## API Reference |
| 110 | + |
| 111 | +### Components |
| 112 | + |
| 113 | +- `<AuthProvider>` - Main provider for authentication context |
| 114 | +- `<LoginForm>` - Pre-built login form component |
| 115 | +- `<RegisterForm>` - Pre-built registration form component |
| 116 | +- `<PasswordReset>` - Password reset form component |
| 117 | +- `<VerificationPrompt>` - Email verification component |
| 118 | + |
| 119 | +### Hooks |
| 120 | + |
| 121 | +- `useAuth()` - Hook to access authentication context |
| 122 | + |
| 123 | +### Configuration |
| 124 | + |
| 125 | +```typescript |
| 126 | +configureAuth({ |
| 127 | + baseUrl: string; |
| 128 | + endpoints?: { |
| 129 | + login: string; |
| 130 | + register: string; |
| 131 | + logout: string; |
| 132 | + refresh: string; |
| 133 | + passwordReset: string; |
| 134 | + verifyEmail: string; |
| 135 | + }; |
| 136 | + tokenStorage: 'localStorage' | 'sessionStorage' | 'cookie' | 'memory'; |
| 137 | +}) |
| 138 | +``` |
| 139 | + |
| 140 | +## Customization |
| 141 | + |
| 142 | +You can customize the components by styling them directly or creating your own components using the `useAuth` hook: |
| 143 | + |
| 144 | +```jsx |
| 145 | +import { useAuth } from 'my-auth-package'; |
| 146 | + |
| 147 | +function CustomLoginForm() { |
| 148 | + const { login } = useAuth(); |
| 149 | + const [email, setEmail] = useState(''); |
| 150 | + const [password, setPassword] = useState(''); |
| 151 | + |
| 152 | + const handleSubmit = async e => { |
| 153 | + e.preventDefault(); |
| 154 | + try { |
| 155 | + await login({ email, password }); |
| 156 | + // Success handling |
| 157 | + } catch (error) { |
| 158 | + // Error handling |
| 159 | + } |
| 160 | + }; |
| 161 | + |
| 162 | + return <form onSubmit={handleSubmit}>{/* Your custom form elements */}</form>; |
| 163 | +} |
| 164 | +``` |
| 165 | + |
| 166 | +## License |
| 167 | + |
| 168 | +MIT |
| 169 | + |
1 | 170 | # TSDX React User Guide |
2 | 171 |
|
3 | | -Congrats! You just saved yourself hours of work by bootstrapping this project with TSDX. Let’s get you oriented with what’s here and how to use it. |
| 172 | +Congrats! You just saved yourself hours of work by bootstrapping this project with TSDX. Let's get you oriented with what's here and how to use it. |
4 | 173 |
|
5 | | -> This TSDX setup is meant for developing React component libraries (not apps!) that can be published to NPM. If you’re looking to build a React-based app, you should use `create-react-app`, `razzle`, `nextjs`, `gatsby`, or `react-static`. |
| 174 | +> This TSDX setup is meant for developing React component libraries (not apps!) that can be published to NPM. If you're looking to build a React-based app, you should use `create-react-app`, `razzle`, `nextjs`, `gatsby`, or `react-static`. |
6 | 175 |
|
7 | | -> If you’re new to TypeScript and React, checkout [this handy cheatsheet](https://github.com/sw-yx/react-typescript-cheatsheet/) |
| 176 | +> If you're new to TypeScript and React, checkout [this handy cheatsheet](https://github.com/sw-yx/react-typescript-cheatsheet/) |
8 | 177 |
|
9 | 178 | ## Commands |
10 | 179 |
|
|
0 commit comments