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

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

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&lt;ResponseModel&gt; {
    return this.apollo
      .mutate&lt;any&gt;({
        mutation: this.LOGIN_USER,
        variables: {
          auth,
        },
      })
      .pipe(
        map((result) =&gt; {
          return result.data.login;
        })
      );
  }

Login component:

login() {
    this.isLoading = true;

    if (this.form.invalid) {
      this._snackBar.open(&#39;L&#252;tfen t&#252;m alanları doldurun.&#39;, &#39;Tamam&#39;, {
        duration: 3000,
      });
      this.isLoading = false;
      return;
    }

    const auth = {
      email: this.form.value.email,
      password: this.form.value.password,
    };

    this.authService.loginService(auth).subscribe((data) =&gt; {
      if (!data.isSuccessful) {
        this._snackBar.open(data.message, &#39;Tamam&#39;, {
          duration: 2000,
        });
        this.isLoading = false;
        return;
      }
      this._snackBar.open(data.message, &#39;Tamam&#39;, {
        duration: 2000,
      });
      localStorage.setItem(&#39;accessToken&#39;, data.data);

      this.router.navigate([&#39;/&#39;]);

      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=&#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:

确定