createOAuth2Token method

Future createOAuth2Token({
  1. required OAuthProvider provider,
  2. String? success,
  3. String? failure,
  4. List<String>? scopes,
})

Create OAuth2 token

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.

If authentication succeeds, userId and secret of a token will be appended to the success URL as query parameters. These can be used to create a new session using the Create session endpoint.

A user is limited to 10 active sessions at a time by default. Learn more about session limits.

Implementation

Future createOAuth2Token(
    {required enums.OAuthProvider provider,
    String? success,
    String? failure,
    List<String>? scopes}) async {
  final String apiPath = '/account/tokens/oauth2/{provider}'
      .replaceAll('{provider}', provider.value);

  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 if (value != null) {
      query.add(Uri.encodeComponent(key) + '=' + Uri.encodeComponent(value));
    }
  });

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

  return client.webAuth(url);
}