@inglorious/web 6.26.1 → 6.26.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.
- package/README.md +30 -23
- package/package.json +1 -1
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
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/web",
|
|
3
|
-
"version": "6.26.
|
|
3
|
+
"version": "6.26.2",
|
|
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": [
|