Hello,
I’m facing an issue with the Authentication in my web app. After a user signs in, the token is sometimes not retrieved correctly from localStorage, leading to unauthorized access errors.
Here’s the problematic code, how to deal it?
// Function to handle user sign-in
async function signIn(email, password) {
try {
const user = await auth.signIn({ username: email, password: password });
console.log(‘User signed in:’, user);
// Store the token
localStorage.setItem(‘authToken’, user.token);
} catch (error) {
console.error(‘Sign-in error:’, error);
}
}
// Function to retrieve the token
function getToken() {
try {
const token = localStorage.getItem(‘authToken’);
if (!token) {
throw new Error(‘No token found’);
}
return token;
} catch (error) {
console.error(‘Token retrieval error:’, error);
}
}