frankcollins3 / Next-Water-App

Happy, Healthy Water Cycling App that tracks user/human fluid intake.
https://next-water-app.vercel.app
1 stars 0 forks source link

splitting token worked and now is returning 't' instead of {userid} [10:15pm] #74

Closed frankcollins3 closed 11 months ago

frankcollins3 commented 11 months ago

attempting to do: a "remember me" function that allows user to log in based on their last session

error: splitting token worked and now is returning 't' instead of {userid}

preCookie ['token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI…Q.CgCRWfRU-sJ3dWjIKcr7Eb-mrc85LCW2d5d5qO2Pa08id:7'] LogInOutGoogle2.tsx:56 pageCookie token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NywiaWF0IjoxNjg5OTAwODM0fQ.CgCRWfRU-sJ3dWjIKcr7Eb-mrc85LCW2d5d5qO2Pa08id:7 cookies.js:25 splitName ['t']0: "t"length: 1[[Prototype]]: Array(0)

heres the function that was originally going to be a lastIndexOf(":") and substring()
last time: splitNameForId: ${token} ${id}

export function userIdFromCookie(name) {
    name = name[0]
    const splitNameForId = name.split("id:")
    console.log('splitName', splitNameForId)

    let userId = splitNameForId[1]
    console.log('userId from cookies func', userId)
    return userId
    // let preregexID = name[0].substring(preIdIndex + 1);
}

proposed approach: instead of "id:${userId}" ---> ***${userid} I thought this messing up meant that somewhere within the token there was the string of "id" but that isn't so.

frankcollins3 commented 11 months ago

exception handler needs empty str because page will have empty string cookie not null if empty. if (userId === null || userId === undefined || userId === ' ') return null [10:22pm]

dodging null && undefined errors but have to handle splitting string when there is cookie

frankcollins3 commented 11 months ago

👍 working but that's what happened last time. .split("***") put up a little more fight than .split("id:") Screen Shot 2023-07-20 at 10 25 20 PM [10:26pm]

frankcollins3 commented 11 months ago

just broke again and then wroked again [10:28pm]

frankcollins3 commented 11 months ago
  export function userIdFromCookie(name) {
    console.log('name', name)
    if (name === null || name === undefined || name === '') return null

👍     const splitChar = name.split('***')
    const userId = splitChar[1]

    return userId ? userId : null    
    // let preregexID = name[0].substring(preIdIndex + 1);
}

Screen Shot 2023-07-20 at 10 40 30 PM

can see the *** separating the last number in the set of characters with the preceding string to the left of it.

this particular token which is generated from crpyto.randomBytes() ends in: 👍 "tc***8"

[10:42pm]

frankcollins3 commented 11 months ago

👍 if (name[0] !== undefined || name[0] !== undefined || name[0] !== ' ') { const splitChar = name[0].split('') const userId = splitChar[1] return userId ? userId : null
// const splitChar = name[0].split('
') || name.split('***') } else { console.log("hey were over here") }

👍 this looks to work most consistently out of every approach but might reopen [11:11pm]

frankcollins3 commented 11 months ago

considering storing 2 cookies: 1 for the token one for the userID [11:20pm]

frankcollins3 commented 11 months ago

👍 other way approach is insane when given additional thought.. cookie (2) ['token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI…DI2fQ.4UUlswdKJka10G0Ag65o_W7XIpGQXpLBD2SKChLWhnE', '8']

sites say: do you accept "cookies" plural. probably not an array