PESolut / leaf-me

new and final mono repo for the leaf me system: Leaf-Me is a full-stack Uber Eats-style delivery platform designed for dispensaries. The app streamlines the process of placing, preparing, and delivering orders through multiple role-based interfaces
0 stars 0 forks source link

feature: a user is directed to sign in when trying to access features that requires one to be signed in #25

Open PESolut opened 1 month ago

PESolut commented 1 month ago

A user should be able to freely browse through the application without signing in. However, upon attempting to add an item to their cart, they should be prompted to sign in. This ensures users are authenticated before they engage in purchase-related actions.

Acceptance Criteria

  1. Users can browse the app without signing in.
  2. Attempting to add an item to the cart triggers a redirect to the sign-in page.
  3. Sign-in and sign-up options are displayed on the redirected page to facilitate user authentication

Tasks

PESolut commented 1 month ago

done: if a user is not signed in, nothing will happen when adding items to cart next step: a user is redirected to the sign in view upon adding item when not signed in note: i remember issues with navigation; does navigation.push work here or should we clear the navigation stack before pushing the sign in page; intuitively it makes sense to clear the stack.

PESolut commented 1 month ago

NavBarButton > handlePress snippet

navigation.dispatch(
                    CommonActions.reset({
                      index: 0,
                      routes: [{ name: 'Dispensaries' }],
                    }))

when we navigate; we want to clear the stack since we are changing views, replace Dispensaries with SignIn

PESolut commented 1 month ago

done: a user is redirected to the sign in componenent upon adding a item to the basket next step: populate the sign in comp with unstyled html data

PESolut commented 1 month ago

done: ' unstyled html added to signin ' next step: style the sign in

PESolut commented 1 month ago

done ' email stage of sign in complete ' next step: design and formulate the sign in system logic notes: Image i see the system as the following: a user is directed to the sign in view 1)upon component mount; state is init for stage and new default state for stage is 1 and default state for new is null 2) user is asked for input; upon input given; check input 3) if email input exists; go to stage 2, if not, go to stage 2 with new state set as true 4) if stage==2, dynamic render signInForm2.js prompting for user input for password; upon input given; 5)checkinput, if new == null, find user that has that password; if not found, take input again. 5a) if found, set stage == 0 6) checkinput, if new == true 7) if stage == 0, take userInput and based on new value, either post a new user and sign that user in and redirect to home or sign in that user and redirect to home

i've implemented the basis of this system; all that is left is to :

these functions will happen on the backend as the following:

  1. Check Email (GET /api/users/checkemail/:usersEmail)
  2. Verify User (POST /api/users/verify)
  3. Create User (POST /api/users)
PESolut commented 1 month ago

create checkEmailExists() function to verify if email exists

on the backend we have four functions for handling auth; doesAccountExist might handle the functionality we need....

const doesAccountExist = async (req, res, next) => {
  const { email } = req.body;
  const credentials = await getLoginByEmail(email)
  // if there is no error message = there is an account, so send error message
  if(credentials.email) {
    res.status(401).json({ error: ` there is an account already linked to ${email}`})
  } else {
    console.log('account does not exist')
    next();
  }
}

I'm sure if i change this, something somewhere else will break... we can however; send a request to this and work with the response for our functionality

PESolut commented 1 month ago

backend crashed when peforming request with just the email... upon checking the logs it seems its the request to check the email without a value for the password causing the error

/app/node_modules/bcrypt/bcrypt.js:144
        error = new Error('data must be a string or Buffer and salt must either be a salt string or a number of rounds');
                ^

Error: data must be a string or Buffer and salt must either be a salt string or a number of rounds
    at Object.hash (/app/node_modules/bcrypt/bcrypt.js:144:17)
    at /app/node_modules/bcrypt/promises.js:29:12
    at new Promise (<anonymous>)
    at module.exports.promise (/app/node_modules/bcrypt/promises.js:20:12)
    at Object.hash (/app/node_modules/bcrypt/bcrypt.js:133:25)
    at /app/middleware/auth.js:50:10

Email Check Endpoint

// CHECK IF USER EXISTS
clientUser.post("/check-email", doesAccountExist, async (req, res) => {
    // If middleware passes (account doesn't exist), send success response
    res.status(200).json({ message: "Email is available" });
});

Added new route to check email availability without requiring password:

Created this instead of modifying registration endpoint to handle missing password cases.

... Reflecting these changes on backend then going to retry the stage 1 of login system

success! a user can go through stage 1 of the login system, email is being checked actively with 1 revision needed (outside the scope of this ticket so ill complete later ) testing that our 'new' state works as expected

PESolut commented 1 month ago

Login System Enhancement - Stage 2 Implementation

✅ DONE:

A user can now successfully trigger loginForm2 after completing stage 1 of the login system. The transition is smooth and provides clear visual feedback.

🎯 Next Step:

Implement the basic structure of loginForm2 by:

📝 Notes:

When proceeding beyond stage 2, we have access to:

🖼️ Development Progress:

Console Log iPhone Demo Video
PESolut commented 1 month ago

signInFormButton multi-use functionality

At its current capacity; the signInFormButton only submits to email; lets make it so we can define a type. for now this button can have two types: email or password.

...

PESolut commented 1 month ago

done: 'signInFromButton multi-use...' next: actually post new accounts; then sign in.

PESolut commented 1 month ago

client user controller > create / register route

Route

{API}/users/ POST 

controller code

clientUser.post("/", hashPass, doesAccountExist, async (req, res) => {
    const newClientUser = await createClientUser(req.body)

    if(!newClientUser.message){
        res.status(200).json(newClientUser)
    }
    else {
        res.status(500).json({error: newClientUser.message})
    }
})

createClientUser query

const createClientUser = async (clientUser) => {
  try {

    const newClientUser = await db.one(
      "INSERT INTO client_user (Name, Address, Latitude, Longitude, email, password) VALUES ($1, $2, $3, $4, $5, $6) RETURNING *",
      [
        clientUser.name,
        clientUser.address,
        clientUser.latitude,
        clientUser.longitude,
        clientUser.email,
        clientUser.password
      ]
    );
    return newClientUser;
  } catch (error) {
    return error;
  }
};

next steps

lets get into signInForm2, build our userDetails state and submit will blank values for name address and lat and long.

PESolut commented 1 month ago

creating createNewUser function within SignIn.js to post to BE

PESolut commented 4 weeks ago

hashPass middleware is not properly handling the Promise chain. It's not waiting for the hash to be set before calling next()... fixing...

PESolut commented 4 weeks ago

done: hashpass bugfix: nextsteps: test createNewUser Functionality.

PESolut commented 4 weeks ago

done: a user is able to register a new account and is redirected without their token next-step: log the new user in after registering by attaching the res.response.token to state userID within provider