webAuth method
Handle OAuth2 session creation.
Implementation
@override
Future webAuth(Uri url, {String? callbackUrlScheme}) {
return FlutterWebAuth2.authenticate(
url: url.toString(),
callbackUrlScheme:
callbackUrlScheme != null && _customSchemeAllowed
? callbackUrlScheme
: "appwrite-callback-${config['project']!}",
options: const FlutterWebAuth2Options(
useWebview: false,
),
).then((value) async {
Uri url = Uri.parse(value);
final key = url.queryParameters['key'];
final secret = url.queryParameters['secret'];
if (key == null || secret == null) {
final error = url.queryParameters['error'];
if (error != null && error.isNotEmpty) {
try {
final parsed = jsonDecode(error);
if (parsed is Map) {
final codeValue = parsed['code'];
final int? code =
codeValue is int
? codeValue
: int.tryParse('${codeValue ?? ''}');
throw AppwriteException(
parsed['message']?.toString() ?? error,
code,
parsed['type']?.toString(),
error,
);
}
} on AppwriteException {
rethrow;
} catch (_) {
// Fall through to a plain-string error when JSON is malformed.
}
throw AppwriteException(error);
}
throw AppwriteException(
"Invalid OAuth2 Response. Key and Secret not available.",
500,
);
}
Cookie cookie = Cookie(key, secret);
cookie.domain = Uri.parse(_endPoint).host;
cookie.httpOnly = true;
cookie.path = '/';
List<Cookie> cookies = [cookie];
await init();
_cookieJar.saveFromResponse(Uri.parse(_endPoint), cookies);
});
}