Skip to content

Commit

Permalink
improved auth functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
truly-indian committed Aug 1, 2024
1 parent 8b0afd0 commit cc78e0c
Show file tree
Hide file tree
Showing 10 changed files with 604 additions and 6 deletions.
1 change: 0 additions & 1 deletion backend/auth/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ router.post('/sign_up', async (req, res) => {

router.post('/sign_in', async (req,res) => {
try {
console.log('req: ', req.body)
const body = req.body;
const resp = await SignIn(body);
const token = jwt.sign({email: resp.email}, "secret_key", {expiresIn: '1h'} )
Expand Down
11 changes: 9 additions & 2 deletions backend/auth/service.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
const User = require('../models/User/User');
const { Save, Fetch } = require('./repository')
const bcrypt = require('bcrypt');
const saltRounds = 10;

exports.SignIn = async (request) => {
try {
const { email, password } = request;
const user = await Fetch(User, {email, password});
const user = await Fetch(User, {email});
const storedPassword = user?.password || '';
const passwordMatched = await bcrypt.compare(password, storedPassword);
if (!passwordMatched) throw {error: 'password did not match', status: '401'}
return user;
} catch (error) {
console.log('error while signing user in: ', {...error})
throw error;
}
}
Expand All @@ -16,7 +22,8 @@ exports.SignUp = async (request) => {
const { email, password } = request;
const user = await Fetch(User, { 'email': email });
if (user) throw { error: 'user already exists', statusCode: 409 }
let newUser = new User({ email, password });
const hashedPassword = await bcrypt.hash(password, saltRounds);
let newUser = new User({ email, password: hashedPassword });
await Save(newUser);
} catch (error) {
throw error;
Expand Down
Loading

0 comments on commit cc78e0c

Please sign in to comment.