1
0
Fork 0
mirror of synced 2024-07-15 11:25:53 +12:00
appwrite/app/sdks/client-flutter/lib/services/account.dart

425 lines
15 KiB
Dart
Raw Normal View History

2020-03-27 02:20:07 +13:00
2020-04-15 02:13:19 +12:00
import 'dart:io';
2020-04-09 05:26:18 +12:00
2020-01-31 05:18:59 +13:00
import 'package:dio/dio.dart';
2020-03-27 02:20:07 +13:00
import 'package:meta/meta.dart';
2020-04-15 02:13:19 +12:00
import 'package:flutter_web_auth/flutter_web_auth.dart';
2020-03-27 02:20:07 +13:00
2020-04-06 01:23:15 +12:00
import "../client.dart";
2020-03-27 02:20:07 +13:00
import '../enums.dart';
2020-04-06 01:23:15 +12:00
import "../service.dart";
2020-01-31 05:18:59 +13:00
class Account extends Service {
2020-04-06 01:23:15 +12:00
Account(Client client): super(client);
2020-01-31 05:18:59 +13:00
2020-04-11 22:02:21 +12:00
/// Get Account
///
2020-01-31 05:18:59 +13:00
/// Get currently logged in user data as JSON object.
2020-04-11 22:02:21 +12:00
///
2020-04-06 01:23:15 +12:00
Future<Response> get() {
final String path = '/account';
2020-01-31 05:18:59 +13:00
2020-04-06 01:23:15 +12:00
final Map<String, dynamic> params = {
};
2020-01-31 05:18:59 +13:00
2020-04-11 22:02:21 +12:00
final Map<String, String> headers = {
'content-type': 'application/json',
};
return client.call(HttpMethod.get, path: path, params: params, headers: headers);
2020-01-31 05:18:59 +13:00
}
2020-04-11 22:02:21 +12:00
/// Create Account
///
2020-02-14 19:28:54 +13:00
/// Use this endpoint to allow a new user to register a new account in your
/// project. After the user registration completes successfully, you can use
2020-06-17 22:12:45 +12:00
/// the [/account/verfication](/docs/client/account#createVerification) route
/// to start verifying the user email address. To allow your new user to login
/// to his new account, you need to create a new [account
/// session](/docs/client/account#createSession).
2020-04-11 22:02:21 +12:00
///
2020-04-06 16:55:38 +12:00
Future<Response> create({@required String email, @required String password, String name = ''}) {
2020-04-06 01:23:15 +12:00
final String path = '/account';
2020-01-31 05:18:59 +13:00
2020-04-06 01:23:15 +12:00
final Map<String, dynamic> params = {
'email': email,
'password': password,
'name': name,
};
2020-01-31 05:18:59 +13:00
2020-04-11 22:02:21 +12:00
final Map<String, String> headers = {
'content-type': 'application/json',
};
return client.call(HttpMethod.post, path: path, params: params, headers: headers);
2020-01-31 05:18:59 +13:00
}
2020-04-11 22:02:21 +12:00
/// Delete Account
///
2020-01-31 05:18:59 +13:00
/// Delete a currently logged in user account. Behind the scene, the user
/// record is not deleted but permanently blocked from any access. This is done
/// to avoid deleted accounts being overtaken by new users with the same email
/// address. Any user-related resources like documents or storage files should
/// be deleted separately.
2020-04-11 22:02:21 +12:00
///
2020-04-06 01:23:15 +12:00
Future<Response> delete() {
final String path = '/account';
2020-01-31 05:18:59 +13:00
2020-04-06 01:23:15 +12:00
final Map<String, dynamic> params = {
};
2020-01-31 05:18:59 +13:00
2020-04-11 22:02:21 +12:00
final Map<String, String> headers = {
'content-type': 'application/json',
};
return client.call(HttpMethod.delete, path: path, params: params, headers: headers);
2020-01-31 05:18:59 +13:00
}
2020-04-11 22:02:21 +12:00
/// Update Account Email
///
2020-01-31 05:18:59 +13:00
/// Update currently logged in user account email address. After changing user
/// address, user confirmation status is being reset and a new confirmation
/// mail is sent. For security measures, user password is required to complete
/// this request.
2020-04-11 22:02:21 +12:00
///
2020-04-06 01:23:15 +12:00
Future<Response> updateEmail({@required String email, @required String password}) {
final String path = '/account/email';
2020-01-31 05:18:59 +13:00
2020-04-06 01:23:15 +12:00
final Map<String, dynamic> params = {
'email': email,
'password': password,
};
2020-01-31 05:18:59 +13:00
2020-04-11 22:02:21 +12:00
final Map<String, String> headers = {
'content-type': 'application/json',
};
return client.call(HttpMethod.patch, path: path, params: params, headers: headers);
2020-01-31 05:18:59 +13:00
}
2020-04-11 22:02:21 +12:00
/// Get Account Logs
///
2020-01-31 05:18:59 +13:00
/// Get currently logged in user list of latest security activity logs. Each
/// log returns user IP address, location and date and time of log.
2020-04-11 22:02:21 +12:00
///
2020-04-06 01:23:15 +12:00
Future<Response> getLogs() {
final String path = '/account/logs';
2020-01-31 05:18:59 +13:00
2020-04-06 01:23:15 +12:00
final Map<String, dynamic> params = {
};
2020-01-31 05:18:59 +13:00
2020-04-11 22:02:21 +12:00
final Map<String, String> headers = {
'content-type': 'application/json',
};
return client.call(HttpMethod.get, path: path, params: params, headers: headers);
2020-01-31 05:18:59 +13:00
}
2020-04-11 22:02:21 +12:00
/// Update Account Name
///
2020-01-31 05:18:59 +13:00
/// Update currently logged in user account name.
2020-04-11 22:02:21 +12:00
///
2020-04-06 01:23:15 +12:00
Future<Response> updateName({@required String name}) {
final String path = '/account/name';
2020-01-31 05:18:59 +13:00
2020-04-06 01:23:15 +12:00
final Map<String, dynamic> params = {
'name': name,
};
2020-01-31 05:18:59 +13:00
2020-04-11 22:02:21 +12:00
final Map<String, String> headers = {
'content-type': 'application/json',
};
return client.call(HttpMethod.patch, path: path, params: params, headers: headers);
2020-01-31 05:18:59 +13:00
}
2020-04-11 22:02:21 +12:00
/// Update Account Password
///
2020-01-31 05:18:59 +13:00
/// Update currently logged in user password. For validation, user is required
/// to pass the password twice.
2020-04-11 22:02:21 +12:00
///
2020-04-06 01:23:15 +12:00
Future<Response> updatePassword({@required String password, @required String oldPassword}) {
final String path = '/account/password';
2020-01-31 05:18:59 +13:00
2020-04-06 01:23:15 +12:00
final Map<String, dynamic> params = {
'password': password,
2020-05-02 16:23:56 +12:00
'oldPassword': oldPassword,
2020-04-06 01:23:15 +12:00
};
2020-01-31 05:18:59 +13:00
2020-04-11 22:02:21 +12:00
final Map<String, String> headers = {
'content-type': 'application/json',
};
return client.call(HttpMethod.patch, path: path, params: params, headers: headers);
2020-01-31 05:18:59 +13:00
}
2020-04-11 22:02:21 +12:00
/// Get Account Preferences
///
2020-02-14 19:28:54 +13:00
/// Get currently logged in user preferences as a key-value object.
2020-04-11 22:02:21 +12:00
///
2020-04-06 01:23:15 +12:00
Future<Response> getPrefs() {
final String path = '/account/prefs';
2020-01-31 05:18:59 +13:00
2020-04-06 01:23:15 +12:00
final Map<String, dynamic> params = {
};
2020-01-31 05:18:59 +13:00
2020-04-11 22:02:21 +12:00
final Map<String, String> headers = {
'content-type': 'application/json',
};
return client.call(HttpMethod.get, path: path, params: params, headers: headers);
2020-01-31 05:18:59 +13:00
}
2020-04-11 22:02:21 +12:00
/// Update Account Preferences
///
2020-01-31 05:18:59 +13:00
/// Update currently logged in user account preferences. You can pass only the
/// specific settings you wish to update.
2020-04-11 22:02:21 +12:00
///
2020-04-06 01:23:15 +12:00
Future<Response> updatePrefs({@required dynamic prefs}) {
final String path = '/account/prefs';
2020-01-31 05:18:59 +13:00
2020-04-06 01:23:15 +12:00
final Map<String, dynamic> params = {
'prefs': prefs,
};
2020-01-31 05:18:59 +13:00
2020-04-11 22:02:21 +12:00
final Map<String, String> headers = {
'content-type': 'application/json',
};
return client.call(HttpMethod.patch, path: path, params: params, headers: headers);
2020-01-31 05:18:59 +13:00
}
2020-04-11 22:02:21 +12:00
/// Create Password Recovery
///
2020-01-31 05:18:59 +13:00
/// Sends the user an email with a temporary secret key for password reset.
/// When the user clicks the confirmation link he is redirected back to your
/// app password reset URL with the secret key and email address values
/// attached to the URL query string. Use the query string params to submit a
2020-06-17 22:12:45 +12:00
/// request to the [PUT /account/recovery](/docs/client/account#updateRecovery)
2020-02-14 19:28:54 +13:00
/// endpoint to complete the process.
2020-04-11 22:02:21 +12:00
///
2020-04-06 01:23:15 +12:00
Future<Response> createRecovery({@required String email, @required String url}) {
final String path = '/account/recovery';
2020-01-31 05:18:59 +13:00
2020-04-06 01:23:15 +12:00
final Map<String, dynamic> params = {
'email': email,
'url': url,
};
2020-01-31 05:18:59 +13:00
2020-04-11 22:02:21 +12:00
final Map<String, String> headers = {
'content-type': 'application/json',
};
return client.call(HttpMethod.post, path: path, params: params, headers: headers);
2020-01-31 05:18:59 +13:00
}
2020-04-11 22:02:21 +12:00
/// Complete Password Recovery
///
2020-01-31 05:18:59 +13:00
/// Use this endpoint to complete the user account password reset. Both the
/// **userId** and **secret** arguments will be passed as query parameters to
2020-02-14 19:28:54 +13:00
/// the redirect URL you have provided when sending your request to the [POST
2020-06-17 22:12:45 +12:00
/// /account/recovery](/docs/client/account#createRecovery) endpoint.
2020-01-31 05:18:59 +13:00
///
/// Please note that in order to avoid a [Redirect
/// Attack](https://github.com/OWASP/CheatSheetSeries/blob/master/cheatsheets/Unvalidated_Redirects_and_Forwards_Cheat_Sheet.md)
/// the only valid redirect URLs are the ones from domains you have set when
/// adding your platforms in the console interface.
2020-04-11 22:02:21 +12:00
///
2020-05-02 16:23:56 +12:00
Future<Response> updateRecovery({@required String userId, @required String secret, @required String password, @required String passwordAgain}) {
2020-04-06 01:23:15 +12:00
final String path = '/account/recovery';
2020-01-31 05:18:59 +13:00
2020-04-06 01:23:15 +12:00
final Map<String, dynamic> params = {
'userId': userId,
'secret': secret,
2020-05-02 16:23:56 +12:00
'password': password,
'passwordAgain': passwordAgain,
2020-04-06 01:23:15 +12:00
};
2020-01-31 05:18:59 +13:00
2020-04-11 22:02:21 +12:00
final Map<String, String> headers = {
'content-type': 'application/json',
};
return client.call(HttpMethod.put, path: path, params: params, headers: headers);
2020-01-31 05:18:59 +13:00
}
2020-04-11 22:02:21 +12:00
/// Get Account Sessions
///
2020-01-31 05:18:59 +13:00
/// Get currently logged in user list of active sessions across different
/// devices.
2020-04-11 22:02:21 +12:00
///
2020-04-06 01:23:15 +12:00
Future<Response> getSessions() {
final String path = '/account/sessions';
2020-01-31 05:18:59 +13:00
2020-04-06 01:23:15 +12:00
final Map<String, dynamic> params = {
};
2020-01-31 05:18:59 +13:00
2020-04-11 22:02:21 +12:00
final Map<String, String> headers = {
'content-type': 'application/json',
};
return client.call(HttpMethod.get, path: path, params: params, headers: headers);
2020-01-31 05:18:59 +13:00
}
2020-04-11 22:02:21 +12:00
/// Create Account Session
///
2020-01-31 05:18:59 +13:00
/// Allow the user to login into his account by providing a valid email and
2020-02-14 19:28:54 +13:00
/// password combination. This route will create a new session for the user.
2020-04-11 22:02:21 +12:00
///
2020-04-06 01:23:15 +12:00
Future<Response> createSession({@required String email, @required String password}) {
final String path = '/account/sessions';
2020-01-31 05:18:59 +13:00
2020-04-06 01:23:15 +12:00
final Map<String, dynamic> params = {
'email': email,
'password': password,
};
2020-01-31 05:18:59 +13:00
2020-04-11 22:02:21 +12:00
final Map<String, String> headers = {
'content-type': 'application/json',
};
return client.call(HttpMethod.post, path: path, params: params, headers: headers);
2020-01-31 05:18:59 +13:00
}
2020-04-11 22:02:21 +12:00
/// Delete All Account Sessions
///
2020-01-31 05:18:59 +13:00
/// Delete all sessions from the user account and remove any sessions cookies
/// from the end client.
2020-04-11 22:02:21 +12:00
///
2020-04-06 01:23:15 +12:00
Future<Response> deleteSessions() {
final String path = '/account/sessions';
2020-01-31 05:18:59 +13:00
2020-04-06 01:23:15 +12:00
final Map<String, dynamic> params = {
};
2020-01-31 05:18:59 +13:00
2020-04-11 22:02:21 +12:00
final Map<String, String> headers = {
'content-type': 'application/json',
};
return client.call(HttpMethod.delete, path: path, params: params, headers: headers);
2020-01-31 05:18:59 +13:00
}
2020-04-11 22:02:21 +12:00
/// Create Account Session with OAuth2
///
2020-02-24 07:09:34 +13:00
/// Allow the user to login to his account using the OAuth2 provider of his
/// choice. Each OAuth2 provider should be enabled from the Appwrite console
2020-01-31 05:18:59 +13:00
/// first. Use the success and failure arguments to provide a redirect URL's
/// back to your app when login is completed.
2020-04-11 22:02:21 +12:00
///
2020-06-17 22:12:45 +12:00
Future createOAuth2Session({@required String provider, String success = 'https://appwrite.io/auth/oauth2/success', String failure = 'https://appwrite.io/auth/oauth2/failure', List scopes = const []}) {
2020-04-06 01:23:15 +12:00
final String path = '/account/sessions/oauth2/{provider}'.replaceAll(RegExp('{provider}'), provider);
2020-01-31 05:18:59 +13:00
2020-04-06 01:23:15 +12:00
final Map<String, dynamic> params = {
'success': success,
'failure': failure,
2020-06-17 22:12:45 +12:00
'scopes': scopes,
2020-04-15 02:13:19 +12:00
'project': client.config['project'],
};
2020-06-17 22:46:15 +12:00
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));
}
});
2020-04-15 02:13:19 +12:00
Uri endpoint = Uri.parse(client.endPoint);
Uri url = new Uri(scheme: endpoint.scheme,
host: endpoint.host,
port: endpoint.port,
path: endpoint.path + path,
2020-06-17 22:46:15 +12:00
query: query.join('&')
2020-04-15 02:13:19 +12:00
);
return FlutterWebAuth.authenticate(
url: url.toString(),
2020-05-18 06:45:14 +12:00
callbackUrlScheme: "appwrite-callback-" + client.config['project']
2020-05-24 05:19:28 +12:00
).then((value) async {
2020-04-15 02:13:19 +12:00
Uri url = Uri.parse(value);
2020-07-10 16:01:51 +12:00
Cookie cookie = new Cookie(url.queryParameters['key'], url.queryParameters['secret']);
cookie.domain = Uri.parse(client.endPoint).host;
cookie.httpOnly = true;
cookie.path = '/';
List<Cookie> cookies = [cookie];
await client.init();
client.cookieJar.saveFromResponse(Uri.parse(client.endPoint), cookies);
2020-04-15 02:13:19 +12:00
});
2020-01-31 05:18:59 +13:00
}
2020-04-11 22:02:21 +12:00
/// Delete Account Session
///
2020-01-31 05:18:59 +13:00
/// Use this endpoint to log out the currently logged in user from all his
/// account sessions across all his different devices. When using the option id
/// argument, only the session unique ID provider will be deleted.
2020-04-11 22:02:21 +12:00
///
2020-04-06 01:23:15 +12:00
Future<Response> deleteSession({@required String sessionId}) {
final String path = '/account/sessions/{sessionId}'.replaceAll(RegExp('{sessionId}'), sessionId);
2020-01-31 05:18:59 +13:00
2020-04-06 01:23:15 +12:00
final Map<String, dynamic> params = {
};
2020-01-31 05:18:59 +13:00
2020-04-11 22:02:21 +12:00
final Map<String, String> headers = {
'content-type': 'application/json',
};
return client.call(HttpMethod.delete, path: path, params: params, headers: headers);
2020-02-14 19:28:54 +13:00
}
2020-04-11 22:02:21 +12:00
/// Create Email Verification
///
2020-02-14 19:28:54 +13:00
/// Use this endpoint to send a verification message to your user email address
/// to confirm they are the valid owners of that address. Both the **userId**
/// and **secret** arguments will be passed as query parameters to the URL you
/// have provider to be attached to the verification email. The provided URL
/// should redirect the user back for your app and allow you to complete the
/// verification process by verifying both the **userId** and **secret**
/// parameters. Learn more about how to [complete the verification
2020-06-17 22:12:45 +12:00
/// process](/docs/client/account#updateAccountVerification).
2020-02-14 19:28:54 +13:00
///
/// Please note that in order to avoid a [Redirect
/// Attack](https://github.com/OWASP/CheatSheetSeries/blob/master/cheatsheets/Unvalidated_Redirects_and_Forwards_Cheat_Sheet.md)
/// the only valid redirect URLs are the ones from domains you have set when
/// adding your platforms in the console interface.
2020-04-11 22:02:21 +12:00
///
2020-04-06 01:23:15 +12:00
Future<Response> createVerification({@required String url}) {
final String path = '/account/verification';
2020-02-14 19:28:54 +13:00
2020-04-06 01:23:15 +12:00
final Map<String, dynamic> params = {
'url': url,
};
2020-02-14 19:28:54 +13:00
2020-04-11 22:02:21 +12:00
final Map<String, String> headers = {
'content-type': 'application/json',
};
return client.call(HttpMethod.post, path: path, params: params, headers: headers);
2020-01-31 05:18:59 +13:00
}
2020-04-11 22:02:21 +12:00
/// Complete Email Verification
///
2020-01-31 05:18:59 +13:00
/// Use this endpoint to complete the user email verification process. Use both
/// the **userId** and **secret** parameters that were attached to your app URL
/// to verify the user email ownership. If confirmed this route will return a
/// 200 status code.
2020-04-11 22:02:21 +12:00
///
2020-04-06 01:23:15 +12:00
Future<Response> updateVerification({@required String userId, @required String secret}) {
final String path = '/account/verification';
2020-01-31 05:18:59 +13:00
2020-04-06 01:23:15 +12:00
final Map<String, dynamic> params = {
'userId': userId,
'secret': secret,
};
2020-01-31 05:18:59 +13:00
2020-04-11 22:02:21 +12:00
final Map<String, String> headers = {
'content-type': 'application/json',
};
return client.call(HttpMethod.put, path: path, params: params, headers: headers);
2020-01-31 05:18:59 +13:00
}
}