Skip to content

Commit

Permalink
feat(authentication): set authentication route
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusgnreis authored Jan 2, 2024
1 parent 75399d8 commit f98603c
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions functions/routes/bling/authentication.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
const getAppData = require('./../../lib/store-api/get-app-data')
const updateAppData = require('./../../lib/store-api/update-app-data')

exports.post = ({ appSdk, admin }, req, res) => {
console.log('>> POST BLING')
const { body, query } = req
const { state, code } = query
const storeId = parseInt(state, 10)
console.log('>> Store: ', storeId, ' code: ', code, ' <<')
if (storeId > 100 && code) {
return appSdk.getAuth(storeId)
.then(async (auth) => {
try {
await updateAppData({ appSdk, storeId, auth }, {
code
})
res.status(200).redirect('https://app.e-com.plus/#/apps/edit/102418/')
} catch (error) {
console.error(error)
const { response, config } = error
let status
if (response) {
status = response.status
const err = new Error(`#${storeId} Bling Webhook error ${status}`)
err.url = config && config.url
err.status = status
err.response = JSON.stringify(response.data)
console.error(err)
}
if (!res.headersSent) {
res.send({
status: status || 500,
msg: `#${storeId} Bling Webhook error`
})
}
}
})
.catch(() => {
console.log('Unauthorized')
if (!res.headersSent) {
res.sendStatus(401)
}
})
res.status(200)
} else {
return res.send({
status: 404,
msg: `StoreId #${storeId} not found`
})
}
}

exports.get = ({ appSdk, admin }, req, res) => {
console.log('>> POST BLING')
const { body, query } = req
const { state, code } = query
const storeId = parseInt(state, 10)
console.log('>> Store: ', storeId, ' code: ', code, ' <<')
if (storeId > 100 && code) {
return appSdk.getAuth(storeId)
.then(async (auth) => {
try {
await updateAppData({ appSdk, storeId, auth }, {
code
})
res.status(200).redirect('https://app.e-com.plus/#/apps/edit/102418/')
} catch (error) {
console.error(error)
const { response, config } = error
let status
if (response) {
status = response.status
const err = new Error(`#${storeId} Bling Webhook error ${status}`)
err.url = config && config.url
err.status = status
err.response = JSON.stringify(response.data)
console.error(err)
}
if (!res.headersSent) {
res.send({
status: status || 500,
msg: `#${storeId} Bling Webhook error`
})
}
}
})
.catch(() => {
console.log('Unauthorized')
if (!res.headersSent) {
res.sendStatus(401)
}
})
res.status(200)
} else {
return res.send({
status: 404,
msg: `StoreId #${storeId} not found`
})
}
}

0 comments on commit f98603c

Please sign in to comment.