@riligar/auth-react 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +125 -0
- package/dist/index.esm.js +828 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.js +844 -0
- package/dist/index.js.map +1 -0
- package/package.json +58 -0
package/README.md
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# Auth React
|
|
2
|
+
|
|
3
|
+
Auth SDK for React with JWT and JWKS.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun add @riligar/auth-react
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Basic Usage
|
|
12
|
+
|
|
13
|
+
```jsx
|
|
14
|
+
import { AuthProvider, useAuth, useSignIn, ProtectedRoute } from '@riligar/auth-react';
|
|
15
|
+
|
|
16
|
+
// 1. Wrap your app with AuthProvider
|
|
17
|
+
function App() {
|
|
18
|
+
return (
|
|
19
|
+
<AuthProvider>
|
|
20
|
+
<Routes>
|
|
21
|
+
<Route path="/login" element={<Login />} />
|
|
22
|
+
<Route element={<ProtectedRoute />}>
|
|
23
|
+
<Route path="/" element={<Home />} />
|
|
24
|
+
</Route>
|
|
25
|
+
</Routes>
|
|
26
|
+
</AuthProvider>
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// 2. Use hooks in your components
|
|
31
|
+
function Login() {
|
|
32
|
+
const signIn = useSignIn();
|
|
33
|
+
|
|
34
|
+
async function handleSubmit(e) {
|
|
35
|
+
e.preventDefault();
|
|
36
|
+
const { email, password } = Object.fromEntries(new FormData(e.target));
|
|
37
|
+
try {
|
|
38
|
+
await signIn(email, password);
|
|
39
|
+
} catch (err) {
|
|
40
|
+
alert(err.message);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return (
|
|
45
|
+
<form onSubmit={handleSubmit}>
|
|
46
|
+
<input name="email" type="email" placeholder="Email" required />
|
|
47
|
+
<input name="password" type="password" placeholder="Password" required />
|
|
48
|
+
<button>Sign In</button>
|
|
49
|
+
</form>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function Home() {
|
|
54
|
+
const { user, isAuthenticated } = useAuth();
|
|
55
|
+
|
|
56
|
+
if (!isAuthenticated) return <p>Not authenticated</p>;
|
|
57
|
+
|
|
58
|
+
return <h1>Hello, {user?.email}!</h1>;
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Features
|
|
63
|
+
|
|
64
|
+
- ✅ **JWT Tokens** - Secure token-based authentication
|
|
65
|
+
- ✅ **JWKS** - Signature verification with `/.well-known/jwks.json`
|
|
66
|
+
- ✅ **Auto refresh** - Tokens renewed automatically
|
|
67
|
+
- ✅ **Social login** - OAuth provider support
|
|
68
|
+
- ✅ **Cross-tab sync** - Synchronized state across tabs
|
|
69
|
+
- ✅ **Route protection** - Protected routes automatically
|
|
70
|
+
- ✅ **SSR friendly** - Server-side rendering compatible
|
|
71
|
+
- ✅ **LocalStorage** - Token persistence in browser
|
|
72
|
+
|
|
73
|
+
## API
|
|
74
|
+
|
|
75
|
+
### Hooks
|
|
76
|
+
|
|
77
|
+
```jsx
|
|
78
|
+
const { user, loading, error, isAuthenticated } = useAuth();
|
|
79
|
+
const signIn = useSignIn();
|
|
80
|
+
const signUp = useSignUp();
|
|
81
|
+
const signOut = useSignOut();
|
|
82
|
+
const checkToken = useCheckToken();
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Utility functions
|
|
86
|
+
|
|
87
|
+
```jsx
|
|
88
|
+
import {
|
|
89
|
+
isAuthenticated,
|
|
90
|
+
getCurrentUser,
|
|
91
|
+
refreshToken,
|
|
92
|
+
socialRedirect
|
|
93
|
+
} from '@riligar/auth-react';
|
|
94
|
+
|
|
95
|
+
// Check if authenticated
|
|
96
|
+
if (isAuthenticated()) {
|
|
97
|
+
console.log('User logged in');
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Get current user data
|
|
101
|
+
const user = getCurrentUser();
|
|
102
|
+
|
|
103
|
+
// Manually refresh token
|
|
104
|
+
await refreshToken();
|
|
105
|
+
|
|
106
|
+
// Redirect to social login
|
|
107
|
+
socialRedirect('google', '/dashboard');
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## Backend Configuration
|
|
111
|
+
|
|
112
|
+
The SDK expects your backend to expose:
|
|
113
|
+
|
|
114
|
+
- `/.well-known/jwks.json` - Public keys for JWT verification
|
|
115
|
+
- `/auth/sign-in` - Login (returns `{ token, user }`)
|
|
116
|
+
- `/auth/sign-up` - Registration (returns `{ token, user }`)
|
|
117
|
+
- `/auth/sign-out` - Logout
|
|
118
|
+
- `/auth/refresh` - Token renewal
|
|
119
|
+
- `/auth/sign-in/:provider` - Social login
|
|
120
|
+
|
|
121
|
+
## Build
|
|
122
|
+
|
|
123
|
+
```bash
|
|
124
|
+
bun run build
|
|
125
|
+
```
|