简体   繁体   English

如何使用 nuxt.auth 以用户身份登录?

[英]How can I log in as a user with nuxt.auth?

I currently have the following nuxt.auth configuration.我目前有以下 nuxt.auth 配置。

auth: {
  strategies: {
    cookie: {
      endpoints: {
        login: { url: '/api/login', method: 'post' },
      },
    },
  },
},

When login is ok, the response is in json format with the following data登录成功后,响应为 json 格式,数据如下

{'user': 'Tlaloc-Es'}

On the login page I have the following code:在登录页面上,我有以下代码:

this.$auth
  .loginWith('cookie', {
    data: {
      email: this.user_email,
      password: this.user_password,
      remember: this.remember,
    },
  })
  .then((data) => {
     const response = data.data.data;
     this.$auth.setUser(response.user);
     console.log(response.user);
     console.log(this.$auth.loggedIn);
  });

The problem is this.$auth.loggedIn always returns false .问题是this.$auth.loggedIn总是返回false

I guess that auth doesn't set the user as logged, but I don't know any other steps I need part of:我猜 auth 不会将用户设置为已登录,但我不知道我需要的任何其他步骤:

this.$auth.setUser(response.user);

After a call, logging in browser stores the following cookies:调用后登录浏览器存储如下cookies:

auth._token.cookie -> true
session -> session token
auth.strategy -> 'cookie'
auth._token_expiration.cookie -> false

How can I set the user as logged?如何将用户设置为已登录?

EDIT编辑

If I execute the logout this value如果我执行注销此值

auth._token.cookie

turn to false, but the session still is stored and anyway转为假,但 session 仍然被存储,无论如何

this.$auth.loggedIn

return false.返回假。

EDIT Another try:编辑另一个尝试:

auth: {
    redirect: {
      login: '/login',
      logout: '/login',
      home: '/',
    },
    strategies: {
      cookie: {
        cookie: {
          name: 'session',
        },
        user: {
          property: false,
          autoFetch: false,
        },
        endpoints: {
          login: { url: '/api/login', method: 'post' },
          logout: { url: '/api/logout', method: 'post' },
        },
      },
    },
  },


async signIn() {
      const succesfulLogin = await this.$auth.loginWith('cookie', {
        data: {
          email: this.user_email,
          password: this.user_password,
          remember: this.remember,
        },
      });
      if (succesfulLogin) {
        const response = succesfulLogin.data.data;
        await this.$auth.setUser({ user: response.user });
        console.log(this.$auth.loggedIn);
        //await this.$auth.logout();
      }
    },

This is after login:这是登录后:

在此处输入图像描述

reponse cookie响应cookie

在此处输入图像描述

Thanks.谢谢。

you should try setting set this.$auth.loggedIn = true to true after receiving the data您应该在收到数据后尝试将 set this.$auth.loggedIn = true设置为 true

this.$auth
    .loginWith('cookie', {
      data: {
        email: this.user_email,
        password: this.user_password,
        remember: this.remember,
      },
    })
    .then((data) => {
      const response = data.data.data;
      this.$auth.setUser(response.user);
      this.$auth.loggedIn = true
      console.log(response.user);
      console.log(this.$auth.loggedIn);

    });

Finally works with the following configuration:最后使用以下配置:

auth: {
  redirect: {
    login: '/login',
    logout: '/login',
    home: '/',
  },
  strategies: {
    cookie: {
      options: {
        httpOnly: true,
        path: '/',
      },
      user: {
        property: false,
        autoFetch: false,
      },
      endpoints: {
        login: { url: '/api/login', method: 'post' },
        logout: { url: '/api/logout', method: 'post' },
      },
    },
  },
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM