@hotosm/hanko-auth 0.2.5 β†’ 0.3.0

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 CHANGED
@@ -1,210 +1,216 @@
1
- # @hotosm/hanko-auth
1
+ # Web Component: `<hotosm-auth>`
2
2
 
3
- Header authentication widget for HOTOSM applications. Displays login/logout state and user profile dropdown.
4
-
5
- **Note:** This package provides only the header widget. The actual login form and profile pages are hosted at the [HOTOSM Login service](https://login.hotosm.org). Users will be redirected there for authentication.
3
+ Lit-based web component for HOTOSM SSO authentication with Hanko and OpenStreetMap integration.
6
4
 
7
5
  ## Installation
8
6
 
9
- Install the package and its peer dependency:
10
-
11
7
  ```bash
12
8
  # npm
13
- npm install @hotosm/hanko-auth @awesome.me/webawesome
9
+ npm install @hotosm/hanko-auth
10
+
11
+ # pnpm
12
+ pnpm add @hotosm/hanko-auth
14
13
 
15
14
  # yarn
16
- yarn add @hotosm/hanko-auth @awesome.me/webawesome
15
+ yarn add @hotosm/hanko-auth
16
+ ```
17
+
18
+ ### Peer Dependencies
19
+
20
+ This package requires Web Awesome to be installed separately:
21
+
22
+ ```bash
23
+ # npm
24
+ npm install @awesome.me/webawesome
17
25
 
18
26
  # pnpm
19
- pnpm add @hotosm/hanko-auth @awesome.me/webawesome
27
+ pnpm add @awesome.me/webawesome
28
+
29
+ # yarn
30
+ yarn add @awesome.me/webawesome
20
31
  ```
21
32
 
22
- ## Usage
33
+ **Why peer dependencies?**
34
+
35
+ - Control the Web Awesome version in your project
36
+ - Share Web Awesome across multiple packages without duplication
37
+ - Reduce bundle size when multiple packages use Web Awesome
23
38
 
24
- ### Basic Setup
39
+ ## Quick Start
40
+
41
+ ### Import the component
25
42
 
26
43
  ```javascript
27
- import '@awesome.me/webawesome';
28
- import '@hotosm/hanko-auth';
44
+ // Import Web Awesome (required peer dependency)
45
+ import "@awesome.me/webawesome";
46
+
47
+ // Import the auth component
48
+ import "@hotosm/hanko-auth";
49
+ ```
50
+
51
+ ### Use in HTML
52
+
53
+ ```html
54
+ <hotosm-auth
55
+ hanko-url="https://login.hotosm.org"
56
+ show-profile="true"
57
+ ></hotosm-auth>
29
58
  ```
30
59
 
31
- ### React/Preact
60
+ ### React Example
32
61
 
33
- ```jsx
34
- import '@awesome.me/webawesome';
35
- import '@hotosm/hanko-auth';
62
+ ```tsx
63
+ import { useEffect, useRef } from "react";
64
+ import "@awesome.me/webawesome";
65
+ import "@hotosm/hanko-auth";
36
66
 
37
- function Header() {
38
- return (
39
- <nav>
40
- <hotosm-auth hanko-url="https://login.hotosm.org" />
41
- </nav>
42
- );
67
+ export function AuthButton({ hankoUrl, onLogin }) {
68
+ const ref = useRef<HTMLElement>(null);
69
+
70
+ useEffect(() => {
71
+ const el = ref.current;
72
+ const handler = (e: CustomEvent) => onLogin(e.detail.user);
73
+ el?.addEventListener("hanko-login", handler);
74
+ return () => el?.removeEventListener("hanko-login", handler);
75
+ }, [onLogin]);
76
+
77
+ return <hotosm-auth ref={ref} hanko-url={hankoUrl} osm-required />;
43
78
  }
44
79
  ```
45
80
 
46
- ### HTML
81
+ ## Attributes
47
82
 
48
- ```html
49
- <!DOCTYPE html>
50
- <html>
51
- <head>
52
- <meta name="hanko-url" content="https://login.hotosm.org">
53
- </head>
54
- <body>
55
- <script type="module">
56
- import '@awesome.me/webawesome';
57
- import '@hotosm/hanko-auth';
58
- </script>
59
-
60
- <!-- Header auth widget -->
61
- <nav>
62
- <hotosm-auth></hotosm-auth>
63
- </nav>
64
- </body>
65
- </html>
66
- ```
83
+ ### Core
84
+
85
+ | Attribute | Type | Default | Description |
86
+ | ----------- | ------ | ------------------------ | ------------------------------------------ |
87
+ | `hanko-url` | string | `window.location.origin` | Login service URL for Hanko authentication |
88
+ | `base-path` | string | `""` | Base URL for OSM OAuth endpoints |
89
+ | `auth-path` | string | `/api/auth/osm` | OSM auth endpoints path |
90
+
91
+ ### Behavior
92
+
93
+ | Attribute | Type | Default | Description |
94
+ | ---------------- | ------- | -------------- | -------------------------- |
95
+ | `osm-required` | boolean | `false` | Require OSM connection |
96
+ | `osm-scopes` | string | `"read_prefs"` | Space-separated OSM scopes |
97
+ | `auto-connect` | boolean | `false` | Auto-redirect to OSM OAuth |
98
+ | `verify-session` | boolean | `false` | Verify session on return |
99
+
100
+ ### Display
101
+
102
+ | Attribute | Type | Default | Description |
103
+ | -------------- | ------- | ------- | ------------------------------------ |
104
+ | `show-profile` | boolean | `false` | Show full profile (vs header button) |
105
+ | `display-name` | string | `""` | Override display name |
106
+
107
+ ### Redirects
108
+
109
+ | Attribute | Type | Default | Description |
110
+ | ----------------------- | ------ | ------- | -------------------------- |
111
+ | `redirect-after-login` | string | `""` | URL after successful login |
112
+ | `redirect-after-logout` | string | `""` | URL after logout |
113
+
114
+ ### Cross-app
67
115
 
68
- ## Component API
116
+ | Attribute | Type | Default | Description |
117
+ | ------------------- | ------ | ------- | ----------------------------- |
118
+ | `mapping-check-url` | string | `""` | URL to check user mapping |
119
+ | `app-id` | string | `""` | App identifier for onboarding |
69
120
 
70
- ### Attributes/Properties
121
+ ## Events
71
122
 
72
- | Attribute | Type | Default | Description |
73
- |-----------|------|---------|-------------|
74
- | `hanko-url` | `string` | `window.location.origin` | Hanko API URL (e.g., `https://login.hotosm.org`) |
75
- | `redirect-after-login` | `string` | Current page | Redirect URL after successful login |
76
- | `redirect-after-logout` | `string` | `""` | Redirect URL after logout |
77
- | `display-name` | `string` | `""` | Override user display name |
78
- | `osm-required` | `boolean` | `false` | Show OSM connection requirement indicator |
79
- | `base-path` | `string` | `""` | Base path for API calls |
80
- | `auth-path` | `string` | `"/api/auth/osm"` | OSM connection check endpoint |
123
+ The component dispatches the following custom events:
81
124
 
82
- ### Events
125
+ | Event | Detail | When |
126
+ | --------------- | ---------------------- | --------------------------- |
127
+ | `hanko-login` | `{ user: HankoUser }` | User logged in |
128
+ | `osm-connected` | `{ osmData: OSMData }` | OSM account linked |
129
+ | `osm-skipped` | `{}` | User skipped OSM connection |
130
+ | `auth-complete` | `{}` | Auth flow complete |
131
+ | `logout` | `{}` | User logged out |
83
132
 
84
- The component dispatches custom events:
133
+ ### Event Handling Example
85
134
 
86
135
  ```javascript
87
- document.addEventListener('hanko-login', (event) => {
88
- console.log('User logged in:', event.detail.user);
136
+ const auth = document.querySelector("hotosm-auth");
137
+
138
+ auth.addEventListener("hanko-login", (e) => {
139
+ console.log("Logged in:", e.detail.user.email);
89
140
  });
90
141
 
91
- document.addEventListener('hanko-logout', () => {
92
- console.log('User logged out');
142
+ auth.addEventListener("osm-connected", (e) => {
143
+ console.log("OSM connected:", e.detail.osmData.osm_username);
93
144
  });
94
145
 
95
- document.addEventListener('osm-connected', (event) => {
96
- console.log('OSM connected:', event.detail.osmData);
146
+ auth.addEventListener("logout", () => {
147
+ console.log("User logged out");
148
+ window.location.href = "/";
97
149
  });
98
150
  ```
99
151
 
100
- ## Examples
152
+ ## Usage Modes
101
153
 
102
- ### Basic Header Widget
154
+ ### Header Mode (default)
103
155
 
104
- ```html
105
- <hotosm-auth
106
- hanko-url="https://login.hotosm.org"
107
- ></hotosm-auth>
108
- ```
109
-
110
- ### With Return URL
111
-
112
- Redirect users back to a specific page after login:
156
+ Shows a compact login button in the header:
113
157
 
114
158
  ```html
115
- <hotosm-auth
116
- hanko-url="https://login.hotosm.org"
117
- redirect-after-login="/dashboard"
118
- ></hotosm-auth>
159
+ <header>
160
+ <hotosm-auth
161
+ hanko-url="https://login.hotosm.org"
162
+ redirect-after-login="/"
163
+ ></hotosm-auth>
164
+ </header>
119
165
  ```
120
166
 
121
- ### Check OSM Connection Status
167
+ ### Profile Mode
122
168
 
123
- Show indicator if OSM connection is required:
169
+ Shows full authentication form (for login pages):
124
170
 
125
171
  ```html
126
172
  <hotosm-auth
127
173
  hanko-url="https://login.hotosm.org"
174
+ show-profile
128
175
  osm-required
129
- auth-path="/api/auth/osm"
176
+ auto-connect
177
+ redirect-after-login="https://portal.hotosm.org"
130
178
  ></hotosm-auth>
131
179
  ```
132
180
 
133
- ### Custom Display Name
181
+ ## Configuration
182
+
183
+ ### Hanko URL Detection
184
+
185
+ The component detects the Hanko URL in the following priority order:
134
186
 
135
- Override the displayed user name:
187
+ 1. `hanko-url` attribute
188
+ 2. `<meta name="hanko-url" content="...">` tag
189
+ 3. `window.HANKO_URL` global variable
190
+ 4. `window.location.origin` (fallback)
136
191
 
137
192
  ```html
138
- <hotosm-auth
139
- hanko-url="https://login.hotosm.org"
140
- display-name="Custom Name"
141
- ></hotosm-auth>
193
+ <!-- Option 1: Attribute -->
194
+ <hotosm-auth hanko-url="https://login.hotosm.org"></hotosm-auth>
195
+
196
+ <!-- Option 2: Meta tag -->
197
+ <meta name="hanko-url" content="https://login.hotosm.org" />
198
+ <hotosm-auth></hotosm-auth>
199
+
200
+ <!-- Option 3: JavaScript -->
201
+ <script>
202
+ window.HANKO_URL = "https://login.hotosm.org";
203
+ </script>
204
+ <hotosm-auth></hotosm-auth>
142
205
  ```
143
206
 
144
207
  ## Styling
145
208
 
146
- The component uses the HOT design system with CSS custom properties injected from CDN:
147
-
148
- - `hotosm-ui-design/dist/hot.css` - Color, typography, spacing variables
149
- - `hotosm-ui-design/dist/hot-font-face.css` - Font definitions
150
- - `hotosm-ui-design/dist/hot-wa.css` - WebAwesome theme overrides
151
-
152
- You can override styles by setting CSS custom properties:
209
+ The component uses Shadow DOM. Override styles with CSS custom properties:
153
210
 
154
211
  ```css
155
212
  hotosm-auth {
156
- --hot-color-gray-800: #1a1a1a;
157
- --hot-font-sans: 'Your Font', sans-serif;
213
+ --primary-color: #d73f3f;
214
+ --text-color: #333;
158
215
  }
159
216
  ```
160
-
161
- ## Features
162
-
163
- - πŸ” **Authentication State** - Shows login/logout status and user info
164
- - πŸ‘€ **Profile Dropdown** - Quick access to profile and logout
165
- - πŸ—ΊοΈ **OSM Status Indicator** - Shows OpenStreetMap connection status
166
- - 🎨 **HOT Design System** - Consistent styling with HOT branding
167
- - πŸ“± **Responsive** - Works on desktop and mobile
168
- - ⚑ **Lightweight** - Built with Lit web components
169
- - πŸ”„ **Cross-App Sessions** - Shared authentication across HOT tools
170
- - 🎯 **Framework Agnostic** - Works with React, Vue, vanilla JS, etc.
171
-
172
- ## How It Works
173
-
174
- 1. **Logged Out**: Displays a "Log in" button
175
- 2. **Click Login**: Redirects to `https://login.hotosm.org` for authentication
176
- 3. **After Login**: User returns to your app (or `redirect-after-login` URL)
177
- 4. **Logged In**: Shows user avatar with dropdown menu:
178
- - My Profile (redirects to login service)
179
- - OSM connection status
180
- - Sign Out
181
-
182
- ## Browser Support
183
-
184
- - Chrome/Edge (latest)
185
- - Firefox (latest)
186
- - Safari (latest)
187
-
188
- ## Development
189
-
190
- ```bash
191
- # Install dependencies
192
- pnpm install
193
-
194
- # Build
195
- pnpm build
196
-
197
- # Development with hot reload
198
- pnpm dev
199
- ```
200
-
201
- ## License
202
-
203
- AGPL-3.0
204
-
205
- ## Links
206
-
207
- - [Documentation](https://hotosm.github.io/auth-docs)
208
- - [GitHub Repository](https://github.com/hotosm/login)
209
- - [Issues](https://github.com/hotosm/login/issues)
210
- - [HOTOSM Website](https://www.hotosm.org)