@yaotoshi/auth-sdk 0.2.1 → 0.2.2

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.
Files changed (2) hide show
  1. package/README.md +171 -0
  2. package/package.json +3 -2
package/README.md ADDED
@@ -0,0 +1,171 @@
1
+ # @yaotoshi/auth-sdk
2
+
3
+ Browser SDK for integrating with a Yaotoshi Accounts service. Handles OAuth 2.0 + PKCE login, token management, and logout.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install @yaotoshi/auth-sdk
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```ts
14
+ import { YaotoshiAuth } from '@yaotoshi/auth-sdk';
15
+
16
+ const auth = new YaotoshiAuth({
17
+ clientId: 'your-client-id',
18
+ redirectUri: 'http://localhost:3000/callback',
19
+ postLogoutRedirectUri: 'http://localhost:3000',
20
+ accountsUrl: 'http://localhost:9999',
21
+ });
22
+ ```
23
+
24
+ ### Login
25
+
26
+ ```ts
27
+ // Redirects the user to the accounts service login page
28
+ auth.login();
29
+ ```
30
+
31
+ ### Handle Callback
32
+
33
+ On your `/callback` page:
34
+
35
+ ```ts
36
+ const { accessToken, user, scope, expiresIn } = await auth.handleCallback();
37
+ // user.email, user.sub (unique ID)
38
+ // expiresIn: token lifetime in seconds
39
+ ```
40
+
41
+ ### Check Auth Status
42
+
43
+ ```ts
44
+ // Returns false if no token or token is expired
45
+ auth.isAuthenticated();
46
+ ```
47
+
48
+ ### Get User Info
49
+
50
+ ```ts
51
+ const user = await auth.getUser();
52
+ // { sub: 'user-id', email: 'user@example.com', email_verified: true }
53
+ ```
54
+
55
+ ### Get Access Token
56
+
57
+ ```ts
58
+ const token = auth.getAccessToken();
59
+ // Use in Authorization header for your own API calls
60
+ ```
61
+
62
+ ### Logout
63
+
64
+ ```ts
65
+ // Revokes the session on the server, clears local token, redirects to postLogoutRedirectUri
66
+ await auth.logout();
67
+ ```
68
+
69
+ ## Config Options
70
+
71
+ | Option | Required | Default | Description |
72
+ |--------|----------|---------|-------------|
73
+ | `clientId` | Yes | — | OAuth client ID (from the accounts admin panel) |
74
+ | `redirectUri` | Yes | — | Your app's callback URL (must be registered in the OAuth client) |
75
+ | `accountsUrl` | Yes | — | Base URL of the accounts service |
76
+ | `postLogoutRedirectUri` | No | — | Where to redirect after logout. If not set, user stays on the accounts login page |
77
+ | `scopes` | No | `['openid', 'email']` | OAuth scopes to request |
78
+ | `apiPathPrefix` | No | `'/api/proxy'` | API path prefix. Use `''` if connecting directly to the API |
79
+ | `storagePrefix` | No | `'yaotoshi_auth'` | Prefix for localStorage/sessionStorage keys |
80
+
81
+ ## Setup
82
+
83
+ Before using the SDK, you need an OAuth client registered in the accounts service:
84
+
85
+ 1. Open the accounts admin panel
86
+ 2. Go to **Admin > Clients**
87
+ 3. Create a new client with:
88
+ - **Type**: `PUBLIC`
89
+ - **Redirect URIs**: your callback URL (e.g. `http://localhost:3000/callback`)
90
+ - **Post-Logout Redirect URIs**: your app URL (e.g. `http://localhost:3000`)
91
+ 4. Copy the **Client ID**
92
+
93
+ ## React Example
94
+
95
+ ```tsx
96
+ import { YaotoshiAuth } from '@yaotoshi/auth-sdk';
97
+ import { useEffect, useState } from 'react';
98
+
99
+ const auth = new YaotoshiAuth({
100
+ clientId: 'your-client-id',
101
+ redirectUri: 'http://localhost:3000/callback',
102
+ postLogoutRedirectUri: 'http://localhost:3000',
103
+ accountsUrl: 'http://localhost:9999',
104
+ });
105
+
106
+ function LoginPage() {
107
+ return <button onClick={() => auth.login()}>Sign in</button>;
108
+ }
109
+
110
+ function CallbackPage() {
111
+ useEffect(() => {
112
+ auth.handleCallback()
113
+ .then(() => window.location.href = '/dashboard')
114
+ .catch(err => console.error('Login failed:', err));
115
+ }, []);
116
+ return <p>Signing you in...</p>;
117
+ }
118
+
119
+ function Dashboard() {
120
+ const [user, setUser] = useState(null);
121
+
122
+ useEffect(() => {
123
+ if (!auth.isAuthenticated()) {
124
+ window.location.href = '/login';
125
+ return;
126
+ }
127
+ auth.getUser().then(setUser);
128
+ }, []);
129
+
130
+ if (!user) return <p>Loading...</p>;
131
+ return (
132
+ <div>
133
+ <p>Welcome, {user.email}</p>
134
+ <button onClick={() => auth.logout()}>Sign out</button>
135
+ </div>
136
+ );
137
+ }
138
+ ```
139
+
140
+ ## Connecting Directly to the API
141
+
142
+ By default, the SDK sends requests through a Next.js proxy at `/api/proxy/*`. If your app connects directly to the accounts API:
143
+
144
+ ```ts
145
+ const auth = new YaotoshiAuth({
146
+ clientId: 'your-client-id',
147
+ redirectUri: 'http://localhost:3000/callback',
148
+ accountsUrl: 'http://localhost:9998', // direct API URL
149
+ apiPathPrefix: '', // no proxy
150
+ });
151
+ ```
152
+
153
+ ## Security
154
+
155
+ - Tokens are stored in `localStorage` (same approach as Auth0, Firebase)
156
+ - PKCE (S256) is used for all OAuth flows
157
+ - Token expiry is tracked — `isAuthenticated()` returns `false` when expired
158
+ - Mitigate XSS risk with a strong `Content-Security-Policy` header on your app
159
+
160
+ ## Browser Only
161
+
162
+ This SDK requires `window`, `localStorage`, `sessionStorage`, and `crypto.subtle`. It is designed for browser environments only.
163
+
164
+ ## Links
165
+
166
+ - [GitHub](https://github.com/onchainyaotoshi/accounts)
167
+ - [npm](https://www.npmjs.com/package/@yaotoshi/auth-sdk)
168
+
169
+ ## License
170
+
171
+ MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yaotoshi/auth-sdk",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "Authentication SDK for Yaotoshi ecosystem apps",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -17,7 +17,8 @@
17
17
  }
18
18
  },
19
19
  "files": [
20
- "dist"
20
+ "dist",
21
+ "README.md"
21
22
  ],
22
23
  "scripts": {
23
24
  "build": "tsup",