-
-
Save zomars/34d9d30c75308a5e57d3c95db7cbef2c to your computer and use it in GitHub Desktop.
Magic Link 1
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
/** | |
* pass mobile number of the user for whom | |
* you want to create an authenticated link, | |
* this mobile number may/may not be in your | |
* user's database | |
*/ | |
function createMagicLink($mobileNumber, $targetScreenUrl) { | |
// generate a random string of length 8 | |
$randomString = generateRandomString(8); | |
$maskedRandomString = generateMaskedRandomString($randomString); | |
// now, store $maskedRandomString in your DB against this user's | |
// mobile number | |
// something like this -> | |
// insert into magic_links (id, masked_random_string, mobile_number, target_screen) | |
// values (123, $maskedRandomString, $mobileNumber, $targetScreenUrl); | |
// now you need to create the URL for the user | |
$host = "https://www.mysite.com"; | |
$magicLinkHandlerAPIEndpoint = "/magic_link_signup"; | |
$completeURL = $host + $magicLinkHandlerAPIEndpoint + "?token=" + $randomString; | |
// your URL will be something like this - | |
// https://www.mysite.com/magic_link_signup?token=abcd1234 | |
// generate a shortened URL - | |
// use https://bitly.com/ to get to know what exactly it is | |
// in our case it will be - https://bit.ly/2O2WQ0c | |
$shortenedURL = generateShortenedURL($completeURL); | |
// send this $shortenedURL to the mobile number | |
sendSMS($shortenedURL, $mobileNumber); | |
} | |
function generateMaskedRandomString($str) { | |
// create a hash of $str using any hashing technique | |
// I am using sha1 to generate the same. | |
return sha1($str); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment