@licklist/design 0.78.5-dev.38 → 0.78.5-dev.39
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Authorizer.d.ts","sourceRoot":"","sources":["../../src/auth/Authorizer.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,
|
|
1
|
+
{"version":3,"file":"Authorizer.d.ts","sourceRoot":"","sources":["../../src/auth/Authorizer.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAwD,MAAM,OAAO,CAAA;AAYvF,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,EAAE,SAAS,GAAG,SAAS,EAAE,CAAA;CACnC;AAID,iBAAS,UAAU,CAAC,KAAK,EAAE,eAAe,2CAyGzC;AAED,OAAO,EAAE,UAAU,EAAE,CAAA"}
|
package/dist/auth/Authorizer.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { jsx, Fragment } from 'react/jsx-runtime';
|
|
2
|
-
import { useState, useCallback, useEffect } from 'react';
|
|
2
|
+
import { useContext, useRef, useState, useCallback, useEffect } from 'react';
|
|
3
3
|
import { useLocation, useNavigate, matchPath } from 'react-router-dom';
|
|
4
4
|
import useUser from '@licklist/plugins/dist/context/user/hooks/useUser';
|
|
5
5
|
import { MATCH_ROUTE_PATTERNS, ROUTES } from '@licklist/core/dist/Config';
|
|
6
6
|
import useAuthApi from '@licklist/plugins/dist/hooks/Api/useAuthApi';
|
|
7
7
|
import useAuth from '@licklist/plugins/dist/context/user/hooks/useAuth';
|
|
8
8
|
import RouteService from '@licklist/plugins/dist/services/Route/RouteService';
|
|
9
|
+
import StorageContext from '@licklist/plugins/dist/context/app/StorageContext';
|
|
10
|
+
import JsonService from '@licklist/plugins/dist/services/Json/JsonService';
|
|
9
11
|
import { BlockLoader } from '../static/loader/BlockLoader.js';
|
|
10
12
|
|
|
11
13
|
function _array_like_to_array(arr, len) {
|
|
@@ -66,11 +68,14 @@ function _unsupported_iterable_to_array(o, minLen) {
|
|
|
66
68
|
if (n === "Map" || n === "Set") return Array.from(n);
|
|
67
69
|
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
|
|
68
70
|
}
|
|
71
|
+
var AUTH_CONTEXT_KEY = 'auth';
|
|
69
72
|
function Authorizer(props) {
|
|
70
73
|
var children = props.children;
|
|
71
74
|
var auth = useAuth();
|
|
72
75
|
var user = useUser();
|
|
73
76
|
var useProfile = useAuthApi().useProfile;
|
|
77
|
+
var storage = useContext(StorageContext);
|
|
78
|
+
var isLoggingOut = useRef(false);
|
|
74
79
|
var pathname = useLocation().pathname;
|
|
75
80
|
var navigate = useNavigate();
|
|
76
81
|
// This will re-fetch user's profile
|
|
@@ -85,6 +90,32 @@ function Authorizer(props) {
|
|
|
85
90
|
return matched.pathname;
|
|
86
91
|
});
|
|
87
92
|
}, []);
|
|
93
|
+
// Check auth on every route change - if storage was cleared, logout immediately
|
|
94
|
+
useEffect(function() {
|
|
95
|
+
if (isLoggingOut.current) return;
|
|
96
|
+
var storedAuth = storage.get(AUTH_CONTEXT_KEY);
|
|
97
|
+
var parsedAuth = storedAuth ? JsonService.decode(storedAuth) : null;
|
|
98
|
+
// If we have auth in memory but not in storage, session was cleared
|
|
99
|
+
if (auth.secrets !== null && !parsedAuth) {
|
|
100
|
+
isLoggingOut.current = true;
|
|
101
|
+
auth.setSecrets(null);
|
|
102
|
+
user.setProfile(null);
|
|
103
|
+
var route = RouteService.getCurrentRoute();
|
|
104
|
+
navigate([
|
|
105
|
+
ROUTES.LOGIN_PAGE,
|
|
106
|
+
"returnUrl=".concat(escape(route))
|
|
107
|
+
].join('?'), {
|
|
108
|
+
replace: true
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}, [
|
|
112
|
+
pathname,
|
|
113
|
+
auth.secrets,
|
|
114
|
+
storage,
|
|
115
|
+
navigate,
|
|
116
|
+
auth,
|
|
117
|
+
user
|
|
118
|
+
]);
|
|
88
119
|
useEffect(function() {
|
|
89
120
|
if (profile.isLoading || profile.isFetching) {
|
|
90
121
|
return;
|
package/package.json
CHANGED
package/src/auth/Authorizer.tsx
CHANGED
|
@@ -1,21 +1,28 @@
|
|
|
1
|
-
import { ReactNode, useEffect, useState, useCallback } from 'react'
|
|
1
|
+
import { ReactNode, useEffect, useState, useCallback, useContext, useRef } from 'react'
|
|
2
2
|
import { matchPath, useLocation, useNavigate } from 'react-router-dom'
|
|
3
3
|
import useUser from '@licklist/plugins/dist/context/user/hooks/useUser'
|
|
4
4
|
import { ROUTES, MATCH_ROUTE_PATTERNS } from '@licklist/core/dist/Config'
|
|
5
5
|
import useAuthApi from '@licklist/plugins/dist/hooks/Api/useAuthApi'
|
|
6
6
|
import useAuth from '@licklist/plugins/dist/context/user/hooks/useAuth'
|
|
7
7
|
import RouteService from '@licklist/plugins/dist/services/Route/RouteService'
|
|
8
|
+
import StorageContext from '@licklist/plugins/dist/context/app/StorageContext'
|
|
9
|
+
import JsonService from '@licklist/plugins/dist/services/Json/JsonService'
|
|
10
|
+
import { IAuth } from '@licklist/plugins/dist/types/context/user/IAuth'
|
|
8
11
|
import { BlockLoader } from '../static/loader/BlockLoader'
|
|
9
12
|
|
|
10
13
|
export interface AuthorizerProps {
|
|
11
14
|
children?: ReactNode | ReactNode[]
|
|
12
15
|
}
|
|
13
16
|
|
|
17
|
+
const AUTH_CONTEXT_KEY = 'auth'
|
|
18
|
+
|
|
14
19
|
function Authorizer(props: AuthorizerProps) {
|
|
15
20
|
const { children } = props
|
|
16
21
|
const auth = useAuth()
|
|
17
22
|
const user = useUser()
|
|
18
23
|
const { useProfile } = useAuthApi()
|
|
24
|
+
const storage = useContext(StorageContext)
|
|
25
|
+
const isLoggingOut = useRef(false)
|
|
19
26
|
|
|
20
27
|
const { pathname } = useLocation()
|
|
21
28
|
const navigate = useNavigate()
|
|
@@ -41,6 +48,24 @@ function Authorizer(props: AuthorizerProps) {
|
|
|
41
48
|
[],
|
|
42
49
|
)
|
|
43
50
|
|
|
51
|
+
// Check auth on every route change - if storage was cleared, logout immediately
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
if (isLoggingOut.current) return
|
|
54
|
+
|
|
55
|
+
const storedAuth = storage.get(AUTH_CONTEXT_KEY)
|
|
56
|
+
const parsedAuth = storedAuth ? JsonService.decode<IAuth>(storedAuth) : null
|
|
57
|
+
|
|
58
|
+
// If we have auth in memory but not in storage, session was cleared
|
|
59
|
+
if (auth.secrets !== null && !parsedAuth) {
|
|
60
|
+
isLoggingOut.current = true
|
|
61
|
+
auth.setSecrets(null)
|
|
62
|
+
user.setProfile(null)
|
|
63
|
+
|
|
64
|
+
const route = RouteService.getCurrentRoute()
|
|
65
|
+
navigate([ROUTES.LOGIN_PAGE, `returnUrl=${escape(route)}`].join('?'), { replace: true })
|
|
66
|
+
}
|
|
67
|
+
}, [pathname, auth.secrets, storage, navigate, auth, user])
|
|
68
|
+
|
|
44
69
|
useEffect(() => {
|
|
45
70
|
if (profile.isLoading || profile.isFetching) {
|
|
46
71
|
return
|