Created
January 21, 2017 23:34
-
-
Save cronoh/6f91dcd8a08b57ed24356cb7218970db to your computer and use it in GitHub Desktop.
Socket IO JWT Flow
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
public function launchWidget($oAuthToken) | |
{ | |
// .. Redacted lots of code | |
$loyaltyIoJwt = $this->loyaltyIoService->createJwt(['user' => $laravelUser->id, 'scopes' => ['**']]); | |
return view('pages.loyalty.widget', [ | |
'jwt' => $loyaltyIoJwt, | |
]); | |
} |
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
<script type="text/javascript"> | |
window.jwt = '{{ $jwt }}'; | |
// This will then later get used by whatever javascript iniates the socket io connection | |
</script> |
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
// 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