restored basic functionality of user auth workflow
This commit is contained in:
@@ -16,8 +16,8 @@ module.exports = async (app) => {
|
||||
|
||||
passport.deserializeUser((user, done) => {
|
||||
process.nextTick(async () => {
|
||||
const user = await LoginService(user.email, user.password);
|
||||
return (user) ? done(null, user) : done(null, false);
|
||||
const foundUser = await AuthInstance.login(user);
|
||||
return foundUser ? done(null, foundUser) : done(null, false);
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
const authRouter = require('./auth');
|
||||
const userRouter = require('./user');
|
||||
const productRouter = require('./product');
|
||||
const orderRouter = require('./order');
|
||||
const orderRouter = require('./orders');
|
||||
const cartRouter = require('./cart');
|
||||
|
||||
module.exports = async (app, passport) => {
|
||||
module.exports = (app, passport) => {
|
||||
authRouter(app, passport);
|
||||
userRouter(app);
|
||||
productRouter(app);
|
||||
|
||||
@@ -1,11 +1,23 @@
|
||||
const AuthService = require('../services/AuthService');
|
||||
const AuthServiceInstance = new AuthService();
|
||||
const router = require('express').Router();
|
||||
|
||||
module.exports = (app, passport) => {
|
||||
app.use('/api/auth', router);
|
||||
|
||||
router.get('/', async (req, res, next) => {
|
||||
try {
|
||||
res.send('auth get response');
|
||||
} catch(e) {
|
||||
next(e);
|
||||
}
|
||||
})
|
||||
|
||||
router.post('/register', async (req, res, next) => {
|
||||
try {
|
||||
|
||||
const data = req.body;
|
||||
const response = await AuthServiceInstance.register(data);
|
||||
res.status(200).send(response);
|
||||
} catch(e) {
|
||||
next(e);
|
||||
}
|
||||
@@ -13,7 +25,9 @@ module.exports = (app, passport) => {
|
||||
|
||||
router.post('/login', passport.authenticate('local'), async (req, res, next) => {
|
||||
try {
|
||||
|
||||
const data = req.body;
|
||||
const response = await AuthServiceInstance.login(data);
|
||||
res.status(200).send(response);
|
||||
} catch(e) {
|
||||
next(e);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
const router = require('express').Router();
|
||||
|
||||
module.exports = (app) => {
|
||||
app.use('/api/order', router);
|
||||
app.use('/api/orders', router);
|
||||
|
||||
router.get('/', async (req, res, next) => {
|
||||
try {
|
||||
@@ -5,15 +5,26 @@ const UserInstance = new UserModel();
|
||||
|
||||
module.exports = class AuthService {
|
||||
async register(data) {
|
||||
const { email } = data;
|
||||
const { email, password } = data;
|
||||
|
||||
try {
|
||||
const user = await UserInstance.findOneById(email);
|
||||
if (user) {
|
||||
throw createError(409, 'Email already in use');
|
||||
}
|
||||
const user = await UserInstance.findOneByEmail(email);
|
||||
if (user) throw createError(409, 'Email already in use');
|
||||
|
||||
return await UserInstance.create(data);
|
||||
const response = bcrypt.genSalt(10, (err, salt) => {
|
||||
if (err) throw err;
|
||||
bcrypt.hash(password, salt, (err, hash) => {
|
||||
if (err) throw err;
|
||||
const newData = {
|
||||
email: email,
|
||||
password: hash
|
||||
}
|
||||
|
||||
return UserInstance.create(newData);
|
||||
})
|
||||
})
|
||||
|
||||
return response;
|
||||
} catch(e) {
|
||||
throw new Error(e);
|
||||
}
|
||||
@@ -26,9 +37,15 @@ module.exports = class AuthService {
|
||||
try {
|
||||
const user = await UserInstance.findOneByEmail(email);
|
||||
if (!user) throw createError(401, 'Incorrect email or password');
|
||||
// const match = bcrypt.compare(user.password, password, (result, err) => {
|
||||
// if (err) throw err;
|
||||
// return result;
|
||||
// })
|
||||
|
||||
const match = await bcrypt.compare(user.password, password);
|
||||
if (!match) throw createError(401, 'Incorrect email or password');
|
||||
// console.log(match);
|
||||
// if (!match) throw createError(401, 'Incorrect email or password');
|
||||
|
||||
console.log(user.password);
|
||||
|
||||
return user;
|
||||
} catch(e) {
|
||||
|
||||
Reference in New Issue
Block a user