简体   繁体   English

如何使用我自己的后端 api 在 nuxt App 中获取令牌(在登录时)?

[英]how to get token (at login) in nuxt App using my own backend api?

I'm working on a project with an other person.我正在和另一个人一起做一个项目。 I'm doing the backend part and he's doing the frontend part.我在做后端,他在做前端。

Our users can register and login but we can't get the token that I generate in my back api.我们的用户可以注册和登录,但我们无法获得我在后台 api 中生成的令牌。

I tried to found some sources to do that but nothing seems to be appropriate to our situation我试图找到一些来源来做到这一点,但似乎没有什么适合我们的情况

How can we do that?我们怎么做?

Thanks :)谢谢 :)

Front:正面:

    <form @submit.prevent="login" method="post">
      <div class="group_field">
        <input id="email" v-model="email" type="email" name="email" required>
        <label for="email">Email</label>
      </div>
      <div class="group_field">
        <input id="password" v-model="pswd" type="password" name="password" required>
        <label for="password">Password</label>
      </div>
      <button type="submit">
        Login
      </button>
      <small>Pas de compte ? <nuxt-link to="/register">S'inscrire</nuxt-link></small>
    </form>

<script>
export default {

  data () {
    return {
      email: '',
      pswd: '',
      token: '',
      error: null
    }
  },

  methods: {
    async login () {
      try {
        await this.$axios.post('http://localhost:3080/login', {
          email: this.email,
          pswd: this.pswd,
          token: this.token
        })

        this.$router.push('/')
      } catch (e) {
        this.error = e.response.data.message
      }
    }
  }

}
</script>

Back :后退 :

index.js :索引.js:

router.route('/login')
        .post(logger, userController.logUser);

users.js :用户.js:

export const logUser = async (req, res) => {
    if (!req.body.email || !req.body.pswd) return throwBadRequest('Missing parameters', res);
    if (!await utils.validateEmail(req.body.email)) return throwBadRequest('Wrong email format', res);
    if (!await utils.validatePassword(req.body.pswd)) return throwBadRequest('Wrong password formatmust be: at least 3 char long with 1 uppercase 1 lowercase and 1 number', res);
    await UserModel.fetchUser(req.body.email, req.body.pswd, (err, result) => {
        if (err) return throwIntServerError(err, res);
        req.token = { token: result };
        return sendOKWithData({ auth: true, token: result }, res);
    });
}

userModel.js :用户模型.js:

UserSchema.statics.fetchUser = async function (userEmail, userPswd, cb) {
    await this.findOne({ userEmail }, async (err, user) => {
        if (err) return cb(err);
        if(!user) return cb(new Error('user Not Found'));
        const isValid = await bcrypt.compare(userPswd, user.userPswd);
        if(isValid === true) {
            let token = jwt.sign({ userEmail: userEmail, userPswd: userPswd }, secret, {expiresIn: 86400});
            return cb(null, token);
        } else {
            return null;
        }
    });
}

I personally use Nuxt auth to login and register users.我个人使用 Nuxt auth 登录和注册用户。 It is convenient because the main configuration is set in nuxt.config.js.很方便,因为主要的配置是在nuxt.config.js里面设置的。 Using axios we have dedicated functions built into Nuxt Auth.使用 axios,我们在 Nuxt Auth 中内置了专用功能。 Maybe I didn't help centrally with the problem, but maybe I pointed out an additional way out.也许我没有集中帮助解决这个问题,但也许我指出了一个额外的出路。

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

相关问题 Nuxt - 无法获取 api 后端 - Nuxt - Cannot GET api backend 如何使用外部登录从Web API 2获取令牌 - How to get token from Web API 2 using external login 如何在初始化我的 Nuxt 应用程序之前获取和存储 Window 宽度? - How to get and store the Window width before initializing my Nuxt App? 需要将从后端(json)发送的Access令牌存储到localstorage并使用该令牌登录。 怎么样? - Need to Store a Access token send from backend (json) into localstorage and login using that token. How? 如何获取Angular应用访问后端API - How to get angular app to access backend-API 如何在不减慢使用 nuxt 的登录过程的情况下只进行一次大量 api 调用 - How do I make a lot of api calls just once without slowing down the login process using nuxt 如何从 Laravel 后端获取数据并在 Vue/Nuxt 前端显示 - How to get data from Laravel backend and display in Vue/Nuxt frontend 如何使用已有的REST API后端在Angular应用中实现登录系统 - How to implement login system in Angular app with already existing REST API backend 我如何在我的 nuxt vps 上使用 laravel csrf 令牌 - how can i use laravel csrf token on my nuxt vps 如何使用Linkedin Javascript API作为我自己网站的登录? - How can I use the Linkedin Javascript API as a login for my own website?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM