Auth & User Management
The 14 methods that create, authenticate and manage users. The client keeps a cookie jar, so the session established by signupUser or loginUser is replayed on every later call automatically.
Most of these are reachable with a publishable key, which is what makes them safe to call straight from a Flutter app: own-account calls are pinned to the signed-in user. Two are not: getUser and checkIfUserExists look up arbitrary users and require a secret key on a server.
Setup
1import 'package:neuctra_authix_dart_package/neuctra_authix_dart_package.dart';
2
3// Flutter app: a publishable key (pk_live_…) passed with --dart-define.
4final authix = NeuctraAuthix(
5 const NeuctraAuthixConfig(
6 appId: String.fromEnvironment('AUTHIX_APP_ID'),
7 apiKey: String.fromEnvironment('AUTHIX_PUBLISHABLE_KEY'),
8 appName: 'MyApp',
9 ),
10);Authentication
signupUser
POST /users/signup
Future<Map<String, dynamic>> signupUser(SignupParams params)| Param | Type | Required | Description |
|---|---|---|---|
| name | String | Yes | Full name. |
| String | Yes | Email address. | |
| password | String | Yes | Plain password — hashed server-side. |
| username | String? | No | Unique handle. |
| phone | String? | No | Phone number. |
| address | String? | No | Postal address. |
| avatarUrl | String? | No | Profile image URL. |
| isActive | bool? | No | Account active flag. |
| role | String? | No | Role name, e.g. 'admin'. |
| extra | Map<String, dynamic> | No | Any further fields your app stores on the user record. |
Returns: Map<String, dynamic>
1final result = await authix.signupUser(
2 const SignupParams(
3 name: 'John Doe',
4 email: '[email protected]',
5 password: 'MyPass123',
6 username: 'johndoe',
7 role: 'user',
8 ),
9);
10
11final userId = result['user']?['id']?.toString() ?? '';Throws
ConfigurationError when name, email or password is empty — before any request is sent.loginUser
POST /users/login
Future<Map<String, dynamic>> loginUser(LoginParams params)| Param | Type | Required | Description |
|---|---|---|---|
| String | Yes | Registered email. | |
| password | String | Yes | Account password. |
Returns: Map<String, dynamic>
1try {
2 await authix.loginUser(
3 const LoginParams(email: '[email protected]', password: 'MyPass123'),
4 );
5 await persistSession();
6} on AuthenticationError {
7 showError('Wrong email or password');
8} on NetworkError {
9 showError('Could not reach the server');
10}Throws
ConfigurationError if either field is empty; AuthenticationError when the credentials are rejected.logoutUser
POST /users/logout
Future<bool> logoutUser({FutureOr<void> Function()? onLoggedOut})| Param | Type | Required | Description |
|---|---|---|---|
| onLoggedOut | Function? | No | Awaited after the session is cleared — use it to navigate or wipe storage. |
Returns: bool
1final ok = await authix.logoutUser(
2 onLoggedOut: () async {
3 await forgetSession();
4 navigatorKey.currentState?.pushReplacementNamed('/login');
5 },
6);checkUserSession
GET /users/session
Future<CheckSessionResponse> checkUserSession()Returns: CheckSessionResponse
1final session = await authix.checkUserSession();
2
3if (session.authenticated) {
4 final name = session.user?['name'];
5 print('Welcome back, $name');
6} else {
7 goToLogin();
8}Email verification
requestEmailVerificationOTP
POST /users/send-verify-otp/:userId
Future<Map<String, dynamic>> requestEmailVerificationOTP({required String userId, required String email})| Param | Type | Required | Description |
|---|---|---|---|
| userId | String | Yes | ID of the user to verify. |
| String | Yes | Address the OTP is sent to. |
Returns: Map<String, dynamic>
1await authix.requestEmailVerificationOTP(
2 userId: userId,
3 email: '[email protected]',
4);verifyEmail
POST /users/verify-email
Future<Map<String, dynamic>> verifyEmail({required String email, required String otp})| Param | Type | Required | Description |
|---|---|---|---|
| String | Yes | Address the OTP was sent to. | |
| otp | String | Yes | Code the user entered. |
Returns: Map<String, dynamic>
1await authix.verifyEmail(
2 email: '[email protected]',
3 otp: '123456',
4);Throws
User records
getUser
GET /users/:appId/user
Future<Map<String, dynamic>> getUser({String? id, String? username})| Param | Type | Required | Description |
|---|---|---|---|
| id | String? | No | Look up by user ID. |
| username | String? | No | Look up by username. |
Returns: Map<String, dynamic>
1final byId = await authix.getUser(id: 'user_123');
2final byName = await authix.getUser(username: 'johndoe');Throws
getUserProfile
POST /users/profile
Future<Map<String, dynamic>> getUserProfile({required String userId})| Param | Type | Required | Description |
|---|---|---|---|
| userId | String | Yes | ID of the profile to load. |
Returns: Map<String, dynamic>
1final profile = await authix.getUserProfile(userId: userId);
2print(profile['email']);updateUser
PUT /users/update/:userId
Future<Map<String, dynamic>> updateUser(UpdateUserParams params)| Param | Type | Required | Description |
|---|---|---|---|
| userId | String | Yes | ID of the user to update. |
| name | String? | No | Full name. |
| username | String? | No | Unique handle. |
| String? | No | Email address. | |
| password | String? | No | Prefer changePassword instead — see the warning below. |
| phone | String? | No | Phone number. |
| address | String? | No | Postal address. |
| avatarUrl | String? | No | Profile image URL. |
| isActive | bool? | No | Account active flag. |
| role | String? | No | Role name. |
Returns: Map<String, dynamic>
1await authix.updateUser(
2 UpdateUserParams(
3 userId: userId,
4 name: 'John A. Doe',
5 phone: '+1 555 0100',
6 ),
7);deleteUser
DELETE /users/delete/:userId
Future<Map<String, dynamic>> deleteUser(DeleteUserParams params)| Param | Type | Required | Description |
|---|---|---|---|
| userId | String | Yes | ID of the user to delete. |
Returns: Map<String, dynamic>
1await authix.deleteUser(DeleteUserParams(userId: userId));
2authix.session.clearSession();Passwords & recovery
changePassword
PUT /users/change-password/:userId
Future<Map<String, dynamic>> changePassword(ChangePasswordParams params)| Param | Type | Required | Description |
|---|---|---|---|
| userId | String | Yes | ID of the signed-in user. |
| currentPassword | String | Yes | Existing password — verified server-side. |
| newPassword | String | Yes | Replacement password. |
Returns: Map<String, dynamic>
1await authix.changePassword(
2 ChangePasswordParams(
3 userId: userId,
4 currentPassword: 'MyPass123',
5 newPassword: 'newPass456',
6 ),
7);Throws
requestResetUserPasswordOTP
POST /users/forgot-password
Future<Map<String, dynamic>> requestResetUserPasswordOTP({required String email})| Param | Type | Required | Description |
|---|---|---|---|
| String | Yes | Address of the account to recover. |
Returns: Map<String, dynamic>
1await authix.requestResetUserPasswordOTP(
2 email: '[email protected]',
3);resetUserPassword
POST /users/reset-password
Future<Map<String, dynamic>> resetUserPassword({required String email, required String otp, required String newPassword})| Param | Type | Required | Description |
|---|---|---|---|
| String | Yes | Address the OTP was sent to. | |
| otp | String | Yes | Code from the recovery email. |
| newPassword | String | Yes | Replacement password. |
Returns: Map<String, dynamic>
1await authix.resetUserPassword(
2 email: '[email protected]',
3 otp: '123456',
4 newPassword: 'newPass456',
5);checkIfUserExists
GET /users/check-user/:userId
Future<CheckUserResponse> checkIfUserExists(String userId)| Param | Type | Required | Description |
|---|---|---|---|
| userId | String | Yes | Passed positionally, not as a params object. |
Returns: CheckUserResponse
1final result = await authix.checkIfUserExists('user_123');
2
3if (result.exists) {
4 print('User is registered for this app');
5}updateUser can set a password — with a secret key
UpdateUserParams accepts a password field, and unlike changePassword it does not require the current one, so only a secret key may use it. With a publishable key the server refuses it; a Flutter app uses changePassword with the current password.
How sessions flow
signupUser and loginUser capture the authix_user_session cookie into the client's jar, and every later call replays it. logoutUser and checkUserSession clear it when the session is gone. With a publishable key this session is what every own-account call acts on; without one they throw NoUserSessionError (401). Persist authix.session.sessionCookie in secure storage to survive restarts, and seed it back with session.setSessionCookie() on launch.
Changing or resetting a password invalidates every session for that user server-side, so a stale cookie elsewhere stops working immediately.
Related