4
4
*/
5
5
6
6
import {
7
- SignInCodeRequiredState ,
8
- SignInPasswordRequiredState ,
9
- UserNotFoundError ,
7
+ SignInCodeRequiredStateHandler ,
8
+ SignInPasswordRequiredStateHandler ,
9
+ SignInState ,
10
10
} from "@azure/msal-native-auth" ;
11
- import { AccountInfo } from "@azure/msal-native-auth" ;
12
11
import { SignInInputs } from "@azure/msal-native-auth" ;
13
12
import { NativeAuthConfiguration } from "@azure/msal-native-auth" ;
14
13
import { NativeAuthPublicClientApplication } from "@azure/msal-native-auth" ;
15
14
16
15
// 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.
18
17
export async function signin (
19
18
username : string ,
20
19
password ?: string
@@ -28,120 +27,54 @@ export async function signin(
28
27
29
28
const signInOptions : SignInInputs = {
30
29
username : username ,
31
- password : password ,
32
30
} ;
33
31
34
32
const result = await app . signIn ( signInOptions ) ;
35
33
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.
71
37
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
94
76
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" ) ;
144
79
}
145
80
}
146
-
147
- console . log ( "Starting sign in sample..." ) ;
0 commit comments