jCtapuk
jCtapuk7mo ago

Token after repeated sign-in

@Michal Srb I have one more question. If the user is already authorized as anonymous and when he enters his email and password, does it generate a new token or remain with the old one? Because the setAuth function doesn’t work I had to install a new function because the token changed
3 Replies
Michal Srb
Michal Srb7mo ago
Yes I think the token would change The anonymous sign in is not fully finished yet (and not documented)
jCtapuk
jCtapukOP7mo ago
Well, it turns out that I didn’t run the call function, which manually overrides the new function for changing update events. I just monitored the change in the token, if it changed, I rewrote the function
watch(token, () => {
client.setAuth((fetchRefreshToken) => {
// CODE REFRESH TOKEN
return token.value
})
})

client.onUpdate(api.auth.user, (data) => {
user.value = data
})
watch(token, () => {
client.setAuth((fetchRefreshToken) => {
// CODE REFRESH TOKEN
return token.value
})
})

client.onUpdate(api.auth.user, (data) => {
user.value = data
})
This function is triggered when I change client.setAuth forcibly if an anonymous person has logged in and registered an account email and password. Sometimes the token disappears when you re-authorize (with an anonymous account).
watch(
token,
() => {
client.setAuth(async ({ forceRefreshToken }) => {
if (forceRefreshToken && refreshToken.value) {
let resetToken = false;

try {
const { tokens } = await client.action(api.auth.signIn, {
refreshToken: refreshToken.value,
});

if (tokens) {
token.value = tokens.token;
refreshToken.value = tokens.refreshToken;
} else {
resetToken = true;
}
} catch (e) {
resetToken = true;
} finally {
if (resetToken) {
token.value = undefined;
refreshToken.value = undefined;
}
}
}

return token.value;
});
},
{ immediate: true },
);
watch(
token,
() => {
client.setAuth(async ({ forceRefreshToken }) => {
if (forceRefreshToken && refreshToken.value) {
let resetToken = false;

try {
const { tokens } = await client.action(api.auth.signIn, {
refreshToken: refreshToken.value,
});

if (tokens) {
token.value = tokens.token;
refreshToken.value = tokens.refreshToken;
} else {
resetToken = true;
}
} catch (e) {
resetToken = true;
} finally {
if (resetToken) {
token.value = undefined;
refreshToken.value = undefined;
}
}
}

return token.value;
});
},
{ immediate: true },
);
Added immediate properties and now the token does not disappear
jCtapuk
jCtapukOP7mo ago
server http + client reactive

Did you find this page helpful?