zoukankan      html  css  js  c++  java
  • koa 项目实战(七)登录接口

    1.登录接口

    /**
     * @route POST api/users/login
     * @desc 登录接口地址
     * @access 接口是公开的
     */
    router.post('/login', async ctx => {
      // 查询
      const findResult = await User.find({ email: ctx.request.body.email });
      const user = findResult[0];
      const password = ctx.request.body.password;
    
      // 判断差没查到
      if (findResult.length == 0) {
        ctx.status = 404;
        ctx.body = { email: '用户不存在!' };
      } else {
        // 查到后 验证密码
        var result = await bcrypt.compareSync(password, user.password);
    
        // 校验通过
        if (result) {
          // 返回token
          ctx.status = 200;
          ctx.body = { success: true };
        } else {
          ctx.status = 400;
          ctx.body = { password: '密码错误!' };
        }
      }
    })

    根目录/routes/api/users.js

    const Router = require('koa-router');
    const router = new Router();
    const bcrypt = require('bcryptjs');
    const gravatar = require('gravatar');
    const tools = require('../../config/tools');
    
    // 引入User
    const User = require('../../models/User');
    
    /**
     * @route GET api/users/test
     * @desc 测试接口地址
     * @access 接口是公开的
     */
    router.get('/test', async ctx => {
      ctx.status = 200;
      ctx.body = { msg: 'users works...' };
    });
    
    /**
     * @route POST api/users/register
     * @desc 注册接口地址
     * @access 接口是公开的
     */
    router.post('/register', async ctx => {
      // console.log(ctx.request.body);
    
      // 通过邮箱判读是否注册过
      const findResult = await User.find({ email: ctx.request.body.email });
      // console.log(findResult);
      if (findResult.length > 0) {
        ctx.status = 500;
        ctx.body = { email: '邮箱已被占用 ' };
      } else {
        const avatar = gravatar.url(ctx.request.body.email, { s: '200', r: 'pg', d: 'mm' });
        const newUser = new User({
          name: ctx.request.body.name,
          email: ctx.request.body.email,
          avatar,
          password: tools.enbcrypt(ctx.request.body.password)
        });
    
        // console.log(newUser);
        // 存储到数据库
        await newUser.save().then(user => {
          ctx.body = user;
        }).catch(err => {
          console.log(err);
        });
    
        // 返回json数据
        ctx.body = newUser;
      }
    });
    
    /**
     * @route POST api/users/login
     * @desc 登录接口地址
     * @access 接口是公开的
     */
    router.post('/login', async ctx => {
      // 查询
      const findResult = await User.find({ email: ctx.request.body.email });
      const user = findResult[0];
      const password = ctx.request.body.password;
    
      // 判断差没查到
      if (findResult.length == 0) {
        ctx.status = 404;
        ctx.body = { email: '用户不存在!' };
      } else {
        // 查到后 验证密码
        var result = await bcrypt.compareSync(password, user.password);
    
        // 校验通过
        if (result) {
          // 返回token
          ctx.status = 200;
          ctx.body = { success: true };
        } else {
          ctx.status = 400;
          ctx.body = { password: '密码错误!' };
        }
      }
    })
    
    module.exports = router.routes();

    2.效果图

  • 相关阅读:
    37个绝对不容错过的HTML5教程和资源
    Google的自动驾驶汽车无事故成功完成30万英里的驾驶路程
    一个基于jQuery Mobile的移动设备实时幻灯javascript类库 taciónJS
    推荐免费黑色UI工具包下载
    分享一些前端开发人员必备的工具,脚本和资源
    使用HTML5画布实现的超棒javascript动画仪表板:gauge.js
    Google(谷歌)将打算在搜索结果中展示Gmail内容
    免费资源下载:30个用户界面工具栏图标设计
    一张超诡异的HTML图片“松鼠” 是图片同时也是web页面
    带给你设计灵感的30个超棒的暗色系网站设计
  • 原文地址:https://www.cnblogs.com/crazycode2/p/11062052.html
Copyright © 2011-2022 走看看