Skip to content

Commit

Permalink
docs: update the AuthVue.component.ts code in vue-authentication-comp…
Browse files Browse the repository at this point in the history
…onents.md to use promises rather than async/await
  • Loading branch information
renanfranca committed Sep 23, 2024
1 parent 4ca760c commit b54516d
Showing 1 changed file with 48 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,45 +77,63 @@ export default defineComponent({
const user = ref<AuthenticatedUser | null>(null);
const isLoading = ref(true);

onMounted(async () => {
await init();
onMounted(() => {
init();
});

const init = async () => {
const init = () => {
isLoading.value = true;
const authenticated = await authRepository.authenticated();
if (authenticated) {
user.value = await authRepository.currentUser();
} else {
user.value = null;
}
isLoading.value = false;
authRepository
.authenticated()
.then(authenticated => {
if (authenticated) {
return authRepository.currentUser();
} else {
return null;
}
})
.then(currentUser => {
user.value = currentUser;
})
.catch(error => {
console.error('Initialization failed:', error);
user.value = null;
})
.finally(() => {
isLoading.value = false;
});
};

const login = async () => {
const login = () => {
isLoading.value = true;
try {
await authRepository.login();
const currentUser = await authRepository.currentUser();
user.value = currentUser.isAuthenticated ? currentUser : null;
} catch (error) {
console.error('Login failed:', error);
user.value = null;
} finally {
isLoading.value = false;
}
authRepository
.login()
.then(() => authRepository.currentUser())
.then(currentUser => {
user.value = currentUser.isAuthenticated ? currentUser : null;
})
.catch(error => {
console.error('Login failed:', error);
user.value = null;
})
.finally(() => {
isLoading.value = false;
});
};

const logout = async () => {
const logout = () => {
isLoading.value = true;
try {
await authRepository.logout();
user.value = null;
} catch (error) {
console.error('Logout failed:', error);
} finally {
isLoading.value = false;
}
authRepository
.logout()
.then(() => {
user.value = null;
})
.catch(error => {
console.error('Logout failed:', error);
})
.finally(() => {
isLoading.value = false;
});
};

return {
Expand Down

0 comments on commit b54516d

Please sign in to comment.