Skip to content

Commit

Permalink
feat: req04 - parcial
Browse files Browse the repository at this point in the history
  • Loading branch information
dopimentel committed Feb 21, 2024
1 parent e18fce6 commit 4ac65a5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ app.get('/', (_request, response) => {
app.use(express.json());

app.post('/login', userController.login);
app.post('/user', userController.create);


app.use(error);
Expand Down
13 changes: 13 additions & 0 deletions src/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ const login = async (req, res, next) => {
res.status(200).json({ token: response.token });
}

const create = async (req, res, next) => {
const { displayName, email, password, image } = req.body;
const response = await userService.create({ displayName, email, password, image });
if (response.status === 409) {
const err = new Error(response.message);
err.status = response.status;
return next(err);
}

res.status(response.status).json({ token: response.token});
}

module.exports = {
login,
create,
};
26 changes: 23 additions & 3 deletions src/services/userService.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ const jwtConfig = {
algorithm: 'HS256',
};

const createToken = (payload) => {
const token = jwt.sign(payload, JWT_SECRET, jwtConfig);
return token;
};

const login = async ({ email, password }) => {
console.log('email', email);
console.log('password', password);
const error = loginValidation({ email, password });
if (error) return { error };

Expand All @@ -22,15 +25,32 @@ const login = async ({ email, password }) => {
};
}

const token = jwt.sign({ email }, JWT_SECRET, jwtConfig);
const token = createToken({ email })

return {
status: 200,
token,
};
};

const create = async ({ displayName, email, password, image }) => {
const userExists = await User.findOne({ where: { email } });
if (userExists) {
return {
status: 409,
message: 'User already registered',
};
}

const user = await User.create({ displayName, email, password, image });
const token = createToken({ email })

return { status: 201, token }
};


module.exports = {
createToken,
login,
create,
};

0 comments on commit 4ac65a5

Please sign in to comment.