amark / gun

An open source cybersecurity protocol for syncing decentralized graph data.
https://gun.eco/docs
Other
18.05k stars 1.16k forks source link

user.is still undefined even after user logs in #1206

Closed Jammooly1 closed 2 years ago

Jammooly1 commented 2 years ago

Hello Gun community, gunjs has been really cool to use but I have a problem validating whether a user is logged in because user.is is acting funny.

I'm calling user.auth(username, password, ({err}) => err && alert(err)); in my code but right after this line, I console.log(user.is) and it says undefined. I know 100% that I'm entering the correct credentials, any thoughts on why user.is is showing undefined?

For example, in my code below console.log prints user.is is undefined but in the next console.log line, there's another console.log line that prints the user object and it contains is along with the alias, pub, ... inside of it.

    const login = (username, password) => {
    console.log('trying to log in')
    try {
        user.auth(username, password, ({err}) => err && alert(err));
        console.log('logged in')
        console.log('user.is is ', user.is)
        console.log('user object is ', user)

        store.state.cur_user = true

    } catch (error) {
        console.log('error logging in')
        store.state.cur_user = false;
    }
    //var x = router.push('/')
    //console.log('x: ', x)
    //console.log('router pushed')
}

Code above found here: https://github.com/Jammooly1/vue3-gunjs-Todo-App/blob/main/src/composables/useLogin.js

This is my project: https://github.com/Jammooly1/vue3-gunjs-Todo-App

atordvairn commented 2 years ago

You have to keep the stuff in the callback i guess

user.auth(username, password, ()=>{
console.log('logged in')
        console.log('user.is is ', user.is)
        console.log('user object is ', user)

        store.state.cur_user = true
});

This may help

atordvairn commented 2 years ago

there is a workaround for this though,

var userObj = JSON.parse(sessionStorage.getItem("pair"))

make sure to handle userObj.priv (private key) safely I have no idea if this is secure or recommended. It just works :)

amark commented 2 years ago

@Jammooly1 looks like @atordvairn helped? Thanks 👏 ! Closing. Please reopen if bug.