Skip to content

Commit

Permalink
fix oauth endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
tharlestsa committed Sep 29, 2024
1 parent eb34a16 commit 47afcf3
Showing 1 changed file with 28 additions and 27 deletions.
55 changes: 28 additions & 27 deletions src/server/controllers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,30 +83,34 @@ module.exports = function (app) {
}
}

Controller.oauth = async function (request, response) {
const { name, email, picture, locale} = request.body
Controller.oauth = async function (request, response) {
const { name, email, picture, locale } = request.body;

let { lang } = request.headers;

if(!lang){
lang = locale.includes('pt')? 'pt' : 'en';
// Check if locale exists and if not, set lang to default
if (!lang) {
if (locale && typeof locale === 'string' && locale.includes('pt')) {
lang = 'pt';
} else {
lang = 'en';
}
}

const texts = _language.getLang(lang);
try {

const user = await prisma.user.findUnique({
where: {
email: email,
},
})
});

if(user) {
if(!user.picture && picture != '' ) {
if (user) {
if (!user.picture && picture != '') {
await prisma.user.update({
where: { id: parseInt(user.id) },
data: { picture: picture },
})
});
}
const payload = {
id: user.id,
Expand All @@ -118,43 +122,40 @@ module.exports = function (app) {
language: user.language,
};
const token = jwt.sign(payload, env.SECRET, {
expiresIn: '24h'
expiresIn: '24h',
});

return response.json(token);

} else {
const user = await prisma.user.create({
const newUser = await prisma.user.create({
data: {
name: name,
email: email,
picture: picture,
terms:false,
terms: false,
},
});

const payload = {
id: user.id,
name: user.name,
email: user.email,
picture: user.picture,
role: user.typeUser,
theme: user.theme,
language: user.language,
id: newUser.id,
name: newUser.name,
email: newUser.email,
picture: newUser.picture,
role: newUser.typeUser,
theme: newUser.theme,
language: newUser.language,
};
const token = jwt.sign(payload, env.SECRET, {
expiresIn: '24h'
expiresIn: '24h',
});

return response.json(token);
}

response.status(500).json({message: texts.login_msg_notfound});
}catch (e) {
console.error(e)
response.status(500).json({message: texts.login_msg_erro + e + '.'});
} catch (e) {
console.error(e);
response.status(500).json({ message: texts.login_msg_erro + e + '.' });
}
}
};

Controller.logout = async function (request, response) {
response.json({ auth: false, token: null });
Expand Down

0 comments on commit 47afcf3

Please sign in to comment.