@inglorious/web 6.26.1 → 6.27.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 +56 -25
- package/package.json +3 -3
- package/src/mobile/index.js +26 -0
- package/types/mobile.d.ts +19 -0
package/README.md
CHANGED
|
@@ -609,45 +609,36 @@ test("requireAuth blocks unauthenticated access", () => {
|
|
|
609
609
|
// Compose the guard with the page type
|
|
610
610
|
const GuardedPage = requireAuth(AdminPage)
|
|
611
611
|
|
|
612
|
-
// Signed out: the
|
|
613
|
-
const mockApi = {
|
|
614
|
-
notify: jest.fn(),
|
|
615
|
-
getEntity: () => ({ user: null }),
|
|
616
|
-
}
|
|
617
|
-
|
|
612
|
+
// Signed out: the guard was told of no user
|
|
618
613
|
const { events } = trigger(
|
|
619
|
-
{ type: "GuardedPage", id: "admin" },
|
|
614
|
+
{ type: "GuardedPage", id: "admin", user: null },
|
|
620
615
|
GuardedPage.routeChange,
|
|
621
|
-
{ route: "
|
|
622
|
-
mockApi,
|
|
616
|
+
{ route: "admin", path: "/admin" },
|
|
623
617
|
)
|
|
624
618
|
|
|
625
619
|
// Should redirect to login
|
|
626
620
|
expect(events).toContainEqual(
|
|
627
621
|
expect.objectContaining({
|
|
628
622
|
type: "navigate",
|
|
629
|
-
payload: expect.objectContaining({ to: "/login" }),
|
|
623
|
+
payload: expect.objectContaining({ to: "/login?from=%2Fadmin" }),
|
|
630
624
|
}),
|
|
631
625
|
)
|
|
632
626
|
})
|
|
633
627
|
|
|
634
628
|
test("requireAuth allows authenticated access", () => {
|
|
635
|
-
// Signed in: the
|
|
629
|
+
// Signed in: the guard was told of a user
|
|
636
630
|
const GuardedPage = requireAuth(AdminPage)
|
|
637
|
-
const mockApi = {
|
|
638
|
-
notify: jest.fn(),
|
|
639
|
-
getEntity: () => ({ user: { id: 1 } }),
|
|
640
|
-
}
|
|
641
631
|
|
|
642
632
|
const { events } = trigger(
|
|
643
|
-
{ type: "GuardedPage", id: "admin" },
|
|
633
|
+
{ type: "GuardedPage", id: "admin", user: { id: 1 } },
|
|
644
634
|
GuardedPage.routeChange,
|
|
645
|
-
{ route: "
|
|
646
|
-
mockApi,
|
|
635
|
+
{ route: "admin", path: "/admin" },
|
|
647
636
|
)
|
|
648
637
|
|
|
649
638
|
// Should NOT redirect
|
|
650
|
-
expect(events).
|
|
639
|
+
expect(events).not.toContainEqual(
|
|
640
|
+
expect.objectContaining({ type: "navigate" }),
|
|
641
|
+
)
|
|
651
642
|
})
|
|
652
643
|
```
|
|
653
644
|
|
|
@@ -1128,16 +1119,30 @@ Guards are simply behaviors that intercept events (like `routeChange`) and can p
|
|
|
1128
1119
|
#### Example: Authentication Guard
|
|
1129
1120
|
|
|
1130
1121
|
```javascript
|
|
1122
|
+
// session.js: the one place the user lives (kept between visits with
|
|
1123
|
+
// withStorage, if at all), announcing each change
|
|
1124
|
+
export const Session = {
|
|
1125
|
+
signIn(entity, user, api) {
|
|
1126
|
+
entity.user = user
|
|
1127
|
+
api.notify("sessionChange", user)
|
|
1128
|
+
},
|
|
1129
|
+
signOut(entity, payload, api) {
|
|
1130
|
+
entity.user = null
|
|
1131
|
+
api.notify("sessionChange", null)
|
|
1132
|
+
},
|
|
1133
|
+
}
|
|
1134
|
+
|
|
1131
1135
|
// guards/require-auth.js
|
|
1132
1136
|
export const requireAuth = (type) => ({
|
|
1137
|
+
sessionChange(entity, user, api) {
|
|
1138
|
+
entity.user = user
|
|
1139
|
+
type.sessionChange?.(entity, user, api)
|
|
1140
|
+
},
|
|
1133
1141
|
routeChange(entity, payload, api) {
|
|
1134
1142
|
// Only act when navigating to this specific route
|
|
1135
1143
|
if (payload.route !== entity.id) return
|
|
1136
1144
|
|
|
1137
|
-
|
|
1138
|
-
// lives (kept between visits with withStorage, if at all)
|
|
1139
|
-
const { user } = api.getEntity("session") ?? {}
|
|
1140
|
-
if (!user) {
|
|
1145
|
+
if (!entity.user) {
|
|
1141
1146
|
// Redirect to login, preserving the intended destination
|
|
1142
1147
|
api.notify("navigate", {
|
|
1143
1148
|
to: `/login?from=${encodeURIComponent(payload.path)}`,
|
|
@@ -1152,6 +1157,8 @@ export const requireAuth = (type) => ({
|
|
|
1152
1157
|
})
|
|
1153
1158
|
```
|
|
1154
1159
|
|
|
1160
|
+
The guard keeps what `sessionChange` told it rather than asking `api.getEntity("session")`, which during an update returns the state from before it: after a sign-in followed by navigation in the same chain of events, it would still have no user.
|
|
1161
|
+
|
|
1155
1162
|
After signing in, `safePath(query.from)` gives the destination back only if it stays on the site, so a crafted `/login?from=//evil.com` leads nowhere: `api.notify("navigate", safePath(query.from) ?? "/")`.
|
|
1156
1163
|
|
|
1157
1164
|
#### Using Guards with Type Composition
|
|
@@ -1718,6 +1725,30 @@ api.notify("#share:share", {
|
|
|
1718
1725
|
|
|
1719
1726
|
---
|
|
1720
1727
|
|
|
1728
|
+
## Native Storage
|
|
1729
|
+
|
|
1730
|
+
iOS and Android can clear a WebView's `localStorage` when the device runs low on space. `createNativeStorage` keeps values in Capacitor Preferences instead, in the native app, and in `localStorage` in a browser. Give it to [`withStorage`](https://www.npmjs.com/package/@inglorious/store#withstorage):
|
|
1731
|
+
|
|
1732
|
+
```javascript
|
|
1733
|
+
import { Preferences } from "@capacitor/preferences"
|
|
1734
|
+
import { withStorage } from "@inglorious/store/storage"
|
|
1735
|
+
import { createNativeStorage } from "@inglorious/web/mobile"
|
|
1736
|
+
|
|
1737
|
+
const preferences = createNativeStorage({ plugin: Preferences })
|
|
1738
|
+
|
|
1739
|
+
const types = {
|
|
1740
|
+
Settings: [Settings, withStorage(["theme"], { storage: () => preferences })],
|
|
1741
|
+
}
|
|
1742
|
+
```
|
|
1743
|
+
|
|
1744
|
+
Preferences answers with promises, so in the native app the entity starts with its defaults and gets the stored fields back a moment later, through `storageRestore`. In a browser it is `localStorage` itself.
|
|
1745
|
+
|
|
1746
|
+
It works as Supabase's session storage too, so signing in survives the same clean-ups:
|
|
1747
|
+
|
|
1748
|
+
```javascript
|
|
1749
|
+
createClient(url, key, { auth: { storage: preferences } })
|
|
1750
|
+
```
|
|
1751
|
+
|
|
1721
1752
|
## Haptics
|
|
1722
1753
|
|
|
1723
1754
|
`@inglorious/web/haptics` plays taps the user can feel. In a native app it uses the system's haptics through [`@capacitor/haptics`](https://capacitorjs.com/docs/apis/haptics), once you hand the plugin over. Elsewhere it vibrates where the browser can (Chrome on Android), once the user has tapped the page; iOS browsers stay silent. Every call returns whether it played.
|
|
@@ -2083,8 +2114,8 @@ import { render, trigger } from "@inglorious/web/test"
|
|
|
2083
2114
|
|
|
2084
2115
|
import { Form } from "@inglorious/web/form"
|
|
2085
2116
|
import { Router, setRoutes } from "@inglorious/web/router"
|
|
2086
|
-
import { Geolocation } from "@inglorious/web/geolocation"
|
|
2087
|
-
import { Compass } from "@inglorious/web/compass"
|
|
2117
|
+
import { Geolocation } from "@inglorious/web/sensors/geolocation"
|
|
2118
|
+
import { Compass } from "@inglorious/web/sensors/compass"
|
|
2088
2119
|
```
|
|
2089
2120
|
|
|
2090
2121
|
This keeps `lit-html` as an internal implementation detail while still giving applications stable, tree-shakeable import paths.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/web",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.27.0",
|
|
4
4
|
"description": "A new web framework that leverages the power of the Inglorious Store combined with the performance and simplicity of lit-html.",
|
|
5
5
|
"author": "Matteo Antony Mistretta <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
|
|
6
6
|
"contributors": [
|
|
@@ -157,12 +157,12 @@
|
|
|
157
157
|
},
|
|
158
158
|
"sideEffects": false,
|
|
159
159
|
"dependencies": {
|
|
160
|
-
"@inglorious/utils": "^5.
|
|
160
|
+
"@inglorious/utils": "^5.4.0",
|
|
161
161
|
"@lit-labs/ssr-client": "^1.1.8",
|
|
162
162
|
"lit-html": "^3.3.3"
|
|
163
163
|
},
|
|
164
164
|
"peerDependencies": {
|
|
165
|
-
"@inglorious/store": "^12.
|
|
165
|
+
"@inglorious/store": "^12.6.0"
|
|
166
166
|
},
|
|
167
167
|
"devDependencies": {
|
|
168
168
|
"@inglorious/eslint-config": "1.2.0",
|
package/src/mobile/index.js
CHANGED
|
@@ -15,3 +15,29 @@ export function isNative() {
|
|
|
15
15
|
|
|
16
16
|
return capacitor.isNative === true
|
|
17
17
|
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A storage for `withStorage` that survives the operating system clearing the
|
|
21
|
+
* WebView's data: in a native app, Capacitor Preferences (`Preferences` from
|
|
22
|
+
* `@capacitor/preferences`, given as `plugin`), answering with promises; in a
|
|
23
|
+
* browser, `localStorage` itself. It also serves as Supabase's `auth.storage`,
|
|
24
|
+
* keeping the session there too.
|
|
25
|
+
*
|
|
26
|
+
* @param {{ plugin?: { get(options: { key: string }): Promise<{ value: string | null }>, set(options: { key: string, value: string }): Promise<void>, remove(options: { key: string }): Promise<void> } }} [config]
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* import { Preferences } from "@capacitor/preferences"
|
|
30
|
+
* const preferences = createNativeStorage({ plugin: Preferences })
|
|
31
|
+
* const types = {
|
|
32
|
+
* Settings: [Settings, withStorage(["theme"], { storage: () => preferences })],
|
|
33
|
+
* }
|
|
34
|
+
*/
|
|
35
|
+
export function createNativeStorage({ plugin } = {}) {
|
|
36
|
+
if (!plugin || !isNative()) return globalThis.localStorage
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
getItem: async (key) => (await plugin.get({ key })).value,
|
|
40
|
+
setItem: (key, value) => plugin.set({ key, value }),
|
|
41
|
+
removeItem: (key) => plugin.remove({ key }),
|
|
42
|
+
}
|
|
43
|
+
}
|
package/types/mobile.d.ts
CHANGED
|
@@ -1 +1,20 @@
|
|
|
1
1
|
export declare function isNative(): boolean
|
|
2
|
+
|
|
3
|
+
/** What `createNativeStorage` needs of `Preferences` from `@capacitor/preferences`. */
|
|
4
|
+
export interface PreferencesPlugin {
|
|
5
|
+
get(options: { key: string }): Promise<{ value: string | null }>
|
|
6
|
+
set(options: { key: string; value: string }): Promise<void>
|
|
7
|
+
remove(options: { key: string }): Promise<void>
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** A storage for `withStorage`: answering with promises in a native app. */
|
|
11
|
+
export interface NativeStorage {
|
|
12
|
+
getItem(key: string): string | null | Promise<string | null>
|
|
13
|
+
setItem(key: string, value: string): void | Promise<void>
|
|
14
|
+
removeItem(key: string): void | Promise<void>
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Capacitor Preferences in a native app, `localStorage` itself in a browser. */
|
|
18
|
+
export declare function createNativeStorage(config?: {
|
|
19
|
+
plugin?: PreferencesPlugin
|
|
20
|
+
}): NativeStorage | Storage | undefined
|