How to use the koa-jwt.sign function in koa-jwt

To help you get started, we’ve selected a few koa-jwt examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github soygul / koan / test / server / mocha.conf.js View on Github external
'use strict';

var config = require('../../server/config/config'),
    mongoSeed = require('../../server/config/mongo-seed'),
    app = require('../../app'),
    jwt = require('koa-jwt'),
    baseUrl = 'http://localhost:' + config.app.port + '/api',
    supertest = require('co-supertest'),
    request = supertest(baseUrl);

// create a valid jwt token to be sent with every request
var user = mongoSeed.users[1];
var token = jwt.sign({id: user._id, name: user.name, email: user.email}, config.app.secret);
token = 'Bearer ' + token;

// make request and token objects available
exports.request = request;
exports.token = token;

// initiate KOAN server before each test is run
// also drop and re-seed the test database before each run
console.log('Mocha starting to run server tests on port ' + config.app.port);
beforeEach(function *() {
  yield app.init(true);
});

// close the server after each test is done
afterEach(function (done) {
  app.server.close(done);
github muffin / server / routes / api.js View on Github external
log('Couldn\'t load user', err)
  }

  if (!user) {
    this.status = 400
    this.body = {
      error: 'User doesn\'t exist'
    }
    return
  }

  // Compare password with the one within the DB
  const isMatch = user.tryPassword(body.password)

  if (isMatch) {
    const token = jwt.sign(body, process.env.SESSION_SECRET, {
      expiresIn: 300
    })

    this.body = {
      token
    }

    return
  }

  this.status = 400

  this.body = {
    error: 'Wrong password'
  }
github caihg / vue-demos / vue2.x-koa2.x / server / controllers / user.js View on Github external
if (userInfo != null) { // 如果查无此用户会返回 null
    if (userInfo.password != data.password) {
      if (!bcrypt.compareSync(data.password, userInfo.password)) {
        this.body = { // 返回给前端的数据
          success: false,
          info: '密码错误!'
        }
      }
    } else { // 密码正确
      const userToken = {
        id: userInfo.id,
        name: userInfo.user_name,
        originExp: Date.now() + 60 * 60 * 1000, // 设置过期时间(毫秒)为 1 小时
      }
      const secret = 'vue-koa-demo'; // 指定密钥,这是之后用来判断 token 合法性的标志
      const token = jwt.sign(userToken, secret); // 签发 token
      this.body = {
        success: true,
        token: token
      }
    }
  } else {
    this.body = {
      success: false,
      info: '用户不存在!'
    }
  }
}
github muffin / server / routes / api / token.js View on Github external
if (!user) {
    ctx.status = 400

    ctx.body = {
      error: 'User doesn\'t exist'
    }

    return
  }

  // Compare password with the one within the DB
  const isMatch = user.tryPassword(body.password)

  if (isMatch) {
    const token = jwt.sign(body, process.env.SESSION_SECRET, {
      expiresIn: 300
    })

    ctx.body = { token }
    return
  }

  ctx.status = 400

  ctx.body = {
    error: 'Wrong password'
  }

  await next()
})
github muffin / cli / lib / routes / api / token.js View on Github external
}

  if (!user) {
    this.status = 400
    this.body = {
      error: 'User doesn\'t exist'
    }

    return
  }

  // Compare password with the one within the DB
  const isMatch = user.tryPassword(body.password)

  if (isMatch) {
    const token = jwt.sign(body, process.env.SESSION_SECRET, {
      expiresIn: 300
    })

    this.body = {
      token
    }

    return
  }

  this.status = 400

  this.body = {
    error: 'Wrong password'
  }
github muffin / server / routes / api.js View on Github external
}

  if (!user) {
    this.status = 401
    this.body = {
      error: 'User doesn\'t exist'
    }

    return
  }

  const isMatch = user.tryPassword(decoded.password)

  if (isMatch) {
    this.body = {
      token: jwt.sign(decoded, process.env.SESSION_SECRET, {
        expiresIn: 300
      })
    }

    return
  }

  this.status = 401

  this.body = {
    error: 'Wrong password'
  }

  yield next
})
github wyntau / kman / server / middles / authorize.js View on Github external
module.exports.sign = function(obj, option){
    option = option || {};
    return jwt.sign(obj, secret, option);
};
github muffin / server / routes / api / token.js View on Github external
if (!user) {
    ctx.status = 401

    ctx.body = {
      error: 'User doesn\'t exist'
    }

    return
  }

  const isMatch = user.tryPassword(decoded.password)

  if (isMatch) {
    ctx.body = {
      token: jwt.sign(decoded, process.env.SESSION_SECRET, {
        expiresIn: 300
      })
    }

    return
  }

  ctx.status = 401

  ctx.body = {
    error: 'Wrong password'
  }

  await next()
})
github rankun203 / koa-starter / app / routes / v1 / auth.js View on Github external
phone   : phone,
    password: password
  });

  if (!foundUserList || foundUserList.length === 0) return this.throw(404, 'No user found');

  const user = foundUserList[0].toJSON();

  const jwtUser = {
    id   : user.id,
    phone: user.phone,
    name : user.name,
    role : user.role
  };

  user.token = jwt.sign(jwtUser, config.jwt.secret, config.jwt.expiresInMinutes);
  this.body  = user;
};

koa-jwt

Koa middleware for validating JSON Web Tokens

MIT
Latest version published 1 year ago

Package Health Score

63 / 100
Full package analysis

Popular koa-jwt functions