generated from ecomplus/application-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(authentication): set authentication route
- Loading branch information
1 parent
75399d8
commit f98603c
Showing
1 changed file
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | ||
}) | ||
} | ||
} |