Package breakdown - initial commit 1.0.0
This commit is contained in:
80
lib/controllers/auth.ts
Normal file
80
lib/controllers/auth.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
import { StatusCodes } from 'http-status-codes';
|
||||
import Koa from 'koa';
|
||||
import Router from 'koa-router';
|
||||
import { StringSchemaDefinition } from 'mongoose';
|
||||
|
||||
import { Auth } from '@mifi/services-common/lib/db';
|
||||
import { create } from '@mifi/services-common/lib/db/dao/create';
|
||||
import { resetPasswordPost } from '@mifi/services-common/lib/db/api/resetPasswordPost';
|
||||
import { resetPasswordGet } from '@mifi/services-common/lib/db/api/resetPasswordGet';
|
||||
import { deleteById } from '@mifi/services-common/lib/db/dao/deleteById';
|
||||
import { deleteStrategy } from '@mifi/services-common/lib/db/api/deleteStrategy';
|
||||
import { AuthDocument } from '@mifi/services-common/lib/db/schema/auth';
|
||||
|
||||
import { ROUTE_PREFIX as prefix, RESET_ROUTE } from '../constants/env';
|
||||
import passport from '../passport';
|
||||
import { ErrorCodes, getErrorBody } from '../constants/errors';
|
||||
import { authenticated } from '../middleware/authenication';
|
||||
|
||||
const routerOpts: Router.IRouterOptions = { prefix };
|
||||
const router: Router = new Router(routerOpts);
|
||||
|
||||
router.get('/info', (ctx) => {
|
||||
ctx.body = {
|
||||
service: process.env.SERVICE_NAME,
|
||||
};
|
||||
});
|
||||
|
||||
router.post('/', async (ctx) => {
|
||||
console.log('POST: /auth [ctx]', ctx);
|
||||
const data = await create(<AuthDocument & { password: string }>ctx.request.body).catch((err) =>
|
||||
console.error('POST: /auth [err]', err),
|
||||
);
|
||||
console.log('POST: /auth [data]', data);
|
||||
ctx.body = { success: !!data, data };
|
||||
});
|
||||
|
||||
router.delete('/strategy/:id', async (ctx) => {
|
||||
ctx.body = { success: await deleteStrategy(ctx.params.id as StringSchemaDefinition) };
|
||||
});
|
||||
|
||||
router.delete('/:id', async (ctx) => {
|
||||
ctx.body = { success: await deleteById(ctx.params.id as StringSchemaDefinition) };
|
||||
});
|
||||
|
||||
router.post('/login', async (ctx, next) => {
|
||||
return passport.authenticate('local', (err, user) => {
|
||||
ctx.body = user;
|
||||
return user ? ctx.login(user) : ctx.throw(StatusCodes.UNAUTHORIZED);
|
||||
})(ctx, next);
|
||||
});
|
||||
|
||||
router.post(process.env.RESET_ROUTE || RESET_ROUTE, async (ctx) => {
|
||||
const { password, token, username } = ctx.request.body as { token?: string; password?: string; username?: string };
|
||||
let response: false | { record: StringSchemaDefinition; token: string } = false;
|
||||
|
||||
if (username) {
|
||||
response = await resetPasswordGet(username);
|
||||
} else if (token && password) {
|
||||
response = await resetPasswordPost(token, password);
|
||||
}
|
||||
|
||||
ctx.body = { success: !!response, ...(response || getErrorBody(ErrorCodes.RESET_REQUEST_DATA)) };
|
||||
|
||||
if (!response) {
|
||||
ctx.throw(StatusCodes.BAD_REQUEST);
|
||||
}
|
||||
});
|
||||
|
||||
router.patch('/:record', authenticated(), (ctx: Koa.Context) => {
|
||||
if (ctx.user !== ctx.param.record) {
|
||||
ctx.throw(StatusCodes.UNAUTHORIZED);
|
||||
}
|
||||
const data = Auth.findOneAndUpdate({ record: ctx.params.record });
|
||||
if (!data) {
|
||||
ctx.throw(StatusCodes.NOT_FOUND);
|
||||
}
|
||||
ctx.body = { success: true, data };
|
||||
});
|
||||
|
||||
export { router as authRouter };
|
||||
Reference in New Issue
Block a user