I am creating an affiliate functionality on my eCommerce website, I have created link generate functionality, My main concept is if any user buys anything from the generated links the main user who created the URL will get a commission. I cannot be able to store the short generated links in the local storage of a user since when they click on these generated links, they are redirected directly to the targeted page. so I am trying to get the short generated URL from my node js backend. I am using node-localstorage package but it not working.
My expectation is when a user clicks on the generated short link, the generated link will be stored into the local storage anyhow from the frontend or backend.
Please note: the generated link will be shared on many platforms so I want before the user goes to the targeted page the short link will be stored into the local storage because if the user goes to the targeted page the short URL is removed automatically.
This is my frontend code: before executing this useEffect the backend redirects to the targeted page.
useLayoutEffect(() => {
setLoadings(true)
fetch(`http://localhost:5000/findUrl/${path}`)
.then(res => res.json())
.then(data => {
if (data.isUrlTrue) {
localStorage.setItem('affiliate', path)
}
})
.finally(() => setLoadings(false))
}, [path])
This is my backend code which is not working.
app.get('/:shortUrl', async (req, res) => {
const shortenUrl = await bicycleAffiliateLinksCollection.findOne({ short: req.params.shortUrl })
if (shortenUrl === null) return res.sendStatus(404)
// shortenUrl.clicks++
// shortenUrl.save()
localStorage.setItem('affliate', "asdasd");
res.redirect(shortenUrl.full)
})
Store the short URL in the local storage before the browser follows the link:
<a href="short_url" onclick='localStorage.setItem("affiliate","short_url")'>Buy it!</a>