Вам нужно вернуть либо обещание, либо наблюдаемое, так как вы выполняете какой-то асин c код, а затем немедленно его игнорируете.
Код в том виде, в каком он есть, будет эффективно игнорировать ваш onSuccess и всегда будет возвращать неопределенное («значение» isLogged
);
Вместо этого верните наблюдаемое (аналог обещания - вернет результат в какой-то момент в будущем), а затем передайте значение в субъект изнутри обратный вызов onsuccess.
canActivate(): Observable<boolean> {
// return an observable - a value that will return at some point in the future
return this.checkIfUserIsLoggedIn().pipe(
tap(loggedIn => {
// it has now run. tap is one way of getting to the result and doing something
if(!loggedIn) {
this.router.navigateByUrl('/login');
}
});
);
}
private checkIfUserIsLogged(): Observable<boolean> {
if (!window.indexedDB) {
console.log(`Your browser doesn't support a stable version of IndexedDB.`);
return of(false);
}
const subject: Subject<boolean> = new Subject<boolean>();
const request = indexedDB.open(DB.NAME);
request.onsuccess = () => {
const userName = localStorage.getItem('user');
const db = request.result;
const tx = db.transaction(DB.OBJECT_STORE, 'readwrite');
const store = tx.objectStore(DB.OBJECT_STORE);
const logoutRequest = store.get(userName);
logoutRequest.onsuccess = () => {
console.log(logoutRequest.result.isLogged);
const isLogged = logoutRequest.result.isLogged;
// Trigger the observable. The calling code will now run the pipe.
subject.next(isLogged);
subject.complete();
};
};
return subject.asObservable();
}