У меня есть страница входа в мое приложение.
Это часть функции журнала: < /p>
this.loginService.doLogin(this.request).then(res => {
this.storage.set("auth-key", res.token);
this.authService.identity();
if (this.authService.isAuthenticated())
this.router.navigate(["expert-profile", this.authService.getCurrentUser().id], { queryParams: { lang: this.lang } });
return;
} else {
this.storage.remove("auth-key");
this.router.navigateByUrl("/login");
}
}).catch(err => {
this.storage.remove("auth-key");
});
identity() {
if (this.storage.get("auth-key")) {
this.fetch().then(res => {
this.userIdentity = {
id: res.id,
isAuthorized: res.isAuthorized
};
sessionStorage.setItem("userLoggedId", JSON.stringify(this.userIdentity.id));
this.authenticated = true;
}).catch(err => {
this.storage.remove("auth-key");
sessionStorage.removeItem("userLoggedId");
this.authenticated = false;
});
public async fetch(): Promise {
const response = this.http.get(environment.userDetailsUrl, { headers: new HttpHeaders({ "Authorization": "Bearer " + this.storage.get("auth-key") }) });
return await firstValueFrom(response);
< /code>
} < /p>
У меня есть следующая проблема. Чтобы войти в систему, я должен нажать кнопку входа в систему два раза, потому что в первый раз, когда ответ с функции Fetch, вероятно, не появится во времени (это.>
Подробнее здесь: https://stackoverflow.com/questions/796 ... sync-await
Ожидание ответа в функции входа в систему асинхро ⇐ Javascript
Форум по Javascript
-
Anonymous
1746443816
Anonymous
У меня есть страница входа в мое приложение.
Это часть функции журнала: < /p>
this.loginService.doLogin(this.request).then(res => {
this.storage.set("auth-key", res.token);
this.authService.identity();
if (this.authService.isAuthenticated())
this.router.navigate(["expert-profile", this.authService.getCurrentUser().id], { queryParams: { lang: this.lang } });
return;
} else {
this.storage.remove("auth-key");
this.router.navigateByUrl("/login");
}
}).catch(err => {
this.storage.remove("auth-key");
});
identity() {
if (this.storage.get("auth-key")) {
this.fetch().then(res => {
this.userIdentity = {
id: res.id,
isAuthorized: res.isAuthorized
};
sessionStorage.setItem("userLoggedId", JSON.stringify(this.userIdentity.id));
this.authenticated = true;
}).catch(err => {
this.storage.remove("auth-key");
sessionStorage.removeItem("userLoggedId");
this.authenticated = false;
});
public async fetch(): Promise {
const response = this.http.get(environment.userDetailsUrl, { headers: new HttpHeaders({ "Authorization": "Bearer " + this.storage.get("auth-key") }) });
return await firstValueFrom(response);
< /code>
} < /p>
У меня есть следующая проблема. Чтобы войти в систему, я должен нажать кнопку входа в систему два раза, потому что в первый раз, когда ответ с функции Fetch, вероятно, не появится во времени (это.>
Подробнее здесь: [url]https://stackoverflow.com/questions/79606815/waiting-for-response-in-login-function-async-await[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия