Skip to content
This repository was archived by the owner on Feb 11, 2023. It is now read-only.

Commit 3b42a12

Browse files
author
Chase Crawford
committed
Added changes
1 parent 23918e0 commit 3b42a12

File tree

14 files changed

+232
-19
lines changed

14 files changed

+232
-19
lines changed

.DS_Store

-6 KB
Binary file not shown.

src/.DS_Store

-6 KB
Binary file not shown.

src/.vs/slnx.sqlite

88 KB
Binary file not shown.

src/.vs/slnx.sqlite-journal

28.6 KB
Binary file not shown.

src/msal-capacitor-plugin-demo/package-lock.json

Lines changed: 84 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/msal-capacitor-plugin-demo/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"@types/react-router": "^5.1.11",
2020
"@types/react-router-dom": "^5.1.7",
2121
"ionicons": "^5.4.0",
22+
"jsonwebtoken": "^8.5.1",
2223
"react": "^17.0.1",
2324
"react-dom": "^17.0.1",
2425
"react-router": "^5.2.0",

src/msal-capacitor-plugin-demo/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const App: React.FC = () => {
2222

2323
const authProvider = useAuthentication();
2424
const [isAuthenticated, setIsAuthenticated] = useState<boolean>(false);
25-
25+
const [accessToken, setAccessToken] = useState<string>("");
2626

2727
useEffect(()=>{
2828
handleLogin();

src/msal-capacitor-plugin-demo/src/context/auth/AuthContext.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ export const useAuthentication = (): IAuthContext => {
2424
let uri = isPlatform('capacitor') ? 'msauth.{iOS MAC redirect Url}://auth' : 'http://localhost:3000';
2525
console.log(uri);
2626
(await MsalCap.setOptions({
27-
clientId: 'Client ID',
28-
authority: `https://login.microsoftonline.com/{Tenant ID}`,
27+
clientId: '6b51f8a2-d03d-4d86-b753-87b45b89d794',
28+
authority: `https://login.microsoftonline.com/29967363-a86a-4ea6-8f76-29aa44ec6f27`,
2929
redirectUri: uri,
3030
scopes: [
3131
'user.read'

src/msal-capacitor-plugin-demo/src/pages/Home.tsx

Lines changed: 107 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,69 @@
1-
import { IonContent, IonHeader, IonPage, IonTitle, IonToolbar } from '@ionic/react';
1+
import { useContext, useState, useEffect } from 'react';
22
import 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';
35
import './Home.css';
6+
import { AuthenticationResult } from '@azure/msal-common';
7+
8+
9+
10+
interface IRoles {
11+
key: string;
12+
values: string[];
13+
14+
}
415

516
const 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
);

src/msal-capacitor-plugin/ios/Plugin/Plugin.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public class MsalPlugin: CAPPlugin {
2222

2323
// This will act as a constructor for initializing
2424
public override func load() {
25-
2625
self.dateFormatter.dateFormat = "YY-MM-dd'T'HH:mm:ss"
2726
}
2827

0 commit comments

Comments
 (0)