Created
June 13, 2020 18:09
-
-
Save agentcobra/8b92896c51ba5078db2f9bd9498a9f17 to your computer and use it in GitHub Desktop.
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
<?php | |
function getUserInfos( | |
$access_token, | |
$jam = [ | |
'app_id' => '', | |
'redirect_url' => '', | |
'api_secret' => '' | |
] | |
) { | |
$response = json_decode(file_get_contents( | |
"https://core.justauth.me/api/data?secret=" . $jam['api_secret'] . "&access_token=$access_token", | |
false, | |
stream_context_create([ | |
'http' => [ | |
'ignore_errors' => true | |
] | |
]) | |
), true); | |
switch (substr($http_response_header[0], 9, 3)) { | |
case 200: | |
return $response; | |
break; | |
case 400: | |
throw new \Exception( | |
'JamBadRequestException:Access-Token and API Secret are required. | |
Please contact support@justauth.me', | |
400 | |
); | |
break; | |
case 401: | |
throw new \Exception('JamUnauthorizedException:Api Secret is invalid', 401); | |
break; | |
case 404: | |
throw new \Exception('JamNotFoundException:No such Access-Token', 404); | |
break; | |
case 500: | |
throw new \Exception( | |
'JamInternalServerErrorException:Wrong data format. | |
Please contact support@justauth.me', | |
500 | |
); | |
break; | |
default: | |
throw new \Exception('JamUnknowErrorException:Unknow error. Please contact support@justauth.me'); | |
} | |
} | |
if (isset($_GET['access_token'])) { | |
// The expected access_token is present | |
try { | |
$id = getUserInfos($_GET['access_token']); | |
/* | |
* Everything is fine, you can now register or login the user, | |
* depending on the presence in your Database of | |
* the provided $user_infos->jam_id | |
*/ | |
} catch (Exception $e) { | |
die($e->getMessage()); | |
// Login fail, you should redirect to an error page | |
header('Location: /index.php'); | |
exit; | |
} | |
} else { | |
/* | |
* The callback URL wasn't called with the correct parameter | |
* you should redirect to an error page | |
*/ | |
header('Location: /index.php'); | |
exit; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment