英文:
I Need Refresh When I First Time Login
问题
I cannot pass auth
information to my application without refreshing the page.
登录服务:
loginService(auth: Auth): Observable<ResponseModel> {
return this.apollo
.mutate<any>({
mutation: this.LOGIN_USER,
variables: {
auth,
},
})
.pipe(
map((result) => {
return result.data.login;
})
);
}
登录组件:
login() {
this.isLoading = true;
if (this.form.invalid) {
this._snackBar.open('请填写所有字段。', '确定', {
duration: 3000,
});
this.isLoading = false;
return;
}
const auth = {
email: this.form.value.email,
password: this.form.value.password,
};
this.authService.loginService(auth).subscribe((data) => {
if (!data.isSuccessful) {
this._snackBar.open(data.message, '确定', {
duration: 2000,
});
this.isLoading = false;
return;
}
this._snackBar.open(data.message, '确定', {
duration: 2000,
});
localStorage.setItem('accessToken', data.data);
this.router.navigate(['/']);
this.isLoading = false;
});
}
正如我所说,刷新页面后一切都会解决。
在正常情况下,当我登录网站时,它需要直接从 localStorage
获取 auth 信息,并向 API 报告授权信息。
英文:
When I login, I cannot pass auth
information to my application without refreshing the page.
Login service:
loginService(auth: Auth): Observable<ResponseModel> {
return this.apollo
.mutate<any>({
mutation: this.LOGIN_USER,
variables: {
auth,
},
})
.pipe(
map((result) => {
return result.data.login;
})
);
}
Login component:
login() {
this.isLoading = true;
if (this.form.invalid) {
this._snackBar.open('Lütfen tüm alanları doldurun.', 'Tamam', {
duration: 3000,
});
this.isLoading = false;
return;
}
const auth = {
email: this.form.value.email,
password: this.form.value.password,
};
this.authService.loginService(auth).subscribe((data) => {
if (!data.isSuccessful) {
this._snackBar.open(data.message, 'Tamam', {
duration: 2000,
});
this.isLoading = false;
return;
}
this._snackBar.open(data.message, 'Tamam', {
duration: 2000,
});
localStorage.setItem('accessToken', data.data);
this.router.navigate(['/']);
this.isLoading = false;
});
}
Like i said everything is resolved when i refresh the page.
Under normal conditions, when I log in to the site, it needs to get the auth information directly from the localStorage
and report the authorized information to the API.
答案1
得分: 1
我解决了这个问题,实际上我遇到这个问题是因为我忽视了一个非常简单的东西。在登录时,我只使用了按钮的点击事件进行重定向。当我在按钮上加了一个标签并设置 href='/'
时,问题解决了。
英文:
I solved the problem, actually I was having this problem because I overlooked something very simple. While logging in, I was only using the click event on the button I was redirecting. The problem was solved when I put a tag in it and made href='/'
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论