This commit is contained in:
@@ -1,227 +1,50 @@
|
|||||||
// // const errors = require('restify-errors');
|
import Koa from 'koa';
|
||||||
|
import Router from 'koa-router';
|
||||||
|
import { StatusCodes } from 'http-status-codes';
|
||||||
|
import { API_PATH } from '../constants/defaults';
|
||||||
|
|
||||||
// // const config = require('../config');
|
import Auth from '../model/auth';
|
||||||
|
|
||||||
// // const handlePassportResponse = (req, res, next) => (err, user, info) => {
|
const routerOpts: Router.IRouterOptions = {
|
||||||
// // if (err) {
|
prefix: process.env.API_PATH || API_PATH,
|
||||||
// // return next(err);
|
};
|
||||||
// // }
|
|
||||||
|
|
||||||
// // const isVerifiedUser = user &&
|
const router: Router = new Router(routerOpts);
|
||||||
// // user.isRegistrationVerified();
|
|
||||||
|
|
||||||
// // if (user && isVerifiedUser) {
|
router.post('/', async (ctx: Koa.Context) => {
|
||||||
// // return res.send({ ...user.toAuthJSON() });
|
const data = await Auth.create(ctx.body);
|
||||||
// // } else if (user && !isVerifiedUser){
|
data.save();
|
||||||
// // return res.send({
|
ctx.body = { success: true, data };
|
||||||
// // registrationSuccess: true,
|
});
|
||||||
// // nextSteps: 'Check your email for our confirmation email, you will not be able to login without confirming.'
|
|
||||||
// // });
|
|
||||||
// // }
|
|
||||||
|
|
||||||
// // return res.send(400, info);
|
|
||||||
// // };
|
|
||||||
|
|
||||||
// // module.exports = function (server, auth) {
|
router.post('/login', async (ctx: Koa.Context) => {
|
||||||
// // const { passport } = auth;
|
const { body: { username = null, password = null } = {} } = ctx;
|
||||||
|
|
||||||
// // /* Local Auth */
|
if (!username || !password) {
|
||||||
// // server.post('/auth', (req, res, next) => {
|
let errors = {};
|
||||||
// // const { body: { username = null, password = null } = {} } = req;
|
|
||||||
|
|
||||||
// // if (!username || !password) {
|
if (!username) {
|
||||||
// // let errors = {};
|
errors.username = 'is required';
|
||||||
|
}
|
||||||
|
|
||||||
// // if (!username) {
|
if (!password) {
|
||||||
// // errors.username = 'is required';
|
errors.password = 'is required';
|
||||||
// // }
|
}
|
||||||
|
|
||||||
// // if (!password) {
|
ctx.status = StatusCodes.UNPROCESSABLE_ENTITY;
|
||||||
// // errors.password = 'is required';
|
ctx.throw(422, { errors });
|
||||||
// // }
|
}
|
||||||
|
|
||||||
// // return res.send(422, { errors });
|
const callback = handlePassportResponse(req, res, next);
|
||||||
// // }
|
return passport.authenticate('local', { session: false }, callback)(req, res, next);
|
||||||
|
});
|
||||||
|
|
||||||
// // const callback = handlePassportResponse(req, res, next);
|
|
||||||
// // return passport.authenticate('local', { session: false }.then(callback)(req, res, next);
|
|
||||||
// // });
|
|
||||||
|
|
||||||
// // /**
|
router.patch('/:customer_id', async (ctx: Koa.Context) => {
|
||||||
// // * SERVICES
|
const data = await Auth.findByIdAndUpdate(ctx.params.customer_id);
|
||||||
// // */
|
if (!data) {
|
||||||
|
ctx.throw(StatusCodes.NOT_FOUND);
|
||||||
// // /* Google */
|
}
|
||||||
// // server.get(
|
ctx.body = { success: true, data };
|
||||||
// // '/auth/google',
|
});
|
||||||
// // passport.authenticate('google', { scope: 'profile email', session: false }),
|
|
||||||
// // );
|
|
||||||
|
|
||||||
// // server.get(
|
|
||||||
// // '/auth/google/callback',
|
|
||||||
// // (req, res, next) => {
|
|
||||||
// // const callback = handlePassportResponse(req, res, next);
|
|
||||||
// // return passport.authenticate(
|
|
||||||
// // 'google',
|
|
||||||
// // { failureRedirect: '/login' },
|
|
||||||
// // callback,
|
|
||||||
// // )(req, res, next);
|
|
||||||
// // },
|
|
||||||
// // );
|
|
||||||
|
|
||||||
// // /* Facebook */
|
|
||||||
// // server.get(
|
|
||||||
// // '/auth/facebook/login',
|
|
||||||
// // passport.authenticate('facebook', {
|
|
||||||
// // scope: ['email', 'public_profile'],
|
|
||||||
// // session: false,
|
|
||||||
// // }),
|
|
||||||
// // );
|
|
||||||
|
|
||||||
// // server.get(
|
|
||||||
// // '/auth/facebook/loggedin',
|
|
||||||
// // (req, res, next) => {
|
|
||||||
// // const callback = handlePassportResponse(req, res, next);
|
|
||||||
// // return passport.authenticate(
|
|
||||||
// // 'facebook',
|
|
||||||
// // { failureRedirect: '/login' },
|
|
||||||
// // callback,
|
|
||||||
// // )(req, res, next);
|
|
||||||
// // }
|
|
||||||
// // );
|
|
||||||
|
|
||||||
// // server.get(
|
|
||||||
// // '/auth/facebook/link',
|
|
||||||
// // auth.secure,
|
|
||||||
// // (req, res, next) => {
|
|
||||||
// // req.user.record.setLinkCheckBit((err, linkCheckBit) => {
|
|
||||||
// // passport.authenticate('facebookLink', {
|
|
||||||
// // scope: ['email', 'public_profile'],
|
|
||||||
// // session: false,
|
|
||||||
// // state: linkCheckbit,
|
|
||||||
// // })(req, res, next);
|
|
||||||
// // });
|
|
||||||
// // },
|
|
||||||
// // );
|
|
||||||
// //
|
|
||||||
// // server.get(
|
|
||||||
// // '/auth/facebook/linked',
|
|
||||||
// // (req, res, next) => {
|
|
||||||
// // const linkCheckBit = req.query.state;
|
|
||||||
// //
|
|
||||||
// // return passport.authenticate(
|
|
||||||
// // 'facebook',
|
|
||||||
// // { failureRedirect: '/profile' },
|
|
||||||
// // (err, profile) => {
|
|
||||||
// // if (err) {
|
|
||||||
// // return next(err);
|
|
||||||
// // }
|
|
||||||
// //
|
|
||||||
// // User.linkFacebookProfile(linkCheckBit, profile, (err, user) => {
|
|
||||||
// // if (err) {
|
|
||||||
// // return next(err);
|
|
||||||
// // }
|
|
||||||
// //
|
|
||||||
// // if (!user) {
|
|
||||||
// // return next(err, false, 'Linking the account to Facebook was unsuccessful, please try again.');
|
|
||||||
// // }
|
|
||||||
// //
|
|
||||||
// // res.send({
|
|
||||||
// // success: true,
|
|
||||||
// // info: 'Facerbook account successfully linked',
|
|
||||||
// // });
|
|
||||||
// // });
|
|
||||||
// // },
|
|
||||||
// // )(req, res, next);
|
|
||||||
// // }
|
|
||||||
// // );
|
|
||||||
// };
|
|
||||||
|
|
||||||
// import Koa from 'koa';
|
|
||||||
// import Router from 'koa-router';
|
|
||||||
// import { StatusCodes } from 'http-status-codes';
|
|
||||||
|
|
||||||
// import Users from 'grow-db/lib/models/users';
|
|
||||||
|
|
||||||
// const handlePassportResponse = (ctx: Koa.Context) => (err, user, info) => {
|
|
||||||
// if (err) {
|
|
||||||
// return next(err);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// const isVerifiedUser = user &&
|
|
||||||
// user.isRegistrationVerified();
|
|
||||||
|
|
||||||
// if (user && isVerifiedUser) {
|
|
||||||
// return res.send({ ...user.toAuthJSON() });
|
|
||||||
// } else if (user && !isVerifiedUser){
|
|
||||||
// return res.send({
|
|
||||||
// registrationSuccess: true,
|
|
||||||
// nextSteps: 'Check your email for our confirmation email, you will not be able to login without confirming.'
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return res.send(400, info);
|
|
||||||
// };
|
|
||||||
|
|
||||||
// const routerOpts: Router.IRouterOptions = {
|
|
||||||
// prefix: '/auth',
|
|
||||||
// };
|
|
||||||
|
|
||||||
// const router: Router = new Router(routerOpts);
|
|
||||||
|
|
||||||
// router.get('/', async (ctx: Koa.Context) => {
|
|
||||||
// const data = await Customers.find({}).exec();
|
|
||||||
// ctx.body = { data };
|
|
||||||
// });
|
|
||||||
|
|
||||||
// router.get('/:customer_id', async (ctx: Koa.Context) => {
|
|
||||||
// const data = await Customers.findById(ctx.params.customer_id).populate('person').exec();
|
|
||||||
// if (!data) {
|
|
||||||
// ctx.throw(StatusCodes.NOT_FOUND);
|
|
||||||
// }
|
|
||||||
// ctx.body = { data };
|
|
||||||
// });
|
|
||||||
|
|
||||||
// router.delete('/:customer_id', async (ctx: Koa.Context) => {
|
|
||||||
// const data = await Customers.findByIdAndDelete(ctx.params.customer_id).exec();
|
|
||||||
// if (!data) {
|
|
||||||
// ctx.throw(StatusCodes.NOT_FOUND);
|
|
||||||
// }
|
|
||||||
// ctx.body = { success: true, data };
|
|
||||||
// });
|
|
||||||
|
|
||||||
// router.post('/', async (ctx: Koa.Context) => {
|
|
||||||
// const data = await Customers.create(ctx.body);
|
|
||||||
// data.save();
|
|
||||||
// ctx.body = { success: true, data };
|
|
||||||
// });
|
|
||||||
|
|
||||||
// router.post('/', async (ctx: Koa.Context) => {
|
|
||||||
// const { body: { username = null, password = null } = {} } = ctx;
|
|
||||||
|
|
||||||
// if (!username || !password) {
|
|
||||||
// let errors = {};
|
|
||||||
|
|
||||||
// if (!username) {
|
|
||||||
// errors.username = 'is required';
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if (!password) {
|
|
||||||
// errors.password = 'is required';
|
|
||||||
// }
|
|
||||||
|
|
||||||
// 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) => {
|
|
||||||
// const data = await Customers.findByIdAndUpdate(ctx.params.customer_id);
|
|
||||||
// if (!data) {
|
|
||||||
// ctx.throw(StatusCodes.NOT_FOUND);
|
|
||||||
// }
|
|
||||||
// ctx.body = { success: true, data };
|
|
||||||
// });
|
|
||||||
@@ -12,7 +12,7 @@
|
|||||||
"prettier:fix": "prettier --write 'lib/**/*.ts'",
|
"prettier:fix": "prettier --write 'lib/**/*.ts'",
|
||||||
"serve": "ts-node src/server.ts",
|
"serve": "ts-node src/server.ts",
|
||||||
"start": "nodemon",
|
"start": "nodemon",
|
||||||
"test": "jest"
|
"test": "jest --passWithNoTests"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@babel/core": "^7.21.8",
|
"@babel/core": "^7.21.8",
|
||||||
|
|||||||
Reference in New Issue
Block a user