Skip to content

Commit 74b4431

Browse files
committed
Commit
1 parent b228f19 commit 74b4431

17 files changed

+510
-325
lines changed

lib/msal-native-auth/doc/tech-design/Tech Design.md

+32-14
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,27 @@ async function handleSignIn(event) {
5757

5858
switch (result.state) {
5959
case SignInState.Completed:
60+
// read the account info from result by result.data and use it to get account data, tokens, and sign out.
6061
accountInfo = result.data;
6162
const accessToken = await accountInfo.getAccessToken();
6263
fetchProfile(accessToken);
64+
break;
6365
case SignInState.CodeRequired:
6466
navigateToCodeEntryView(result.stateHandler);
67+
break;
6568
case SignInState.PasswordRequired:
6669
navigateToPasswordEntryView(result.stateHandler);
70+
break;
71+
case SignInState.Failed:
72+
// check the error type by calling result.error and handle error
73+
if (result.error instanceof UserNotFoundError) {
74+
// Handle user not found error
75+
} else {
76+
// Handle unexpected error
77+
}
78+
break;
79+
default:
80+
throw new Error("Invalid sign in state");
6781
}
6882
}
6983

@@ -73,14 +87,16 @@ async function submitCode(handler: SignInCodeRequiredStateHandler) {
7387

7488
const result = handler.submitCode(code);
7589

76-
// Handling the error if the action is failed
77-
if (!result.isSuccess) {
78-
return;
90+
switch (result.state) {
91+
case SignInState.Completed:
92+
// read the account info from result by result.data and use it to get account data, tokens, and sign out.
93+
break;
94+
case SignInState.Failed:
95+
// check the error type by calling result.error and handle error
96+
break;
97+
default:
98+
throw new Error("Invalid sign in state");
7999
}
80-
81-
accountInfo = result.data;
82-
const accessToken = await accountInfo.getAccessToken();
83-
fetchProfile(accessToken);
84100
}
85101

86102
// In the Password Entry UI
@@ -89,14 +105,16 @@ async function submitPassword(handler: SignInPasswordRequiredStateHandler) {
89105

90106
const result = handler.submitPassword(password);
91107

92-
// Handling the error if the action is failed
93-
if (!result.isSuccess) {
94-
return;
108+
switch (result.state) {
109+
case SignInState.Completed:
110+
// read the account info from result by result.data and use it to get account data, tokens, and sign out.
111+
break;
112+
case SignInState.Failed:
113+
// check the error type by calling result.error and handle error
114+
break;
115+
default:
116+
throw new Error("Invalid sign in state");
95117
}
96-
97-
accountInfo = result.data;
98-
const accessToken = await accountInfo.getAccessToken();
99-
fetchProfile(accessToken);
100118
}
101119
```
102120

lib/msal-native-auth/samples/SignInSample.ts

+47-114
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@
44
*/
55

66
import {
7-
SignInCodeRequiredState,
8-
SignInPasswordRequiredState,
9-
UserNotFoundError,
7+
SignInCodeRequiredStateHandler,
8+
SignInPasswordRequiredStateHandler,
9+
SignInState,
1010
} from "@azure/msal-native-auth";
11-
import { AccountInfo } from "@azure/msal-native-auth";
1211
import { SignInInputs } from "@azure/msal-native-auth";
1312
import { NativeAuthConfiguration } from "@azure/msal-native-auth";
1413
import { NativeAuthPublicClientApplication } from "@azure/msal-native-auth";
1514

1615
// This sample demonstrates how to sign in a user using the MSAL Native Auth library.
17-
// Currently, this sample doesn't work and only is used to demonstrate the usage of the library.
16+
// Currently, this sample doesn't work and is only used to demonstrate the usage of the library.
1817
export async function signin(
1918
username: string,
2019
password?: string
@@ -28,120 +27,54 @@ export async function signin(
2827

2928
const signInOptions: SignInInputs = {
3029
username: username,
31-
password: password,
3230
};
3331

3432
const result = await app.signIn(signInOptions);
3533

36-
if (!result.isSuccess) {
37-
// check the errr type and handle error
38-
39-
if (result.error instanceof UserNotFoundError) {
40-
// handle user not found error
41-
} else {
42-
// handle unexpected error
43-
}
44-
45-
return;
46-
}
47-
48-
// Check if the flow is completed
49-
if (result.isFlowCompleted()) {
50-
// Get the account info which can be used to get account data, tokens, and sign out.
51-
const accountManager: AccountInfo = result.data as AccountInfo;
52-
53-
accountManager.getAccount();
54-
accountManager.getIdToken();
55-
await accountManager.getAccessToken();
56-
await accountManager.signOut();
57-
58-
return;
59-
}
60-
61-
// code required
62-
if (result.state instanceof SignInCodeRequiredState) {
63-
// collect code from customer.
64-
const code = "test-code";
65-
66-
const submitCodeResult = await result.state.submitCode(code);
67-
68-
if (!submitCodeResult.isSuccess) {
69-
// handle error
70-
34+
switch (result.state) {
35+
case SignInState.Completed:
36+
// read the account info from result by result.data and use it to get account data, tokens, and sign out.
7137
return;
72-
}
73-
74-
// Get the account manager which can be used to get account information, tokens, and sign out.
75-
const accountManager: AccountInfo =
76-
submitCodeResult.data as AccountInfo;
77-
78-
accountManager.getAccount();
79-
accountManager.getIdToken();
80-
await accountManager.getAccessToken();
81-
await accountManager.signOut();
82-
83-
return;
84-
}
85-
86-
// resend code and submit code
87-
if (result.state instanceof SignInCodeRequiredState) {
88-
// resend code
89-
const resendCodeResult = await result.state.resendCode();
90-
91-
if (!resendCodeResult.isSuccess) {
92-
// handle error
93-
38+
case SignInState.CodeRequired:
39+
// collect code from customer.
40+
const code = "test-code";
41+
42+
const submitCodeResult = await (
43+
result.stateHandler as SignInCodeRequiredStateHandler
44+
).submitCode(code);
45+
46+
switch (submitCodeResult.state) {
47+
case SignInState.Completed:
48+
// read the account info from result by submitCodeResult.data and use it to get account data, tokens, and sign out.
49+
return;
50+
case SignInState.Failed:
51+
// check the error type by calling result.error and handle error
52+
break;
53+
default:
54+
throw new Error("Invalid sign in state");
55+
}
56+
case SignInState.PasswordRequired:
57+
// collect password from customer.
58+
const password = "test-pwd";
59+
60+
const submitPasswordResult = await (
61+
result.stateHandler as SignInPasswordRequiredStateHandler
62+
).sumbmitPassword(password);
63+
64+
switch (submitPasswordResult.state) {
65+
case SignInState.Completed:
66+
// read the account info from result by submitPasswordResult.data and use it to get account data, tokens, and sign out.
67+
return;
68+
case SignInState.Failed:
69+
// check the error type by calling result.error and handle error
70+
break;
71+
default:
72+
throw new Error("Invalid sign in state");
73+
}
74+
case SignInState.Failed:
75+
// check the error type by calling result.error and handle error
9476
return;
95-
}
96-
97-
// collect code from customer.
98-
const code = "test-code";
99-
100-
const submitCodeResult = await (
101-
resendCodeResult.state as SignInCodeRequiredState
102-
).submitCode(code);
103-
104-
if (!submitCodeResult.isSuccess) {
105-
// handle error
106-
107-
return;
108-
}
109-
110-
// Get the account manager which can be used to get account information, tokens, and sign out.
111-
const accountManager: AccountInfo =
112-
submitCodeResult.data as AccountInfo;
113-
114-
accountManager.getAccount();
115-
accountManager.getIdToken();
116-
await accountManager.getAccessToken();
117-
await accountManager.signOut();
118-
119-
return;
120-
}
121-
122-
// password required
123-
if (result.state instanceof SignInPasswordRequiredState) {
124-
// collect password from customer.
125-
const pwd = "test-password";
126-
const submitPasswordResult = await result.state.sumbmitPassword(pwd);
127-
128-
if (!submitPasswordResult.isSuccess) {
129-
// handle error
130-
131-
return;
132-
}
133-
134-
// Get the account manager which can be used to get account information, tokens, and sign out.
135-
const accountManager: AccountInfo =
136-
submitPasswordResult.data as AccountInfo;
137-
138-
accountManager.getAccount();
139-
accountManager.getIdToken();
140-
await accountManager.getAccessToken();
141-
await accountManager.signOut();
142-
143-
return;
77+
default:
78+
throw new Error("Invalid sign in state");
14479
}
14580
}
146-
147-
console.log("Starting sign in sample...");

lib/msal-native-auth/samples/lib/msal-native-auth.samples.js

+37-73
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Licensed under the MIT License.
1111
*/
1212
// This sample demonstrates how to sign in a user using the MSAL Native Auth library.
13-
// Currently, this sample doesn't work and only is used to demonstrate the usage of the library.
13+
// Currently, this sample doesn't work and is only used to demonstrate the usage of the library.
1414
async function signin(username, password) {
1515
const config = {
1616
auth: { clientId: "test-client-id" },
@@ -19,86 +19,50 @@
1919
const app = msalNativeAuth.NativeAuthPublicClientApplication.create(config);
2020
const signInOptions = {
2121
username: username,
22-
password: password,
2322
};
2423
const result = await app.signIn(signInOptions);
25-
if (!result.isSuccess) {
26-
// check the errr type and handle error
27-
if (result.error instanceof msalNativeAuth.UserNotFoundError) ;
28-
return;
29-
}
30-
// Check if the flow is completed
31-
if (result.isFlowCompleted()) {
32-
// Get the account info which can be used to get account data, tokens, and sign out.
33-
const accountManager = result.resultData;
34-
accountManager.getAccount();
35-
accountManager.getIdToken();
36-
await accountManager.getAccessToken();
37-
await accountManager.signOut();
38-
return;
39-
}
40-
// code required
41-
if (result.nextStateHandler instanceof msalNativeAuth.SignInCodeRequiredState) {
42-
// collect code from customer.
43-
const code = "test-code";
44-
const submitCodeResult = await result.nextStateHandler.submitCode(code);
45-
if (!submitCodeResult.isSuccess) {
46-
// handle error
47-
return;
48-
}
49-
// Get the account manager which can be used to get account information, tokens, and sign out.
50-
const accountManager = submitCodeResult.resultData;
51-
accountManager.getAccount();
52-
accountManager.getIdToken();
53-
await accountManager.getAccessToken();
54-
await accountManager.signOut();
55-
return;
56-
}
57-
// resend code and submit code
58-
if (result.nextStateHandler instanceof msalNativeAuth.SignInCodeRequiredState) {
59-
// resend code
60-
const resendCodeResult = await result.nextStateHandler.resendCode();
61-
if (!resendCodeResult.isSuccess) {
62-
// handle error
63-
return;
64-
}
65-
// collect code from customer.
66-
const code = "test-code";
67-
const submitCodeResult = await resendCodeResult.nextStateHandler.submitCode(code);
68-
if (!submitCodeResult.isSuccess) {
69-
// handle error
24+
switch (result.state) {
25+
case msalNativeAuth.SignInState.Completed:
26+
// read the account info from result by result.data and use it to get account data, tokens, and sign out.
7027
return;
71-
}
72-
// Get the account manager which can be used to get account information, tokens, and sign out.
73-
const accountManager = submitCodeResult.resultData;
74-
accountManager.getAccount();
75-
accountManager.getIdToken();
76-
await accountManager.getAccessToken();
77-
await accountManager.signOut();
78-
return;
79-
}
80-
// password required
81-
if (result.nextStateHandler instanceof msalNativeAuth.SignInPasswordRequiredState) {
82-
// collect password from customer.
83-
const pwd = "test-password";
84-
const submitPasswordResult = await result.nextStateHandler.sumbmitPassword(pwd);
85-
if (!submitPasswordResult.isSuccess) {
86-
// handle error
28+
case msalNativeAuth.SignInState.CodeRequired:
29+
// collect code from customer.
30+
const code = "test-code";
31+
const submitCodeResult = await result.stateHandler.submitCode(code);
32+
switch (submitCodeResult.state) {
33+
case msalNativeAuth.SignInState.Completed:
34+
// read the account info from result by submitCodeResult.data and use it to get account data, tokens, and sign out.
35+
return;
36+
case msalNativeAuth.SignInState.Failed:
37+
// check the error type by calling result.error and handle error
38+
break;
39+
default:
40+
throw new Error("Invalid sign in state");
41+
}
42+
case msalNativeAuth.SignInState.PasswordRequired:
43+
// collect password from customer.
44+
const password = "test-pwd";
45+
const submitPasswordResult = await result.stateHandler.sumbmitPassword(password);
46+
switch (submitPasswordResult.state) {
47+
case msalNativeAuth.SignInState.Completed:
48+
// read the account info from result by submitPasswordResult.data and use it to get account data, tokens, and sign out.
49+
return;
50+
case msalNativeAuth.SignInState.Failed:
51+
// check the error type by calling result.error and handle error
52+
break;
53+
default:
54+
throw new Error("Invalid sign in state");
55+
}
56+
case msalNativeAuth.SignInState.Failed:
57+
// check the error type by calling result.error and handle error
8758
return;
88-
}
89-
// Get the account manager which can be used to get account information, tokens, and sign out.
90-
const accountManager = submitPasswordResult.resultData;
91-
accountManager.getAccount();
92-
accountManager.getIdToken();
93-
await accountManager.getAccessToken();
94-
await accountManager.signOut();
95-
return;
59+
default:
60+
throw new Error("Invalid sign in state");
9661
}
9762
}
98-
console.log("Starting sign in sample...");
9963

10064
async function test_signin() {
101-
signin("test-username", "test-password");
65+
signin("test-username");
10266
}
10367
test_signin();
10468

0 commit comments

Comments
 (0)