Skip to content

Commit

Permalink
chore: rollback changes
Browse files Browse the repository at this point in the history
  • Loading branch information
brMonteiro-G committed Nov 16, 2023
1 parent a1218f0 commit 01ab4ba
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 99 deletions.
15 changes: 15 additions & 0 deletions app/api/facebook/func.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const app = require('@/app')

module.exports = async (context) => {
let { ra, email } = context.body.ra

const user = await app.models.users.findOne({ ra, email })

const { _id } = user

if (user) {
return { userId: _id }
}

return null
}
5 changes: 5 additions & 0 deletions app/api/facebook/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const app = require('@/app')

module.exports = async (router) => {
router.post('/facebook/sync', app.helpers.routes.func(require('./func.js')))
}
164 changes: 65 additions & 99 deletions app/setup/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,153 +6,119 @@ 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', {})

// 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 findConditions = [
// { 'oauth.facebook': faceUser.id }
// ]
async function facebook (context) {
const { inApp = '', userId = '', env = '' } = _.get(context.session, 'grant.dynamic', {})
console.log('this is the context', context.query)
const accessToken = context.query.access_token
const url = `https://graph.facebook.com/v18.0/me?fields=id,email&access_token=${accessToken}`
const resp = await Axios.get(url)

//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 faceUser = resp.data

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

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

// if (userId) {
// findConditions.push({ _id: userId.split('?')[0] })
// }
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 {
throw new Error('Cannot login with facebook, please connect with google')
user = new App.models.users({
oauth: {
email: faceUser.email,
facebook: faceUser.id,
picture: faceUser.picture.data.url
}
})
}

// 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()}`,
// };
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, user) {
const {
inApp = '',
userId = '',
env = '',
} = _.get(context.session, 'grant.dynamic', {})
async function google(context) {
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 01ab4ba

Please sign in to comment.