Skip to content

Commit

Permalink
hotfix: workaround login issue
Browse files Browse the repository at this point in the history
  • Loading branch information
brMonteiro-G committed Nov 15, 2023
1 parent 8bd78e5 commit a1218f0
Showing 1 changed file with 97 additions and 63 deletions.
160 changes: 97 additions & 63 deletions app/setup/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,119 +6,153 @@ const App = require('@/app')

module.exports = async (app) => {
app.server
.use(session({ secret: app.config.GRANT_SECRET, saveUninitialized: true, resave: true }))
.use(
session({
secret: app.config.GRANT_SECRET,
saveUninitialized: true,
resave: true,
})
)

.use(grant(app.config.GRANT_CONFIG))
.get('/oauth/facebook', app.helpers.routes.func(facebook))
.get('/oauth/google', app.helpers.routes.func(google))
}

async function facebook (context) {
const { inApp = '', userId = '', env = '' } = _.get(context.session, 'grant.dynamic', {})
async function facebook(context) {
// const { inApp = '', userId = '', env = '' } = _.get(context.session, 'grant.dynamic', {})

const accessToken = context.query.access_token
const url = `https://graph.facebook.com/me?fields=id,name,email,picture.width(640)&metadata=1&access_token=${accessToken}`
const resp = await Axios.get(url)
// const accessToken = context.query.access_token
// const url = `https://graph.facebook.com/me?fields=id,name,email,picture.width(640)&metadata=1&access_token=${accessToken}`
// const resp = await Axios.get(url)

const faceUser = resp.data

if(!faceUser.id) {
throw new Error('Missing faceUser.id')
}
// const faceUser = resp.data

const findConditions = [
{ 'oauth.facebook': faceUser.id }
]
// if(!faceUser.id) {
// throw new Error('Missing faceUser.id')
// }

if (userId) {
findConditions.push({ _id: userId.split('?')[0] })
}
// const findConditions = [
// { 'oauth.facebook': faceUser.id }
// ]

//na tela de login
//vou clicar em login do face
//vou abrir um popup pedindo o email do facebook cadastrado e ra
//vou pegar esse dado
//vou fazer uma query and com o email e ra
//vou fazer uma busca no banco e verificar se ele existe
//vou autenticar pelo google
//ou seja, será o mesmo que associar uma conta google ao usuário -> fluxo da pagina de configurações

const findConditions = [{ 'oauth.emailFacebook': context.query.email }]

// if (userId) {
// findConditions.push({ _id: userId.split('?')[0] })
// }

// check if user exists in database
// let user = await App.models.users.findOne({
// $or: findConditions
// })

let user = await App.models.users.findOne({
$or: findConditions
$or: findConditions,
})

if(user) {
if (userId) user.set('active', true)
user.set('oauth.facebook', faceUser.id)

if (faceUser.email) {
user.set('oauth.emailFacebook', faceUser.email)
}

if (user) {
return google(context,user)
} else {
user = new App.models.users({
oauth: {
email: faceUser.email,
facebook: faceUser.id,
picture: faceUser.picture.data.url
}
})
throw new Error('Cannot login with facebook, please connect with google')
}

await user.save()

const WEB_URL = env == 'development' ? 'http://localhost:7500' : App.config.WEB_URL

return {
_redirect: inApp.split('?')[0] == 'true'
? `ufabcnext://login?token=${await user.generateJWT()}&`
:`${WEB_URL}/login?token=${user.generateJWT()}`
}
// if(user) {
// if (userId) user.set('active', true)
// user.set('oauth.facebook', faceUser.id)

// if (faceUser.email) {
// user.set('oauth.emailFacebook', faceUser.email)
// }

// } else {
// user = new App.models.users({
// oauth: {
// email: faceUser.email,
// facebook: faceUser.id,
// picture: faceUser.picture.data.url
// }
// })
// }

// await user.save();

// const WEB_URL =
// env == 'development' ? 'http://localhost:7500' : App.config.WEB_URL;

// return {
// _redirect:
// inApp.split('?')[0] == 'true'
// ? `ufabcnext://login?token=${await user.generateJWT()}&`
// : `${WEB_URL}/login?token=${user.generateJWT()}`,
// };
}

async function google(context) {
const { inApp = '', userId = '', env = '' } = _.get(context.session, 'grant.dynamic', {})
async function google(context, user) {
const {
inApp = '',
userId = '',
env = '',
} = _.get(context.session, 'grant.dynamic', {})

const accessToken = context.query.access_token
const url = 'https://www.googleapis.com/plus/v1/people/me'
const resp = await Axios.get(url, { headers: {
Authorization: `Bearer ${accessToken}`
}})
const resp = await Axios.get(url, {
headers: {
Authorization: `Bearer ${accessToken}`,
},
})

const googleUser = resp.data

if(!googleUser.id) {
if (!googleUser.id) {
throw new Error('Missing googleUser.id')
}

const findConditions = [
{ 'oauth.google': googleUser.id },
]
const findConditions = [{ 'oauth.google': googleUser.id }]

if (userId) {
findConditions.push({ _id: userId.split('?')[0] })
}

let user = await App.models.users.findOne({
$or: findConditions
})
// let user = await App.models.users.findOne({
// $or: findConditions,
// });

if(user) {
if (user) {
if (userId) user.set('active', true)
user.set('oauth.google', googleUser.id)

if (googleUser.emails[0].value) {
user.set('oauth.emailGoogle', googleUser.emails[0].value)
}

} else {
user = new App.models.users({
oauth: {
email: googleUser.emails[0].value,
google: googleUser.id
}
google: googleUser.id,
},
})
}

await user.save()

const WEB_URL = env == 'development' ? 'http://localhost:7500' : App.config.WEB_URL

const WEB_URL =
env == 'development' ? 'http://localhost:7500' : App.config.WEB_URL

return {
_redirect: inApp.split('?')[0] == 'true'
? `ufabcnext://login?token=${await user.generateJWT()}&`
:`${WEB_URL}/login?token=${user.generateJWT()}`
_redirect:
inApp.split('?')[0] == 'true'
? `ufabcnext://login?token=${await user.generateJWT()}&`
: `${WEB_URL}/login?token=${user.generateJWT()}`,
}
}

0 comments on commit a1218f0

Please sign in to comment.