createOAuth2Session method

Future createOAuth2Session({
  1. required String provider,
  2. String? success,
  3. String? failure,
  4. List? scopes,
})

Create Account Session with OAuth2

Allow the user to login to their account using the OAuth2 provider of their choice. Each OAuth2 provider should be enabled from the Appwrite console first. Use the success and failure arguments to provide a redirect URL's back to your app when login is completed.

Implementation

Future createOAuth2Session(
    {required String provider,
    String? success,
    String? failure,
    List? scopes}) {
  final String path = '/account/sessions/oauth2/{provider}'
      .replaceAll(RegExp('{provider}'), provider);

  final Map<String, dynamic> params = {
    'success': success,
    'failure': failure,
    'scopes': scopes,
    'project': client.config['project'],
  };

  final List query = [];

  params.forEach((key, value) {
    if (value is List) {
      for (var item in value) {
        query.add(Uri.encodeComponent(key + '[]') +
            '=' +
            Uri.encodeComponent(item));
      }
    } else {
      query.add(Uri.encodeComponent(key) + '=' + Uri.encodeComponent(value));
    }
  });

  Uri endpoint = Uri.parse(client.endPoint);
  Uri url = new Uri(
      scheme: endpoint.scheme,
      host: endpoint.host,
      port: endpoint.port,
      path: endpoint.path + path,
      query: query.join('&'));

  if (kIsWeb) {
    html.window.location.href = url.toString();
    return Future.value();
  } else {
    return client.webAuth(url.toString());
  }
}