首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在第二个API调用之前,NuxtJS 8月重定向。

在第二个API调用之前,NuxtJS 8月重定向。
EN

Stack Overflow用户
提问于 2022-05-24 18:57:16
回答 1查看 131关注 0票数 0

当我在登录页面上运行以下方法时,会发现一些奇怪的行为。在调用login()方法之后,我想运行一个单独的调用来获取概要文件的详细信息和权限,然后重定向到/home,而是在返回第二个api调用之前直接重定向到/

代码语言:javascript
复制
attempt() {
      try {
        let data = {
          email: this.creds.email,
          password: this.creds.password
        }

        this.$auth.login({data: data}).then(() => {
          //I want this API call to run BEFORE redirecting
          this.axios.get(this.makeAuthUrl('user/'+process.env.NUXT_ENV_COMMUNITY_ID), this.getHeaders(false)).then(user => {
            console.log(user.data)
            this.$store.commit("SET_PROFILE", user.data.profile)
            this.$store.commit("SET_ABILITIES", user.data.abilities)
            this.actionLoading = false;
            this.$router.push({path: "/home"})
          })
        })

      } catch (e) {
        console.log(e)
        this.actionLoading = false;
        this.$toast.error("Invalid Credentials!");
      }
    }

那么,我如何修改代码以延迟重定向,直到我显式地重定向用户?

使用nuxtjs/auth模块

谢谢!编辑添加的异步等待解释

代码语言:javascript
复制
async attempt() {
      try {
        let response = await this.$auth.loginWith('local', {
          data: {
              email: this.creds.email,
              password: this.creds.password
            }
        })
        this.axios.get(this.makeAuthUrl('user/'+process.env.NUXT_ENV_COMMUNITY_ID), this.getHeaders(false)).then(user => {
          console.log(user)
          this.$store.commit("SET_PROFILE", user.data.profile)
          this.$store.commit("SET_ABILITIES", user.data.abilities)
          this.actionLoading = false;
          this.$router.push({path: "/"});
        })
      } catch (e) {
        console.log(e)
        this.actionLoading = false;
        this.$toast.error("Invalid Credentials!");
      }
    }

编辑2

代码语言:javascript
复制
try {
        let response = await this.$auth.loginWith('local', {
          data: {
            email: this.creds.email,
            password: this.creds.password
          }
        })
        let response2 = await this.axios.get(this.makeAuthUrl('user/'+process.env.NUXT_ENV_COMMUNITY_ID), this.getHeaders(false))
        if(response2.status === 200) {
          console.log(response2.data)
          this.$store.commit("SET_PROFILE", response2.data.profile)
          this.$store.commit("SET_ABILITIES", response2.data.abilities)
          await this.$router.push({path: "/"});
        }
      } catch (e) {
        console.log(e)
        this.actionLoading = false;
        this.$toast.error("Invalid Credentials!");
      }
EN

回答 1

Stack Overflow用户

发布于 2022-05-24 20:13:34

修正如下:

代码语言:javascript
复制
async attempt() {
      try {
        let data = {
          email: this.creds.email,
          password: this.creds.password
        }
        let response = await this.axios.post(this.makeAuthUrl('login'), data, this.getHeaders(false))
        if(response.status === 200) {
          this.$auth.strategy.token.set(response.data.token)
        }
        let getUser = await this.axios.get(this.makeAuthUrl('user/'+process.env.NUXT_ENV_COMMUNITY_ID), this.getHeaders(false))
        if(getUser.status === 200) {
          this.$auth.setUser(getUser.data.user)
          this.$store.commit("SET_PROFILE", getUser.data.profile)
          this.$store.commit("SET_ABILITIES", getUser.data.abilities)
          return this.$router.push({path: "/"});
        }
      } catch (e) {
        this.actionLoading = false;
        this.$toast.error("Invalid Credentials!");
      }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72368124

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档