我第一次登录时需要刷新。

huangapple go评论99阅读模式
英文:

I Need Refresh When I First Time Login

问题

I cannot pass auth information to my application without refreshing the page.

登录服务:

  1. loginService(auth: Auth): Observable<ResponseModel> {
  2. return this.apollo
  3. .mutate<any>({
  4. mutation: this.LOGIN_USER,
  5. variables: {
  6. auth,
  7. },
  8. })
  9. .pipe(
  10. map((result) => {
  11. return result.data.login;
  12. })
  13. );
  14. }

登录组件:

  1. login() {
  2. this.isLoading = true;
  3. if (this.form.invalid) {
  4. this._snackBar.open('请填写所有字段。', '确定', {
  5. duration: 3000,
  6. });
  7. this.isLoading = false;
  8. return;
  9. }
  10. const auth = {
  11. email: this.form.value.email,
  12. password: this.form.value.password,
  13. };
  14. this.authService.loginService(auth).subscribe((data) => {
  15. if (!data.isSuccessful) {
  16. this._snackBar.open(data.message, '确定', {
  17. duration: 2000,
  18. });
  19. this.isLoading = false;
  20. return;
  21. }
  22. this._snackBar.open(data.message, '确定', {
  23. duration: 2000,
  24. });
  25. localStorage.setItem('accessToken', data.data);
  26. this.router.navigate(['/']);
  27. this.isLoading = false;
  28. });
  29. }

正如我所说,刷新页面后一切都会解决。

在正常情况下,当我登录网站时,它需要直接从 localStorage 获取 auth 信息,并向 API 报告授权信息。

英文:

When I login, I cannot pass auth information to my application without refreshing the page.

Login service:

  1. loginService(auth: Auth): Observable&lt;ResponseModel&gt; {
  2. return this.apollo
  3. .mutate&lt;any&gt;({
  4. mutation: this.LOGIN_USER,
  5. variables: {
  6. auth,
  7. },
  8. })
  9. .pipe(
  10. map((result) =&gt; {
  11. return result.data.login;
  12. })
  13. );
  14. }

Login component:

  1. login() {
  2. this.isLoading = true;
  3. if (this.form.invalid) {
  4. this._snackBar.open(&#39;L&#252;tfen t&#252;m alanları doldurun.&#39;, &#39;Tamam&#39;, {
  5. duration: 3000,
  6. });
  7. this.isLoading = false;
  8. return;
  9. }
  10. const auth = {
  11. email: this.form.value.email,
  12. password: this.form.value.password,
  13. };
  14. this.authService.loginService(auth).subscribe((data) =&gt; {
  15. if (!data.isSuccessful) {
  16. this._snackBar.open(data.message, &#39;Tamam&#39;, {
  17. duration: 2000,
  18. });
  19. this.isLoading = false;
  20. return;
  21. }
  22. this._snackBar.open(data.message, &#39;Tamam&#39;, {
  23. duration: 2000,
  24. });
  25. localStorage.setItem(&#39;accessToken&#39;, data.data);
  26. this.router.navigate([&#39;/&#39;]);
  27. this.isLoading = false;
  28. });
  29. }

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=&#39;/&#39; 时,问题解决了。

英文:

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=&#39;/&#39;.

huangapple
  • 本文由 发表于 2023年5月7日 20:31:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193944.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定