Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/Jay-byte389/AMS/llms.txt

Use this file to discover all available pages before exploring further.

src/services/storage.js is a typed helper layer that wraps @react-native-async-storage/async-storage with named, purpose-specific functions. Rather than calling AsyncStorage.getItem and JSON.parse directly throughout the app, all persistence logic is centralised here. The file covers four concerns: user account registration, session management, favourited doctors, and a local appointment cache. Every function is async and returns a structured result object or typed value — never a raw string.

Storage Keys

ConstantKeyStores
USERS_KEY@registered_usersJSON array of all registered UserObjects
SESSION_KEY@logged_in_userJSON-serialised UserObject of the active session
FAVOURITE_KEY@favourite_doctorsJSON array of favourite doctor ID strings
Appoinment_key@appoinmentsJSON array of locally cached Appointment objects
The @appoinments key contains a deliberate typo (single p in appointments). This matches the constant name Appoinment_key in the source file. Do not “correct” it in your own reads or writes — changing the key would orphan any data already stored under the original string.

User Account Functions

saveUser(userData)

Appends a new user to the @registered_users array. Reads the existing array first so no data is overwritten.
userData
object
required
The user object to persist. Should contain at minimum email or mobile (for future lookup) and password. Any additional fields are stored as-is.
Returns Promise<{ success: boolean, error?: string }>
const result = await saveUser({
  name: 'Jane Doe',
  email: 'jane@example.com',
  mobile: '0712345678',
  password: 'hunter2',
});

if (!result.success) {
  console.error('Failed to save user:', result.error);
}

getAllUsers()

Reads and deserialises the entire @registered_users array from storage. Returns Promise<UserObject[]> — an empty array [] if no users have been registered yet, or if a read error occurs.
const users = await getAllUsers();
console.log(`${users.length} registered users`);

getUser(emailOrPhone)

Finds a single user by matching the provided string against both user.email and user.mobile fields.
emailOrPhone
string
required
The email address or mobile number to search for. The lookup is case-sensitive and matches using strict equality (===).
Returns Promise<UserObject | undefined>undefined if no matching user is found.
const user = await getUser('jane@example.com');
if (!user) {
  console.log('No account found for that email or phone.');
}

validateUser(emailOrPhone, password)

Looks up a user by emailOrPhone and compares the provided password against the stored value. Returns a structured result rather than throwing on credential failure.
emailOrPhone
string
required
The email address or mobile number to look up.
password
string
required
The plaintext password to compare against user.password.
Returns Promise<{ success: boolean, user?: UserObject, error?: string }>
Scenariosuccessusererror
User found, password matchestrueuser object
User not foundfalse'User not found. Please sign up first.'
Password mismatchfalse'Incorrect password.'
Unexpected errorfalseerror.message

Session Functions

saveLoggedInUser(userData)

Serialises and stores the given user object under @logged_in_user, establishing an active session. Overwrites any previously stored session.
userData
object
required
The user object to store as the active session. Typically the object returned by validateUser or passed into signupUser.
Returns Promise<{ success: boolean, error?: string }>

getLoggedInUser()

Reads the active session from @logged_in_user. Called by the checkAuthSession thunk on every app launch. Returns Promise<UserObject | null>null if no session exists or if a read error occurs.
const user = await getLoggedInUser();
if (user) {
  console.log('Resuming session for', user.name);
} else {
  console.log('No active session — show login screen');
}

logoutUser()

Removes the @logged_in_user key from AsyncStorage, ending the session. Note that this does not remove the user’s account from @registered_users — they can log in again later. Returns Promise<{ success: boolean, error?: string }>
const result = await logoutUser();
if (result.success) {
  console.log('Session cleared');
}

Favourites Functions

saveFavourites(ids)

Serialises the given array of doctor ID strings to @favourite_doctors, replacing any previously saved list.
ids
string[]
required
The complete array of doctor IDs to persist. Pass an empty array [] to clear all favourites.
Returns Promise<void> — errors are caught and logged to the console; no error value is surfaced.
await saveFavourites(['doc_001', 'doc_007', 'doc_042']);

loadFavourites()

Reads and deserialises the favourites list from @favourite_doctors. Returns Promise<string[]> — an empty array [] if no favourites have been saved yet, or if a read error occurs.
const ids = await loadFavourites();
dispatch(setFavourite(ids));

Appointment Functions

saveAppointment(appointment)

Appends a single appointment to the local @appoinments cache. This provides an offline-safe copy independent of the REST API.
appointment
object
required
The appointment object to append. Should match the shape used by addAppointment / the REST API.
Returns Promise<void> — errors are caught and logged; no error value is surfaced.
await saveAppointment({
  id: 'appt_123',
  doctorId: 'doc_007',
  date: '2025-07-20',
  time: '10:00 AM',
  status: 'Upcoming',
});

getAppoinments()

Reads and deserialises the locally cached appointments list from @appoinments.
The function name getAppoinments (single p) reflects the spelling in the source file and must be used exactly as-is when importing. The underlying storage key @appoinments has the same spelling — both are consistent with each other.
Returns Promise<Appointment[]> — an empty array [] if no appointments are cached, or if a read error occurs.
const cached = await getAppoinments();
console.log(`${cached.length} appointments in local cache`);

Typical Usage Example

import {
  validateUser,
  saveLoggedInUser,
} from '../services/storage';

const handleLogin = async (emailOrPhone, password) => {
  const result = await validateUser(emailOrPhone, password);

  if (!result.success) {
    setError(result.error); // 'User not found.' or 'Incorrect password.'
    return;
  }

  await saveLoggedInUser(result.user);
  // Now dispatch checkAuthSession or set state directly
};

All passwords are stored and compared as plaintext strings in @registered_users. This is acceptable for local development and prototyping, but must never be used in production. Before shipping, replace the password field with a hashed value (e.g. using bcrypt or a similar library) and update validateUser to perform a hash comparison rather than direct string equality.

Build docs developers (and LLMs) love