Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/return user data on activation #308

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 48 additions & 38 deletions api/controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,27 +234,32 @@ async function createOrUpdateUser(accessToken, profile, type = 'facebook') {

function activateUser(req, res) {
const url = req.swagger.params.url.value;
nev.confirmTempUser(url, function (err, user) {
const confirmTempUserCallback = async (err, user) => {
if (user) {
nev.sendConfirmationEmail(user.email, function (err, info) {
nev.sendConfirmationEmail(user.email, async function(err, info) {
if (err) {
return res.status(404).json({
message: 'ERROR: sending confirmation email FAILED ' + info
});
console.error(
'message: ERROR: sending confirmation email FAILED' + info
);
}
return res.status(200).json({
success: 1,
userid: user._id,
message: 'CONFIRMED!'
});
});
const response = await populateUserForLogin(req, user, user.email);
return res.status(200).json({
...response,
success: 1,
userid: user._id,
message: 'CONFIRMED!'
});
} else {
return res.status(404).json({
message: 'ERROR: confirming your temporary user FAILED, please try to login again',
error: 'ERROR: confirming your temporary user FAILED, please try to login again'
message:
'ERROR: confirming your temporary user FAILED, please try to login again',
error:
'ERROR: confirming your temporary user FAILED, please try to login again'
});
}
});
};
nev.confirmTempUser(url, confirmTempUserCallback);
}

async function listUser(req, res) {
Expand Down Expand Up @@ -384,6 +389,35 @@ function updateUser(req, res) {
});
}

const populateUserForLogin = async (req, user, email) => {
const userId = user._id;
req.session.userId = userId;

const tokenString = auth.issueToken({
email,
id: userId
});

if (!user.location || !user.location.country)
try {
await updateUserLocation(req.ip, user);
} catch (error) {
console.error(error.message);
}

const settings = await getSettings(user);
const subscriber = await getSubscriber(user);

const response = {
...user.toJSON(),
settings,
subscriber,
birthdate: moment(user.birthdate).format('YYYY-MM-DD'),
authToken: tokenString
};
return response;
};

function loginUser(req, res) {
const { email, password } = req.body;

Expand All @@ -393,31 +427,7 @@ function loginUser(req, res) {
message: 'Wrong email or password.'
});
} else {
const userId = user._id;
req.session.userId = userId;

const tokenString = auth.issueToken({
email,
id: userId
});

if (!user.location || !user.location.country)
try {
await updateUserLocation(req.ip, user);
} catch (error) {
console.error(error.message);
}

const settings = await getSettings(user);
const subscriber = await getSubscriber(user);

const response = {
...user.toJSON(),
settings,
subscriber,
birthdate: moment(user.birthdate).format('YYYY-MM-DD'),
authToken: tokenString
};
const response = await populateUserForLogin(req, user, email);
return res.status(200).json(response);
}
});
Expand Down