@longline/aqua-ui 1.0.16 → 1.0.17
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/package.json +1 -1
- package/services/Auth/Auth.d.ts +26 -0
- package/services/Auth/Auth.js +63 -0
- package/services/Auth/AuthContext.d.ts +11 -0
- package/services/Auth/AuthContext.js +3 -0
- package/services/Auth/index.d.ts +2 -0
- package/services/Auth/index.js +2 -0
- package/services/Auth/useAuth.d.ts +5 -0
- package/services/Auth/useAuth.js +9 -0
package/package.json
CHANGED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
interface IProps {
|
|
3
|
+
children?: React.ReactNode;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* `Auth` serves as an authentication and authorization context. It wraps an application,
|
|
7
|
+
* providing it with the `useAuth` hook. This hook is used to determine if a user is currently
|
|
8
|
+
* authenticated, and redirect to a different route if necessary.
|
|
9
|
+
*
|
|
10
|
+
* Wrap an application:
|
|
11
|
+
*
|
|
12
|
+
* ```tsx
|
|
13
|
+
* <Auth>
|
|
14
|
+
* ...
|
|
15
|
+
* </Auth>
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* In the application, use `useAuth`:
|
|
19
|
+
*
|
|
20
|
+
* ```tsx
|
|
21
|
+
* const auth = useAuth();
|
|
22
|
+
* auth.signin("access", "refresh");
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
declare const Auth: (props: IProps) => React.JSX.Element;
|
|
26
|
+
export { Auth };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
|
2
|
+
__assign = Object.assign || function(t) {
|
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
+
s = arguments[i];
|
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
+
t[p] = s[p];
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
return __assign.apply(this, arguments);
|
|
11
|
+
};
|
|
12
|
+
import * as React from 'react';
|
|
13
|
+
import { AuthContext } from './AuthContext';
|
|
14
|
+
/**
|
|
15
|
+
* `Auth` serves as an authentication and authorization context. It wraps an application,
|
|
16
|
+
* providing it with the `useAuth` hook. This hook is used to determine if a user is currently
|
|
17
|
+
* authenticated, and redirect to a different route if necessary.
|
|
18
|
+
*
|
|
19
|
+
* Wrap an application:
|
|
20
|
+
*
|
|
21
|
+
* ```tsx
|
|
22
|
+
* <Auth>
|
|
23
|
+
* ...
|
|
24
|
+
* </Auth>
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* In the application, use `useAuth`:
|
|
28
|
+
*
|
|
29
|
+
* ```tsx
|
|
30
|
+
* const auth = useAuth();
|
|
31
|
+
* auth.signin("access", "refresh");
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
var Auth = function (props) {
|
|
35
|
+
var getInitialState = function () {
|
|
36
|
+
// Try to load Auth from session storage.
|
|
37
|
+
var authStr = sessionStorage.getItem('auth');
|
|
38
|
+
var authData = authStr ? JSON.parse(authStr) : {};
|
|
39
|
+
var initialState = {
|
|
40
|
+
access_token: null,
|
|
41
|
+
refresh_token: null
|
|
42
|
+
};
|
|
43
|
+
initialState = __assign(__assign({}, initialState), authData);
|
|
44
|
+
return initialState;
|
|
45
|
+
};
|
|
46
|
+
var _a = React.useState(getInitialState()), data = _a[0], setData = _a[1];
|
|
47
|
+
var signin = function (access_token, refresh_token) {
|
|
48
|
+
var newData = {
|
|
49
|
+
access_token: access_token,
|
|
50
|
+
refresh_token: refresh_token
|
|
51
|
+
};
|
|
52
|
+
setData(newData);
|
|
53
|
+
// Store auth in session:
|
|
54
|
+
sessionStorage.setItem('auth', JSON.stringify(newData));
|
|
55
|
+
};
|
|
56
|
+
var signout = function () {
|
|
57
|
+
setData({ access_token: null, refresh_token: null });
|
|
58
|
+
// Remove auth from session:
|
|
59
|
+
sessionStorage.removeItem('auth');
|
|
60
|
+
};
|
|
61
|
+
return (React.createElement(AuthContext.Provider, { value: __assign({ signin: signin, signout: signout }, data) }, props.children));
|
|
62
|
+
};
|
|
63
|
+
export { Auth };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
interface IAuth {
|
|
3
|
+
access_token: string;
|
|
4
|
+
refresh_token: string;
|
|
5
|
+
}
|
|
6
|
+
interface IAuthContext extends IAuth {
|
|
7
|
+
signin: (access_token: string, refresh_token: string) => void;
|
|
8
|
+
signout: () => void;
|
|
9
|
+
}
|
|
10
|
+
declare const AuthContext: React.Context<IAuthContext>;
|
|
11
|
+
export { AuthContext, IAuth, IAuthContext };
|