Created
January 19, 2023 21:33
-
-
Save Echooff3/180d96140e7d4d564fa85f05912ebc60 to your computer and use it in GitHub Desktop.
Very simple link expiration for React Aps
This file contains hidden or 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
if(window.location.search.indexOf('key') > -1 ) | |
{ | |
try { | |
// Could probably do this better but I'm being lazy | |
const linkKey = window.location.href.toString().split('=')[1] | |
// Side effect of being lazy means I have to add back the 2 == at the end of the string I just split | |
var bytes = CryptoJS.AES.decrypt(linkKey + '==', KEY); | |
var originalText = bytes.toString(CryptoJS.enc.Utf8); | |
passUser = moment(originalText).valueOf() > moment().valueOf() | |
} catch (error) { | |
passUser = false; | |
} | |
} | |
if(!passUser) { | |
return <NoAccess /> | |
} else { | |
<Access /> | |
} |
This file contains hidden or 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
import React from "react"; | |
import CryptoJS from "crypto-js"; | |
import moment from "moment/moment"; | |
const KEY = 'somekey' | |
export default _ => { | |
var ciphertext = CryptoJS.AES.encrypt(moment().add(1, 'days').endOf('day').toString(), KEY).toString(); | |
var bytes = CryptoJS.AES.decrypt(ciphertext, KEY); | |
var originalText = bytes.toString(CryptoJS.enc.Utf8); | |
var isLinkValid = moment(originalText).valueOf() > moment().valueOf() | |
const href = `${window.location.protocol}//${window.location.host}?key=${ciphertext}`; | |
return(<pre className="my-8">{href}</pre>) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you need a down and dirty link expiration this code works just fine. However, someone savvy could reverse engineer it from inspecting the code. DO NOT USE FOR SENSITIVE INFO!