1- import { IonContent , IonHeader , IonPage , IonTitle , IonToolbar } from '@ionic/ react' ;
1+ import { useContext , useState , useEffect } from 'react' ;
22import ExploreContainer from '../components/ExploreContainer' ;
3+ import { AuthContext } from '../context/auth' ;
4+ import { IonContent , IonHeader , IonPage , IonTitle , IonToolbar , IonButton , IonItemDivider , IonCard , IonCardContent , IonCardHeader , IonCardTitle , IonCardSubtitle , IonList , IonItem , IonLabel } from '@ionic/react' ;
35import './Home.css' ;
6+ import { AuthenticationResult } from '@azure/msal-common' ;
7+
8+
9+
10+ interface IRoles {
11+ key : string ;
12+ values : string [ ] ;
13+
14+ }
415
516const Home : React . FC = ( ) => {
17+
18+ const { acquireAccessTokenForUser, acquireAuthenticationResult } = useContext ( AuthContext ) ;
19+ const [ authenticationResults , setAuthenticationResults ] = useState < AuthenticationResult > ( ) ;
20+ const [ accessToken , setAccessToken ] = useState < string > ( "" ) ;
21+
22+ useEffect ( ( ) => {
23+ handleAuthResults ( ) ;
24+ } , [ ] )
25+
26+ const handleAuthResults = async ( ) => {
27+ try {
28+ let results = await acquireAuthenticationResult ( ) ;
29+
30+ if ( results ) {
31+ setAuthenticationResults ( results as AuthenticationResult ) ;
32+ console . log ( results ) ;
33+ }
34+ }
35+ catch ( error ) {
36+ console . log ( error ) ;
37+ }
38+ }
39+ const handleAccessToken = async ( ) => {
40+ try {
41+ let token = await acquireAccessTokenForUser ( [
42+ 'https://func-auth-a.azurewebsites.net/user_impersonation'
43+ ] ) ;
44+
45+
46+
47+ if ( token ) {
48+ setAccessToken ( token as string ) ;
49+ console . log ( token ) ;
50+ }
51+ }
52+ catch ( error ) {
53+ console . log ( error ) ;
54+ }
55+ }
56+
57+ const getProperty = < T , K extends keyof T > ( o ?: T , propertyName ?: K ) : T [ K ] => {
58+
59+ if ( o && propertyName ) {
60+ return o [ propertyName ] ;
61+ } else {
62+ throw Error
63+ }
64+ // o[propertyName] is of type T[K]
65+ }
66+
667 return (
768 < IonPage >
869 < IonHeader >
@@ -16,7 +77,51 @@ const Home: React.FC = () => {
1677 < IonTitle size = "large" > Blank</ IonTitle >
1778 </ IonToolbar >
1879 </ IonHeader >
19- < ExploreContainer />
80+ < IonCard >
81+ < IonCardHeader >
82+ < IonCardTitle > Auth Results</ IonCardTitle >
83+ < IonCardSubtitle class = "ion-text-nowrap" > Tenant: { authenticationResults ?. tenantId } </ IonCardSubtitle >
84+ < IonCardSubtitle class = "ion-text-nowrap" > Unique Id: { authenticationResults ?. uniqueId } </ IonCardSubtitle >
85+ < IonCardSubtitle class = "ion-text-nowrap" > Username: { authenticationResults ?. account ?. username } </ IonCardSubtitle >
86+ { /* <IonCardSubtitle>{authenticationResults.}</IonCardSubtitle> */ }
87+ </ IonCardHeader >
88+ < IonCardContent >
89+ < IonList >
90+
91+ < IonItem >
92+ < IonLabel > { ( Object . entries ( authenticationResults ?. idTokenClaims ?? { } ) . filter ( ( [ x , y ] ) => x === "roles" ) . values ( ) ) } </ IonLabel >
93+ </ IonItem >
94+
95+ {
96+ Object . entries ( authenticationResults ?. account ?. idTokenClaims ?? { } ) . map ( ( [ key , value ] ) => {
97+ if ( typeof ( value ) === typeof ( Object ) ) {
98+ Object . entries ( value ) . map ( ( [ key , value ] ) => {
99+ return (
100+ < IonItem >
101+ < IonLabel > { typeof ( value ) } : { key } - { value } </ IonLabel >
102+ </ IonItem >
103+ )
104+ } )
105+ } else {
106+ return (
107+ < IonItem >
108+ < IonLabel > { typeof ( value ) } : { key } - { value } </ IonLabel >
109+ </ IonItem >
110+ )
111+ }
112+ } )
113+ }
114+ </ IonList >
115+
116+ </ IonCardContent >
117+ </ IonCard >
118+ < div >
119+ < p > { authenticationResults ?. account ?. username } </ p >
120+ < IonItemDivider />
121+ < IonButton onClick = { handleAccessToken } expand = "block" > Get Token</ IonButton >
122+ < IonItemDivider />
123+ < p > < small > { accessToken } </ small > </ p >
124+ </ div >
20125 </ IonContent >
21126 </ IonPage >
22127 ) ;
0 commit comments