1
1
/* eslint-disable @silverhand/fp/no-mutation */
2
- import { baseUrl , redirectUrl } from '../consts' ;
3
-
4
- const Home = async ( app , logtoClient ) => {
5
- if ( ! logtoClient ) {
6
- throw new Error ( 'no logto client found' ) ;
7
- }
8
-
9
- // eslint-disable-next-line @silverhand/fp/no-let
10
- let isAuthenticated = false ;
11
- const onClickSignIn = ( ) => logtoClient . signIn ( redirectUrl ) ;
12
- const onClickSignOut = ( ) => logtoClient . signOut ( baseUrl ) ;
13
-
14
- ( async ( ) => {
15
- isAuthenticated = await logtoClient . isAuthenticated ( ) ;
16
- renderButton ( container , { isAuthenticated, onClickSignIn, onClickSignOut } ) ;
17
-
18
- if ( isAuthenticated ) {
19
- const userInfo = await logtoClient . fetchUserInfo ( ) ;
20
- renderTable ( container , { userInfo } ) ;
21
- }
22
- } ) ( ) ;
23
-
24
- const fragment = document . createDocumentFragment ( ) ;
25
-
26
- const container = document . createElement ( 'div' ) ;
27
- container . classList . add ( 'container' ) ;
28
-
29
- const h3 = document . createElement ( 'h3' ) ;
30
- h3 . innerHTML = 'Logto Browser Sample' ;
31
-
32
- container . append ( h3 ) ;
33
- renderButton ( container , { isAuthenticated, onClickSignIn, onClickSignOut } ) ;
34
-
35
- fragment . append ( container ) ;
36
- app . append ( fragment ) ;
37
- } ;
2
+ import { baseUrl , redirectUrl , resourceIndicators } from '../consts' ;
38
3
39
4
const renderButton = ( container , states ) => {
40
5
const { isAuthenticated, onClickSignIn, onClickSignOut } = states ;
@@ -67,6 +32,7 @@ const renderTable = (container, states) => {
67
32
table . append ( thead ) ;
68
33
69
34
const tbody = document . createElement ( 'tbody' ) ;
35
+
70
36
for ( const [ key , value ] of Object . entries ( userInfo ) ) {
71
37
const tr = document . createElement ( 'tr' ) ;
72
38
const tdName = document . createElement ( 'td' ) ;
@@ -81,5 +47,100 @@ const renderTable = (container, states) => {
81
47
container . append ( table ) ;
82
48
} ;
83
49
50
+ const renderTokenTable = ( container , logtoClient , userInfo ) => {
51
+ const table = document . createElement ( 'table' ) ;
52
+
53
+ const thead = document . createElement ( 'thead' ) ;
54
+ const tr = document . createElement ( 'tr' ) ;
55
+ const thName = document . createElement ( 'th' ) ;
56
+ const thValue = document . createElement ( 'th' ) ;
57
+ const thAction = document . createElement ( 'th' ) ;
58
+
59
+ thName . innerHTML = 'TokenType' ;
60
+ thValue . innerHTML = 'Value' ;
61
+ thAction . innerHTML = 'GetToken' ;
62
+
63
+ tr . append ( thName , thValue , thAction ) ;
64
+ thead . append ( tr ) ;
65
+ table . append ( thead ) ;
66
+
67
+ const tbody = document . createElement ( 'tbody' ) ;
68
+
69
+ const renderTokenRow = ( name , value , action ) => {
70
+ const tr = document . createElement ( 'tr' ) ;
71
+ const tdName = document . createElement ( 'td' ) ;
72
+ const tdValue = document . createElement ( 'td' ) ;
73
+ const tdAction = document . createElement ( 'td' ) ;
74
+
75
+ tdName . innerHTML = name ;
76
+ tdValue . innerHTML = value ;
77
+
78
+ const getTokenButton = document . createElement ( 'button' ) ;
79
+ getTokenButton . innerHTML = 'Get Token' ;
80
+ tdAction . append ( getTokenButton ) ;
81
+
82
+ getTokenButton . addEventListener ( 'click' , async ( ) => {
83
+ const claims = await action ( ) ;
84
+ tdValue . innerHTML = JSON . stringify ( claims ) ;
85
+ } ) ;
86
+
87
+ tr . append ( tdName , tdValue , tdAction ) ;
88
+ tbody . append ( tr ) ;
89
+ } ;
90
+
91
+ // Get resource access token
92
+ for ( const resource of resourceIndicators ) {
93
+ renderTokenRow ( resource , '' , ( ) => logtoClient . getAccessTokenClaims ( resource ) ) ;
94
+ }
95
+
96
+ // Get Organization access token
97
+ const { organizations } = userInfo ;
98
+ if ( organizations ) {
99
+ for ( const organization of organizations ) {
100
+ renderTokenRow ( organization , '' , ( ) => logtoClient . getOrganizationTokenClaims ( organization ) ) ;
101
+ }
102
+ }
103
+
104
+ table . append ( tbody ) ;
105
+ container . append ( table ) ;
106
+ } ;
107
+
108
+ const render = async ( container , logtoClient ) => {
109
+ const onClickSignIn = ( ) => logtoClient . signIn ( redirectUrl ) ;
110
+ const onClickSignOut = ( ) => logtoClient . signOut ( baseUrl ) ;
111
+
112
+ const isAuthenticated = await logtoClient . isAuthenticated ( ) ;
113
+
114
+ renderButton ( container , { isAuthenticated, onClickSignIn, onClickSignOut } ) ;
115
+
116
+ if ( isAuthenticated ) {
117
+ const userInfo = await logtoClient . fetchUserInfo ( ) ;
118
+ renderTable ( container , { userInfo } ) ;
119
+ renderTokenTable ( container , logtoClient , userInfo ) ;
120
+ }
121
+ } ;
122
+
123
+ const Home = ( app , logtoClient ) => {
124
+ if ( ! logtoClient ) {
125
+ throw new Error ( 'no logto client found' ) ;
126
+ }
127
+
128
+ const fragment = document . createDocumentFragment ( ) ;
129
+
130
+ const container = document . createElement ( 'div' ) ;
131
+ container . classList . add ( 'container' ) ;
132
+
133
+ const h3 = document . createElement ( 'h3' ) ;
134
+ h3 . innerHTML = 'Logto Browser Sample' ;
135
+ container . append ( h3 ) ;
136
+
137
+ fragment . append ( container ) ;
138
+ app . append ( fragment ) ;
139
+
140
+ ( async ( ) => {
141
+ await render ( container , logtoClient ) ;
142
+ } ) ( ) ;
143
+ } ;
144
+
84
145
export default Home ;
85
146
/* eslint-enable @silverhand/fp/no-mutation */
0 commit comments