-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathexample_app.dart
More file actions
134 lines (121 loc) · 3.7 KB
/
Copy pathexample_app.dart
File metadata and controls
134 lines (121 loc) · 3.7 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import 'dart:io' show Platform;
import 'package:auth0_flutter/auth0_flutter.dart';
import 'package:auth0_flutter/auth0_flutter_web.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'constants.dart';
import 'hero.dart';
import 'user.dart';
class ExampleApp extends StatefulWidget {
final Auth0? auth0;
const ExampleApp({this.auth0, final Key? key}) : super(key: key);
@override
State<ExampleApp> createState() => _ExampleAppState();
}
class _ExampleAppState extends State<ExampleApp> {
UserProfile? _user;
late Auth0 auth0;
late Auth0Web auth0Web;
@override
void initState() {
super.initState();
auth0 = widget.auth0 ??
Auth0(dotenv.env['AUTH0_DOMAIN']!, dotenv.env['AUTH0_CLIENT_ID']!);
auth0Web =
Auth0Web(dotenv.env['AUTH0_DOMAIN']!, dotenv.env['AUTH0_CLIENT_ID']!);
if (kIsWeb) {
auth0Web.onLoad().then((final credentials) => setState(() {
_user = credentials?.user;
}));
}
}
Future<void> login() async {
try {
if (kIsWeb) {
return auth0Web.loginWithRedirect(redirectUrl: 'http://localhost:3000');
}
Credentials credentials;
if (Platform.isWindows) {
credentials = await auth0
.windowsWebAuthentication()
.login(appCustomURL: 'auth0flutter://callback');
} else {
credentials = await auth0
.webAuthentication(scheme: dotenv.env['AUTH0_CUSTOM_SCHEME'])
// Use a Universal Link callback URL on iOS 17.4+ / macOS 14.4+
// useHTTPS is ignored on Android
.login(useHTTPS: true);
}
setState(() {
_user = credentials.user;
});
} catch (e) {
print(e);
}
}
Future<void> logout() async {
try {
if (kIsWeb) {
await auth0Web.logout(returnToUrl: 'http://localhost:3000');
} else if (Platform.isWindows) {
await auth0
.windowsWebAuthentication()
.logout(appCustomURL: 'auth0flutter://callback');
setState(() {
_user = null;
});
} else {
await auth0
.webAuthentication(scheme: dotenv.env['AUTH0_CUSTOM_SCHEME'])
// Use a Universal Link logout URL on iOS 17.4+ / macOS 14.4+
// useHTTPS is ignored on Android
.logout(useHTTPS: true);
setState(() {
_user = null;
});
}
} catch (e) {
print(e);
}
}
@override
Widget build(final BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Padding(
padding: const EdgeInsets.only(
top: padding,
bottom: padding,
left: padding / 2,
right: padding / 2,
),
child: Column(crossAxisAlignment: CrossAxisAlignment.center, children: [
Expanded(
child: Row(children: [
_user != null
? Expanded(child: UserWidget(user: _user))
: const Expanded(child: HeroWidget())
])),
_user != null
? ElevatedButton(
onPressed: logout,
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<Color>(Colors.black),
),
child: const Text('Logout'),
)
: ElevatedButton(
onPressed: login,
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<Color>(Colors.black),
),
child: const Text('Login'),
)
]),
)),
);
}
}