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
axios.post("https://your-server-url/init-insta", { | |
code: 'code-query-param-from-url', | |
redirectUrl: 'redirect-url' // needs to be registered at fb developer console | |
}) | |
.then(({ data }) => { | |
// handle success case | |
}) |
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
// Invoke this function on button click or whatever other use case | |
function setupInsta(){ | |
let appId = "XXXXXXXXXXXXXX"; | |
let redUri = window.location.origin + "/insta"; | |
let url = `https://api.instagram.com/oauth/authorize?client_id=${appId}&redirect_uri=${redUri}&scope=user_profile,user_media&response_type=code`; | |
window.open(url, "_blank").focus(); | |
} |
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
app.post('/api/verify-token', (req, res) => { | |
const { otp, request_id } = req.body; | |
if (!otp) return res.status(400).send({ message: "Otp is required" }); | |
if (!request_id) return res.status(400).send({ message: "Invalid request" }); | |
verifyOtp(request_id, otp, async (result) => { | |
if (result) { | |
// ... | |
res.send({ message: "Success" }); | |
} else { |
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
export const verifyOtp = (request_id, code, callback) => { | |
vonage.verify.check({ request_id, code }, (err, result) => { | |
if (err) { | |
console.error(err); | |
callback(false); | |
} else { | |
if (result.status != '0') { | |
console.log("====verify err====", result); | |
callback(false); | |
} else { |
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
app.post('/api/request-token', (req, res) => { | |
let { phoneNo } = req.body; | |
if (!phoneNo) return res.status(400).send({ message: "Phone no. is required" }); | |
phoneNo = phoneNo.replace(/\s/g, ""); | |
console.log("Sending code to ", phoneNo); | |
requestCode(phoneNo, (result) => { | |
if (result) { | |
// success at 1st attempt |
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
export const cancelOtp = (request_id, callback) => { | |
vonage.verify.control({ | |
request_id: request_id, | |
cmd: 'cancel' | |
}, (err, result) => { | |
if (err) { | |
console.error("====cancel err", err); | |
callback(false); | |
} else { | |
console.log(result); |
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
export const requestCode = (phoneNo, callback) => { | |
vonage.verify.request({ | |
number: phoneNo, | |
brand: "Your App/Website Name" | |
}, (err, result) => { | |
if (err) { | |
console.error(err); | |
callback(false); | |
} else { | |
if (result.request_id) { |
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
const Vonage = require('@vonage/server-sdk'); | |
const vonage = new Vonage({ | |
apiKey: process.env.VONAGE_KEY, | |
apiSecret: process.env.VONAGE_SECRET | |
}); | |
export const requestCode = (phoneNo, callback) => { | |
//... | |
}); |
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
const express = require('express') | |
const app = express() | |
const port = 3000 | |
const { requestCode, verifyOtp, cancelOtp } = require("./vonage.service.js"); | |
app.post('/api/request-token', (req, res) => { | |
//... | |
}) | |
app.post('/api/verify-token', (req, res) => { |
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
let data = []; | |
try { | |
let query = { | |
index: 'index-name', | |
body: { | |
query: { | |
bool: { | |
// ... | |
} | |
}, |