From 39ba4965e06b5bead67995587bda3f024746a141 Mon Sep 17 00:00:00 2001 From: mifi Date: Tue, 2 May 2023 22:10:47 -0400 Subject: [PATCH] Tweaks --- lib/controllers/auth.ts | 251 ++++++---------------------------------- package.json | 2 +- 2 files changed, 38 insertions(+), 215 deletions(-) diff --git a/lib/controllers/auth.ts b/lib/controllers/auth.ts index 1f83fb4..4eb2038 100644 --- a/lib/controllers/auth.ts +++ b/lib/controllers/auth.ts @@ -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) => { -// // if (err) { -// // return next(err); -// // } +const routerOpts: Router.IRouterOptions = { + prefix: process.env.API_PATH || API_PATH, +}; -// // const isVerifiedUser = user && -// // user.isRegistrationVerified(); +const router: Router = new Router(routerOpts); -// // 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.' -// // }); -// // } +router.post('/', async (ctx: Koa.Context) => { + const data = await Auth.create(ctx.body); + data.save(); + ctx.body = { success: true, data }; +}); -// // return res.send(400, info); -// // }; -// // module.exports = function (server, auth) { -// // const { passport } = auth; +router.post('/login', async (ctx: Koa.Context) => { + const { body: { username = null, password = null } = {} } = ctx; -// // /* Local Auth */ -// // server.post('/auth', (req, res, next) => { -// // const { body: { username = null, password = null } = {} } = req; + if (!username || !password) { + let errors = {}; -// // if (!username || !password) { -// // let errors = {}; + if (!username) { + errors.username = 'is required'; + } -// // if (!username) { -// // errors.username = 'is required'; -// // } + if (!password) { + errors.password = 'is required'; + } -// // if (!password) { -// // errors.password = 'is required'; -// // } + ctx.status = StatusCodes.UNPROCESSABLE_ENTITY; + 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); -// // }); -// // /** -// // * SERVICES -// // */ - -// // /* Google */ -// // server.get( -// // '/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 }; -// }); +router.patch('/:customer_id', async (ctx: Koa.Context) => { + const data = await Auth.findByIdAndUpdate(ctx.params.customer_id); + if (!data) { + ctx.throw(StatusCodes.NOT_FOUND); + } + ctx.body = { success: true, data }; +}); \ No newline at end of file diff --git a/package.json b/package.json index 30e52a8..c19d7e7 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "prettier:fix": "prettier --write 'lib/**/*.ts'", "serve": "ts-node src/server.ts", "start": "nodemon", - "test": "jest" + "test": "jest --passWithNoTests" }, "devDependencies": { "@babel/core": "^7.21.8",