|
1 |
| -To create a new user with Dart using the Appwrite Users API, you can use one of the provided methods depending on the hashing algorithm you want to use for the password. Below are examples for creating a user with different hashing algorithms: |
| 1 | +To create a new user using the Appwrite Users API with Dart, you can use the `create` method from the `Users` class. Below is an example of how to do this: |
2 | 2 |
|
3 |
| -### Using PHPass |
4 | 3 | ```dart
|
5 | 4 | import 'package:dart_appwrite/dart_appwrite.dart';
|
6 | 5 |
|
7 | 6 | Client client = Client()
|
8 |
| - .setEndpoint('https://cloud.appwrite.io/v1') // Your API Endpoint |
| 7 | + .setEndpoint('https://<REGION>.cloud.appwrite.io/v1') // Your API Endpoint |
9 | 8 | .setProject('<YOUR_PROJECT_ID>') // Your project ID
|
10 | 9 | .setKey('<YOUR_API_KEY>'); // Your secret API key
|
11 | 10 |
|
12 | 11 | Users users = Users(client);
|
13 | 12 |
|
14 |
| -User result = await users.createPHPassUser( |
15 |
| - userId: '<USER_ID>', |
16 |
| - |
17 |
| - password: 'password', |
18 |
| - name: '<NAME>', // (optional) |
19 |
| -); |
| 13 | +Future<void> createUser() async { |
| 14 | + try { |
| 15 | + User result = await users.create( |
| 16 | + userId: '<USER_ID>', |
| 17 | + email: '[email protected]', // (optional) |
| 18 | + phone: '+12065550100', // (optional) |
| 19 | + password: '', // (optional) |
| 20 | + name: '<NAME>', // (optional) |
| 21 | + ); |
| 22 | + print('User created: ${result.$id}'); |
| 23 | + } catch (e) { |
| 24 | + print('Error creating user: $e'); |
| 25 | + } |
| 26 | +} |
| 27 | +
|
| 28 | +createUser(); |
20 | 29 | ```
|
21 | 30 |
|
22 |
| -### Using Scrypt |
23 |
| -```dart |
24 |
| -import 'package:dart_appwrite/dart_appwrite.dart'; |
25 |
| -
|
26 |
| -Client client = Client() |
27 |
| - .setEndpoint('https://cloud.appwrite.io/v1') // Your API Endpoint |
28 |
| - .setProject('<YOUR_PROJECT_ID>') // Your project ID |
29 |
| - .setKey('<YOUR_API_KEY>'); // Your secret API key |
30 |
| -
|
31 |
| -Users users = Users(client); |
32 |
| -
|
33 |
| -User result = await users.createScryptUser( |
34 |
| - userId: '<USER_ID>', |
35 |
| - |
36 |
| - password: 'password', |
37 |
| - passwordSalt: '<PASSWORD_SALT>', |
38 |
| - passwordCpu: 0, |
39 |
| - passwordMemory: 0, |
40 |
| - passwordParallel: 0, |
41 |
| - passwordLength: 0, |
42 |
| - name: '<NAME>', // (optional) |
43 |
| -); |
44 |
| -``` |
45 |
| - |
46 |
| -### Using Bcrypt |
47 |
| -```dart |
48 |
| -import 'package:dart_appwrite/dart_appwrite.dart'; |
49 |
| -
|
50 |
| -Client client = Client() |
51 |
| - .setEndpoint('https://cloud.appwrite.io/v1') // Your API Endpoint |
52 |
| - .setProject('<YOUR_PROJECT_ID>') // Your project ID |
53 |
| - .setKey('<YOUR_API_KEY>'); // Your secret API key |
54 |
| -
|
55 |
| -Users users = Users(client); |
56 |
| -
|
57 |
| -User result = await users.createBcryptUser( |
58 |
| - userId: '<USER_ID>', |
59 |
| - |
60 |
| - password: 'password', |
61 |
| - name: '<NAME>', // (optional) |
62 |
| -); |
63 |
| -``` |
64 |
| - |
65 |
| -### Using Scrypt Modified |
66 |
| -```dart |
67 |
| -import 'package:dart_appwrite/dart_appwrite.dart'; |
68 |
| -
|
69 |
| -Client client = Client() |
70 |
| - .setEndpoint('https://cloud.appwrite.io/v1') // Your API Endpoint |
71 |
| - .setProject('<YOUR_PROJECT_ID>') // Your project ID |
72 |
| - .setKey('<YOUR_API_KEY>'); // Your secret API key |
73 |
| -
|
74 |
| -Users users = Users(client); |
75 |
| -
|
76 |
| -User result = await users.createScryptModifiedUser( |
77 |
| - userId: '<USER_ID>', |
78 |
| - |
79 |
| - password: 'password', |
80 |
| - passwordSalt: '<PASSWORD_SALT>', |
81 |
| - passwordSaltSeparator: '<PASSWORD_SALT_SEPARATOR>', |
82 |
| - passwordSignerKey: '<PASSWORD_SIGNER_KEY>', |
83 |
| - name: '<NAME>', // (optional) |
84 |
| -); |
85 |
| -``` |
86 |
| - |
87 |
| -### Using Argon2 |
88 |
| -```dart |
89 |
| -import 'package:dart_appwrite/dart_appwrite.dart'; |
90 |
| -
|
91 |
| -Client client = Client() |
92 |
| - .setEndpoint('https://cloud.appwrite.io/v1') // Your API Endpoint |
93 |
| - .setProject('<YOUR_PROJECT_ID>') // Your project ID |
94 |
| - .setKey('<YOUR_API_KEY>'); // Your secret API key |
95 |
| -
|
96 |
| -Users users = Users(client); |
97 |
| -
|
98 |
| -User result = await users.createArgon2User( |
99 |
| - userId: '<USER_ID>', |
100 |
| - |
101 |
| - password: 'password', |
102 |
| - name: '<NAME>', // (optional) |
103 |
| -); |
104 |
| -``` |
105 |
| - |
106 |
| -Replace `<YOUR_PROJECT_ID>`, `<YOUR_API_KEY>`, `<USER_ID>`, `<PASSWORD_SALT>`, `<PASSWORD_SALT_SEPARATOR>`, and `<PASSWORD_SIGNER_KEY>` with your actual Appwrite project details and user-specific information. |
| 31 | +In this example, replace `<REGION>`, `<YOUR_PROJECT_ID>`, `<YOUR_API_KEY>`, `<USER_ID>`, and other placeholders with your actual Appwrite project details and user information. The `create` method allows you to specify optional parameters such as `email`, `phone`, `password`, and `name`. |
107 | 32 |
|
108 | 33 | Sources:
|
109 | 34 | - https://appwrite.io/docs/references/cloud/server-dart/users
|
0 commit comments