Skip to content

Commit

Permalink
added automatic api call to server to get whatsapp status
Browse files Browse the repository at this point in the history
  • Loading branch information
Aromiii committed Feb 18, 2023
1 parent eca7e6c commit 3164def
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

/.firebase

# dependencies
/node_modules
/.pnp
Expand Down
30 changes: 27 additions & 3 deletions src/pages/message.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useState} from "react";
import React, {useEffect, useState} from "react";
import {getAppCheckTokenForApiCall, getIdTokenForApiCall} from "../firebaseConfig";
import axios, {AxiosResponse} from "axios";

Expand All @@ -12,6 +12,30 @@ export default function MessageWrapper() {
const [showQr, setShowQr] = useState(false)
const [selectedFile, setSelectedFile] = useState<File | null>(null)

useEffect(() => {
const x = async () => {
try {

// Getting tokens from Firebase to send along the API request, so backend can ensure that user has correct access rights
const idToken = await getIdTokenForApiCall()
const appCheckToken = await getAppCheckTokenForApiCall()

const result: AxiosResponse = await axios.get("http://localhost:3001/whatsapp/status", {
headers: {
"X-Firebase-IdToken": idToken,
"X-Firebase-AppCheck": appCheckToken.token
}
})

console.log(result.data)

} catch (error: any) {
handleMessageApiCallResponse(error)
}
}
x()
}, [])

const setFile = (event: any) => {
setSelectedFile(event.target.files[0])
}
Expand All @@ -22,10 +46,10 @@ export default function MessageWrapper() {
return
}

if (error.response.status == 409) {
if (error.response.data.type == "qr") {

// Regex replaces all + with %2B because + can't be used in urls parameter values, so they are converted to %2B which is equivalent
setQr(error.response.data.replace(/\+/g, "%2B"))
setQr(error.response.data.data.replace(/\+/g, "%2B"))

setShowQr(true)
return
Expand Down

0 comments on commit 3164def

Please sign in to comment.