Skip to content

Commit 045f937

Browse files
committed
Initial commit
1 parent f214952 commit 045f937

File tree

14 files changed

+1204
-28
lines changed

14 files changed

+1204
-28
lines changed

README.md

Lines changed: 172 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,179 @@
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+
1170
# TSDX React User Guide
2171

3-
Congrats! You just saved yourself hours of work by bootstrapping this project with TSDX. Lets get you oriented with whats 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.
4173

5-
> This TSDX setup is meant for developing React component libraries (not apps!) that can be published to NPM. If youre 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`.
6175
7-
> If youre 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/)
8177
9178
## Commands
10179

example/index.tsx

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,66 @@
11
import 'react-app-polyfill/ie11';
22
import * as React from 'react';
33
import * as ReactDOM from 'react-dom';
4-
import { Thing } from '../.';
4+
import {
5+
AuthProvider,
6+
LoginForm,
7+
RegisterForm,
8+
PasswordReset,
9+
configureAuth,
10+
} from '../.';
11+
12+
// Configure auth endpoints
13+
configureAuth({
14+
baseUrl: 'https://api.example.com',
15+
tokenStorage: 'localStorage',
16+
});
517

618
const App = () => {
19+
const [view, setView] = React.useState<'login' | 'register' | 'reset'>(
20+
'login'
21+
);
22+
723
return (
8-
<div>
9-
<Thing />
10-
</div>
24+
<AuthProvider
25+
onLoginSuccess={user => console.log('Logged in successfully', user)}
26+
onRegisterSuccess={user => console.log('Registered successfully', user)}
27+
>
28+
<div style={{ maxWidth: '400px', margin: '0 auto', padding: '20px' }}>
29+
<h1>Auth Example</h1>
30+
31+
{view === 'login' && (
32+
<>
33+
<LoginForm />
34+
<div style={{ marginTop: '20px' }}>
35+
<button onClick={() => setView('register')}>
36+
Need an account? Register
37+
</button>
38+
<button onClick={() => setView('reset')}>Forgot password?</button>
39+
</div>
40+
</>
41+
)}
42+
43+
{view === 'register' && (
44+
<>
45+
<RegisterForm />
46+
<div style={{ marginTop: '20px' }}>
47+
<button onClick={() => setView('login')}>
48+
Already have an account? Login
49+
</button>
50+
</div>
51+
</>
52+
)}
53+
54+
{view === 'reset' && (
55+
<>
56+
<PasswordReset />
57+
<div style={{ marginTop: '20px' }}>
58+
<button onClick={() => setView('login')}>Back to Login</button>
59+
</div>
60+
</>
61+
)}
62+
</div>
63+
</AuthProvider>
1164
);
1265
};
1366

0 commit comments

Comments
 (0)