Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/BhushanBadhe39/SkinFirts/llms.txt

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

src/services/authService.js is the single source of truth for all authentication logic in SkinFirts. It sits between the UI layer and the raw API functions, coordinating three responsibilities in each operation: calling the relevant API function, persisting the result to AsyncStorage under the key @user_account_details, and keeping the Redux store in sync by dispatching the appropriate UserSlice action. All four exported functions are async and designed to be called with the Redux dispatch function obtained from useDispatch().

Imports

src/services/authService.js
import AsyncStorage from '@react-native-async-storage/async-storage';
import { clearUser, setUser } from '../redux/Slices/UserSlice';
import { createUser, fetchUsers, updateUser } from '../api/usersApi';

Functions


signUpUser(formData, dispatch)

Registers a new user by calling createUser, then immediately persists the returned user object to AsyncStorage and dispatches setUser so the Redux store reflects the authenticated session. On success it always resolves to true.
export const signUpUser = async (formData, dispatch) => {
    const newUser = await createUser(formData);
    await AsyncStorage.setItem('@user_account_details', JSON.stringify(newUser));
    dispatch(setUser(newUser));
    return true;
};
formData
object
required
Registration fields that match the user schema — typically name, email, and password collected from the sign-up form. These are forwarded directly to createUser, which merges them with the pre-defined appointment seed fields before posting to the API.
dispatch
function
required
The Redux dispatch function obtained from useDispatch(). Used to call setUser(newUser) after a successful registration, placing the user in the store and setting isLoggedIn = true.
return
Promise<true>
Always resolves to true when the API call and storage write succeed. Any network or storage error will cause the promise to reject.
Side effects:
  • Calls createUser(formData) → POST ./users/
  • Writes @user_account_details to AsyncStorage
  • Dispatches setUser(newUser) to the Redux store
Example usage:
import { signUpUser } from '../services/authService';
import { useDispatch } from 'react-redux';

export default function SignUpScreen() {
  const dispatch = useDispatch();

  const handleSignUp = async (formData) => {
    const success = await signUpUser(formData, dispatch);
    if (success) {
      // Navigate to home screen
    }
  };
}

loginUser(email, password, dispatch)

Authenticates an existing user by fetching all users from the API, then finding the one whose email matches (case-insensitive, trimmed) and whose password matches exactly. If a matching user is found, the record is written to AsyncStorage and dispatched to the Redux store. Returns false if no match is found or if the network request fails.
export const loginUser = async (email, password, dispatch) => {
    try {
        const users = await fetchUsers();
        const authUser = users.find(
            user =>
                user.email.toLowerCase() === email.trim().toLowerCase() &&
                user.password === password
        );
        if (!authUser) return false;
        await AsyncStorage.setItem('@user_account_details', JSON.stringify(authUser));
        dispatch(setUser(authUser));
        return true;
    } catch (error) {
        console.log('Error fetching data' + error);
        return false;
    }
};
email
string
required
The user’s email address. Compared case-insensitively after trimming leading and trailing whitespace — " User@Example.com " will match "user@example.com" in the database.
password
string
required
The user’s password. Compared with strict equality (===) against the stored value; no hashing is applied at the client level.
dispatch
function
required
The Redux dispatch function obtained from useDispatch(). Dispatches setUser(authUser) on a successful match.
return
Promise<boolean>
Resolves to true when a matching user is found and the session is persisted. Resolves to false if no matching user is found or if an error is thrown during the fetch.
Side effects:
  • Calls fetchUsers() → GET ./users
  • Writes @user_account_details to AsyncStorage on success
  • Dispatches setUser(authUser) on success
Example usage:
import { loginUser } from '../services/authService';
import { useDispatch } from 'react-redux';

export default function LoginScreen() {
  const dispatch = useDispatch();

  const handleLogin = async (email, password) => {
    const success = await loginUser(email, password, dispatch);
    if (!success) {
      Alert.alert('Login failed', 'Invalid email or password.');
    }
  };
}

editUserDetails(id, updates, dispatch)

Updates an existing user’s profile by calling updateUser, then synchronises both AsyncStorage and the Redux store with the fresh data returned by the API. Returns true on success.
export const editUserDetails = async (id, updates, dispatch) => {
    try {
        const newUser = await updateUser(id, updates);
        if (!newUser) return false;
        await AsyncStorage.setItem('@user_account_details', JSON.stringify(newUser));
        dispatch(setUser(newUser));
        return true;
    } catch (error) {
        console.log('Error');
    }
};
id
string
required
The user’s unique ID (the numeric string assigned by MockAPI, e.g. "7"). Passed directly as the path parameter to updateUser.
updates
object
required
A partial user object containing only the fields to change. For example, { name: "Alex" } updates just the name without affecting other fields.
dispatch
function
required
The Redux dispatch function obtained from useDispatch(). Dispatches setUser(newUser) with the server’s updated user object.
return
Promise<boolean>
Resolves to true when the API call succeeds and the updated user is persisted. Returns false if updateUser returns a falsy value. Returns undefined if an error is caught (error is currently only logged).
Side effects:
  • Calls updateUser(id, updates) → PATCH ./users/:id
  • Overwrites @user_account_details in AsyncStorage with the updated user
  • Dispatches setUser(newUser) to the Redux store
Example usage:
import { editUserDetails } from '../services/authService';
import { useDispatch, useSelector } from 'react-redux';

export default function EditProfileScreen() {
  const dispatch = useDispatch();
  const user = useSelector(state => state.user.user);

  const handleSave = async (updates) => {
    const success = await editUserDetails(user.id, updates, dispatch);
    if (success) {
      // Show success feedback
    }
  };
}

logoutUser(dispatch)

Ends the authenticated session by removing @user_account_details from AsyncStorage and dispatching clearUser(), which resets the Redux user state to null and sets isLoggedIn to false.
export const logoutUser = async (dispatch) => {
    await AsyncStorage.removeItem('@user_account_details');
    dispatch(clearUser());
};
dispatch
function
required
The Redux dispatch function obtained from useDispatch(). Dispatches clearUser() to wipe the user object from the store.
return
Promise<void>
Resolves once AsyncStorage.removeItem completes and clearUser has been dispatched. Does not return a value.
Side effects:
  • Removes @user_account_details from AsyncStorage
  • Dispatches clearUser() — sets state.user.user = null and state.user.isLoggedIn = false
Example usage:
import { logoutUser } from '../services/authService';
import { useDispatch } from 'react-redux';

export default function ProfileScreen({ navigation }) {
  const dispatch = useDispatch();

  const handleLogout = async () => {
    await logoutUser(dispatch);
    navigation.reset({ index: 0, routes: [{ name: 'Login' }] });
  };
}
Always await logoutUser(dispatch) before performing a navigation reset. Calling navigation reset synchronously without awaiting may cause the new screen to briefly read stale AsyncStorage data before the removal completes.

Build docs developers (and LLMs) love