Skip to content

Instantly share code, notes, and snippets.

@cronoh
Created January 21, 2017 23:34
Show Gist options
  • Save cronoh/6f91dcd8a08b57ed24356cb7218970db to your computer and use it in GitHub Desktop.
Save cronoh/6f91dcd8a08b57ed24356cb7218970db to your computer and use it in GitHub Desktop.
Socket IO JWT Flow
public function launchWidget($oAuthToken)
{
// .. Redacted lots of code
$loyaltyIoJwt = $this->loyaltyIoService->createJwt(['user' => $laravelUser->id, 'scopes' => ['**']]);
return view('pages.loyalty.widget', [
'jwt' => $loyaltyIoJwt,
]);
}
<script type="text/javascript">
window.jwt = '{{ $jwt }}';
// This will then later get used by whatever javascript iniates the socket io connection
</script>
// This gets run as soon as a new connection is opened
function forceAuth(ws, req) {
ws.authenaticated = false;
if (req.query && req.query.token) {
const auth = validateJwt(req.query.token);
if (auth) {
console.log('Connection authenticated successfully!', auth);
ws.auth = auth;
ws.authenticated = true;
}
}
if (!ws.authenticated) {
ws.close();
}
return ws;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment