API Documentation
The documentation for the SwiftyRx can be found at https://acme.swiftyrx.dev/api/ui/
Client Installation
The clients are not yet published to pub.dev. They will be soon.
Example Usage
Authorization
custom_auth_manager.dart
1import 'dart:async';
2
3import 'package:dio/dio.dart';
4import 'package:swifty/swifty_oauth_client/api.dart';
5import 'package:swifty/swifty_oauth_client/model/token_response.dart';
6import 'package:swifty/swifty_oauth_client/model/user_info_response.dart';
7import 'package:swifty/utils/api_helpers.dart' if (dart.library.js_interop) 'package:swifty/utils/api_helpers_web.dart';
8
9import '../../swifty_oauth_client/model/get_token_request.dart';
10import 'custom_auth_user_provider.dart';
11
12export 'custom_auth_manager.dart';
13
14extension on Response? {
15 get meta => AuthenticatedMeta.fromJson(this?.data['meta'] as Map<String, dynamic>);
16}
17
18class CustomAuthManager {
19 // Auth session attributes
20 TokenResponse? tokenResponse;
21 UserInfoResponse? userInfoResponse;
22 DateTime? tokenExpiration;
23
24 Future signOut() async {
25 tokenResponse = null;
26 userInfoResponse = null;
27 }
28
29 Future<SwiftyAuthUser> signIn(String username, String password, BuildContext context) async {
30 Dio dio = OAuthDioService().dio;
31 final api = Swifty(dio: dio).getOAuthApi();
32 SwiftyAuthUser authUser = SwiftyAuthUser(loggedIn: false);
33 try {
34 await api.getToken(getTokenRequest: GetTokenRequest(grantType: "password", username: username, password: password)).then((response) {
35 tokenResponse = response.data;
36 tokenExpiration = DateTime.now().add(Duration(seconds: tokenResponse?.expiresIn ?? 300));
37
38 if (!kIsWeb) {
39 saveStringPreference("accessToken", tokenResponse?.accessToken ?? '');
40 saveStringPreference("refreshToken", tokenResponse?.refreshToken ?? '');
41 }
42 }).onError((error, stackTrace) {
43 if (kDebugMode) {
44 debugPrint("Error when calling /oauth/token: $error");
45 }
46
47 throw error ?? "Unknown error";
48 });
49 if (tokenResponse != null) {
50 await api.getUserInfo(headers: {"Authorization": "Bearer ${tokenResponse?.accessToken}"}).then((response) {
51 userInfoResponse = response.data;
52 authUser = SwiftyAuthUser(loggedIn: true, tokenResponse: tokenResponse, userInfo: userInfoResponse);
53 if (!context.mounted) {
54 return;
55 }
56 Provider.of<UserInfoNotifier>(context, listen: false).setUserInfo(userInfoResponse!);
57 }).onError((error, stackTrace) {
58 if (kDebugMode) {
59 debugPrint("Error when calling /oath/userinfo: $error");
60 }
61
62 throw error ?? "Unknown error";
63 });
64 }
65 } on DioException catch (e) {
66 if (kDebugMode) {
67 debugPrint("Exception when calling OAuth API: $e");
68 }
69 } finally {}
70 return authUser;
71 }
72}
Full Example
A more complete example of using the Dart SDK is forthcoming.