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.效果图

koa 项目实战(七)登录接口的更多相关文章

  1. koa 项目实战(十一)验证登录和注册的 input

    1.验证注册参数 根目录/validation/register.js const Validator = require('validator'); const isEmpty = require( ...

  2. koa 项目实战(九)passport验证token

    1.安装模块 npm install koa-passport -D npm install passport-jwt -D 2.解析token 根目录/config/passport.js cons ...

  3. koa 项目实战(八)生成token

    1.安装模块 npm install jsonwebtoken --save 2.引用 const jwt = require('jsonwebtoken'); ... // 返回token cons ...

  4. koa 项目实战(四)注册接口和调试工具(postman)

    1.安装模块 npm install koa-bodyparser --save npm install bcryptjs --save 2.引入模块 根目录/app.js const bodyPar ...

  5. koa 项目实战(三)创建测试接口和用户模型

    1.创建测试接口,并引入用户模型 根目录/routes/api/users.js const Router = require('koa-router'); const router = new Ro ...

  6. koa 项目实战(六)注册接口加密

    1.创建工具类 根目录/config/tools.js const bcrypt = require('bcryptjs'); const tools = { enbcrypt(password) { ...

  7. 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_4-3.登录检验JWT实战之封装通用方法

    笔记 3.登录检验JWT实战之封装通用方法     讲解:引入相关依赖并开发JWT工具类 1.加入相关依赖 <!-- JWT相关 -->             <dependenc ...

  8. koa 项目实战(五)全球公用头像的使用

    1.安装模块 npm install gravatar --save 2.使用 根目录/routes/api/users.js const gravatar = require('gravatar') ...

  9. koa 项目实战(二)连接 mongodb 数据库

    1.配置文件 根目录/config/keys.js module.exports = { mongoURI: 'mongodb://127.0.0.1:27017/mongodb' } 2.启动文件 ...

随机推荐

  1. DB2 alter 新增/删除/修改列

    SQL语句 增加列.修改列.删除列 1 添加字段 语法 : alter table 表名称 add 字段名称 类型 demo: alter table tableName add columnName ...

  2. Ubuntu系统---以virtualenv方式安装Tensorflow-CPU

    Ubuntu系统---以virtualenv方式安装Tensorflow-CPU 一.安装环境 环境:Ubuntu18.04+CPU+python2.7 本文介绍:如何在ubuntu上以virtual ...

  3. 使用Tarjan进行缩点无向图

    int From[maxn],Laxt[maxn],To[maxn<<2],Next[maxn<<2],cnt; int low[maxn],dfn[maxn],times,q ...

  4. Airtest 支持的手机,系统等环境

    据个人经验,Airtest 支持的以下设备会跑的比较666 Android 平台 华为荣耀9青春版 版本:8.0.0 型号:LLD-AL10 评价:自动化运行最6 华为 荣耀10青春版 版本:9.0. ...

  5. JAI丢包掉帧处理

    问题 时间戳停止变化/图像停止更新 描述 本小白刚刚接触JAI,有很多不懂的地方.这次遇到问题是请教了YZ大哥(不知道年龄,暂时这么称呼),很感谢YZ大哥的耐心指导.因为我不仅不知道怎么调,连在哪里调 ...

  6. PLSQL打开文件中文出现乱码

    假定数据库使用的是:American_America.AL32UTF8字符集. 查询方式:SELECT * FROM v$nls_parameters ; 查看NLS_CHARACTERSET 的值是 ...

  7. BZOJ 3435 / Luogu 3920 [WC2014]紫荆花之恋 (替罪羊树 动态点分治 套 Treap)

    题意 略 分析 引用PoPoQQQ的话 吾辈有生之年终于把这道题切了...QAQ (蒟蒻狂笑) Orz PoPoQQQ,我又抄PoPoQQQ的题解了 - 突然发现有旋Treap没那么难写 学习了一波C ...

  8. [Svelte 3] Use an onMount lifecycle method to fetch and render data in Svelte 3

    Every Svelte component has a lifecycle that starts when it is created, and ends when it is destroyed ...

  9. Python2.x与3​​.x版本区别Ⅱ

    除法运算 Python中的除法较其它语言显得非常高端,有套很复杂的规则.Python中的除法有两个运算符,/和// 首先来说/除法: 在python 2.x中/除法就跟我们熟https://www.x ...

  10. Codeforces Round #402 (Div. 2) D题 【字符串二分答案+暴力】

    D. String Game Little Nastya has a hobby, she likes to remove some letters from word, to obtain anot ...