/
main.dart
118 lines (109 loc) · 3.89 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_azure_b2c/flutter_azure_b2c.dart';
import 'package:flutter_azure_b2c/B2COperationResult.dart';
import 'package:flutter_azure_b2c/B2CConfiguration.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _retdata = "";
List<String>? _subjects;
B2CConfiguration? _configuration;
@override
void initState() {
super.initState();
// It is possible to register callbacks in order to handle return values
// from asynchronous calls to the plugin
AzureB2C.registerCallback(B2COperationSource.INIT, (result) async {
if (result.reason == B2COperationState.SUCCESS) {
_configuration = await AzureB2C.getConfiguration();
}
});
// Important: Remeber to handle redirect states (if you want to support
// the web platform with redirect method) and init the AzureB2C plugin
// before the material app starts.
AzureB2C.handleRedirectFuture().then((_) => AzureB2C.init("auth_config"));
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Azure AD B2C Plugin example app'),
),
body: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
children: [
TextButton(
onPressed: () async {
// you can just perform calls to the AzureB2C plugin to
// handle the B2C protocol (e.g. acquire, refresh tokens
// or sign out).
var data = await AzureB2C.policyTriggerInteractive(
_configuration!.defaultAuthority.policyName,
_configuration!.defaultScopes!,
null);
setState(() {
_retdata = data;
});
},
child: Text("LogIn")),
TextButton(
onPressed: () async {
var subjects = await AzureB2C.getSubjects();
var info = await AzureB2C.getUserInfo(subjects![0]);
setState(() {
_subjects = subjects;
_retdata = json.encode(info);
});
},
child: Text("UserInfo")),
],
),
Row(
children: [
TextButton(
onPressed: () async {
var token = await AzureB2C.getAccessToken(_subjects![0]);
setState(() {
_retdata = json.encode(token);
});
},
child: Text("AccessToken")),
TextButton(
onPressed: () async {
var data = await AzureB2C.policyTriggerSilently(
_subjects![0],
_configuration!.defaultAuthority.policyName,
_configuration!.defaultScopes!,
);
setState(() {
_retdata = data;
});
},
child: Text("Refresh")),
TextButton(
onPressed: () async {
var data = await AzureB2C.signOut(_subjects![0]);
setState(() {
_retdata = data;
});
},
child: Text("LogOut")),
],
),
Text(_retdata)
],
)),
),
);
}
}