This commit is contained in:
@@ -4,6 +4,8 @@ import { StatusCodes } from 'http-status-codes';
|
|||||||
import { API_PATH } from '../constants/defaults';
|
import { API_PATH } from '../constants/defaults';
|
||||||
|
|
||||||
import Auth from '../model/auth';
|
import Auth from '../model/auth';
|
||||||
|
import passport from '../passport';
|
||||||
|
import { sign } from '../utils/jwt';
|
||||||
|
|
||||||
const routerOpts: Router.IRouterOptions = {
|
const routerOpts: Router.IRouterOptions = {
|
||||||
prefix: process.env.API_PATH || API_PATH,
|
prefix: process.env.API_PATH || API_PATH,
|
||||||
@@ -17,30 +19,18 @@ router.post('/', async (ctx: Koa.Context) => {
|
|||||||
ctx.body = { success: true, data };
|
ctx.body = { success: true, data };
|
||||||
});
|
});
|
||||||
|
|
||||||
|
router.post('/login', async (ctx: Koa.Context, next) => {
|
||||||
router.post('/login', async (ctx: Koa.Context) => {
|
return passport.authenticate('local', (err, user) => {
|
||||||
const { body: { username = null, password = null } = {} } = ctx;
|
if (user === false) {
|
||||||
|
ctx.body = { token: sign() };
|
||||||
if (!username || !password) {
|
ctx.throw(StatusCodes.UNAUTHORIZED);
|
||||||
let errors = {};
|
|
||||||
|
|
||||||
if (!username) {
|
|
||||||
errors.username = 'is required';
|
|
||||||
}
|
}
|
||||||
|
ctx.body = { token: sign(user) };
|
||||||
if (!password) {
|
return ctx.login(user);
|
||||||
errors.password = 'is required';
|
})(ctx, next);
|
||||||
}
|
await next();
|
||||||
|
|
||||||
ctx.status = StatusCodes.UNPROCESSABLE_ENTITY;
|
|
||||||
ctx.throw(422, { errors });
|
|
||||||
}
|
|
||||||
|
|
||||||
const callback = handlePassportResponse(req, res, next);
|
|
||||||
return passport.authenticate('local', { session: false }, callback)(req, res, next);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
router.patch('/:customer_id', async (ctx: Koa.Context) => {
|
router.patch('/:customer_id', async (ctx: Koa.Context) => {
|
||||||
const data = await Auth.findByIdAndUpdate(ctx.params.customer_id);
|
const data = await Auth.findByIdAndUpdate(ctx.params.customer_id);
|
||||||
if (!data) {
|
if (!data) {
|
||||||
|
|||||||
Reference in New Issue
Block a user