|
| 1 | +# @think-grid-labs/react-starter-auth |
| 2 | + |
1 | 3 | <div align="center"> |
2 | | - <a href="#" title="React Starter Authentication"> |
3 | | - <img src="https://github.com/akosidencio/react-starter-auth/blob/main/react-starter-auth.png" alt="React Starter Authentication" /> |
4 | | - </a> |
| 4 | + <img src="https://raw.githubusercontent.com/ThinkGrid-Labs/react-starter-auth/main/react-starter-auth.png" alt="React Starter Authentication" width="600" /> |
| 5 | + <p>A lightweight, type-safe, and robust authentication suite for React and Next.js.</p> |
5 | 6 | </div> |
6 | 7 |
|
| 8 | +--- |
7 | 9 |
|
8 | | -### Features |
| 10 | +### 🚀 Features |
| 11 | +- **Type-Safe**: Built with TypeScript, supporting generic User objects for better developer experience. |
| 12 | +- **Next.js & React Ready**: Optimized for both client-side and server-side contexts. |
| 13 | +- **JWT Centric**: Seamless handling of JWT tokens with automatic expiry checks. |
| 14 | +- **Secure by Default**: Strict cookie settings and secure state persistence. |
| 15 | +- **Fetcher Integration**: Auto-injects `Authorization` headers into your API requests. |
| 16 | +- **Route Protection**: Higher-Order Components and Wrapper components for private routes. |
9 | 17 |
|
10 | | -Authentication library built for react token based authentication with JWT |
11 | | -- Lightweight and easy to use |
12 | | -- Built for React JS |
13 | | -- Works with Next js |
14 | | -- JWT based authentication |
15 | | -- Secure client authentication |
| 18 | +--- |
16 | 19 |
|
17 | | -### Installation |
18 | | -```jsx |
19 | | - npm install @think-grid-labs/react-starter-auth |
20 | | - |
21 | | - yarn @think-grid-labs/react-starter-auth |
| 20 | +### 📦 Installation |
| 21 | + |
| 22 | +```bash |
| 23 | +# Using pnpm |
| 24 | +pnpm add @think-grid-labs/react-starter-auth |
| 25 | + |
| 26 | +# Using npm |
| 27 | +npm install @think-grid-labs/react-starter-auth |
| 28 | + |
| 29 | +# Using yarn |
| 30 | +yarn add @think-grid-labs/react-starter-auth |
22 | 31 | ``` |
23 | 32 |
|
24 | | -### Setup |
| 33 | +--- |
25 | 34 |
|
26 | | -```jsx |
27 | | -import { AuthProvider } from '@think-grid-labs/react-starter-auth'; |
| 35 | +### 🛠️ Setup |
| 36 | + |
| 37 | +Wrap your application in the `AuthProvider`: |
28 | 38 |
|
29 | | -<AuthProvider> |
30 | | - <App> |
31 | | -</AuthProvider> |
| 39 | +```tsx |
| 40 | +import { AuthProvider } from '@think-grid-labs/react-starter-auth'; |
32 | 41 |
|
| 42 | +function App({ children }) { |
| 43 | + return ( |
| 44 | + <AuthProvider> |
| 45 | + {children} |
| 46 | + </AuthProvider> |
| 47 | + ); |
| 48 | +} |
33 | 49 | ``` |
34 | | -### Signin |
35 | 50 |
|
36 | | -```jsx |
37 | | -import { useAuth } from '@think-grid-labs/react-starter-auth' |
| 51 | +--- |
38 | 52 |
|
39 | | -const { signIn } = useAuth() |
| 53 | +### 🔑 Authentication Flow |
40 | 54 |
|
41 | | -const access_token = 'jsjdjdjsxxfd' // response from api |
42 | | -const user { |
43 | | - name: 'john doe', |
44 | | - email: 'example@example.com', |
45 | | - phone: '', |
46 | | - role: '' |
47 | | -} |
| 55 | +#### Sign In |
| 56 | +```tsx |
| 57 | +import { useAuth } from '@think-grid-labs/react-starter-auth'; |
48 | 58 |
|
49 | | -const authuser = { |
50 | | - token: access_token, |
51 | | - user: user |
52 | | -} |
53 | | -signIn(authuser) |
| 59 | +const { signIn } = useAuth(); |
54 | 60 |
|
| 61 | +const handleLogin = async () => { |
| 62 | + const { token, user } = await api.login(credentials); |
| 63 | + |
| 64 | + signIn({ |
| 65 | + token, |
| 66 | + user: { |
| 67 | + id: user.id, |
| 68 | + name: user.display_name, |
| 69 | + role: 'admin' |
| 70 | + } |
| 71 | + }); |
| 72 | +}; |
55 | 73 | ``` |
56 | 74 |
|
57 | | -### User |
58 | | - |
59 | | -```jsx |
60 | | -import { useAuth } from '@think-grid-labs/react-starter-auth' |
| 75 | +#### Get User (State) |
| 76 | +```tsx |
| 77 | +import { useAuth } from '@think-grid-labs/react-starter-auth'; |
61 | 78 |
|
62 | | -// You can pass a custom User type for better type safety |
63 | | -interface MyUser { |
| 79 | +// Define your own User interface for full type safety |
| 80 | +interface UserProfile { |
| 81 | + id: string; |
64 | 82 | name: string; |
65 | | - email: string; |
| 83 | + role: string; |
66 | 84 | } |
67 | 85 |
|
68 | | -const { isAuthenticated, user } = useAuth<MyUser>() |
| 86 | +const { user, isAuthenticated, isLoading } = useAuth<UserProfile>(); |
| 87 | + |
| 88 | +console.log(user?.role); // Fully typed! |
69 | 89 | ``` |
70 | 90 |
|
71 | | -### Fetcher |
| 91 | +--- |
| 92 | + |
| 93 | +### 📡 Data Fetching |
72 | 94 |
|
73 | | -fetcher extends the native Web fetch() API to update each request on the server to set headers Authorizaton Bearer upon sign in. |
| 95 | +The `fetcher` utility automatically appends the `Authorization: Bearer <token>` header to your requests if a valid token exists. |
74 | 96 |
|
75 | | -```jsx |
76 | | -import { fetcher } from '@think-grid-labs/react-starter-auth' |
| 97 | +```tsx |
| 98 | +import { fetcher } from '@think-grid-labs/react-starter-auth'; |
77 | 99 |
|
78 | | -const res = fetcher('https://example.com/api/posts') // GET |
79 | | -const data = await res.json() |
| 100 | +const getProfile = async () => { |
| 101 | + const response = await fetcher('https://api.example.com/me'); |
| 102 | + const data = await response.json(); |
| 103 | + return data; |
| 104 | +}; |
| 105 | +``` |
| 106 | + |
| 107 | +--- |
| 108 | + |
| 109 | +### 🛡️ Route Protection |
80 | 110 |
|
81 | | -const res = fetcher('https://example.com/api/posts', { method: 'POST', body: JSON.stringify(data) }) // POST |
82 | | -const data = await res.json() |
| 111 | +#### Using `ProtectedRoute` |
| 112 | +```tsx |
| 113 | +import { ProtectedRoute } from '@think-grid-labs/react-starter-auth'; |
| 114 | +import Dashboard from './Dashboard'; |
83 | 115 |
|
| 116 | +// Automatically redirects to /login if not authenticated |
| 117 | +<ProtectedRoute component={Dashboard} redirectPath="/login" /> |
84 | 118 | ``` |
85 | 119 |
|
86 | | -### Private route page |
| 120 | +#### Using `withAuthentication` (HOC) |
| 121 | +```tsx |
| 122 | +import { withAuthentication } from '@think-grid-labs/react-starter-auth'; |
87 | 123 |
|
88 | | -```jsx |
89 | | -import { ProtectedRoute, withAuthentication } from '@think-grid-labs/react-starter-auth' |
| 124 | +const PrivatePage = () => <div>Private Content</div>; |
90 | 125 |
|
| 126 | +export default withAuthentication(PrivatePage); |
91 | 127 | ``` |
92 | 128 |
|
| 129 | +--- |
| 130 | + |
| 131 | +### 📄 License |
| 132 | +MIT © [ThinkGrid-Labs](https://github.com/ThinkGrid-Labs) |
| 133 | + |
93 | 134 |
|
0 commit comments