So currently, I have a page, and I need to call a route, and the JSON response it returns, I need to store it in a variable on my chat.blade.php page. I tried using {{route}} as you'll see in the code, but that wouldn't get me back to my chat.blade, unless I added a redirect(), ->route() to /me, but then I got stuck in a loop.
chat.blade.php
<div class="content">
<head>
<script>
// quick javascript to act as a client to our web socket server
// to state the obvious, this has nothing to do with PHP; this is javascript
var conn = new WebSocket('ws://localhost:9000');
var meObj = {};
// some basic call back functions when
// certain events happen on the web socket server
// when we successfully connect, log a simple message
conn.onopen = function (e) {
meObj = {{ route('profiles-me') }};
console.log("Connected!");
};
--- redacted to keep it short ---
MeController.php
--- Redacted to keep it short ---
public function me()
{
/** @var User $user */
$user = Auth::user();
// Lets extract the APP_KEY string to decode it.
$appKeyString = explode(':', env('APP_KEY'));
$secret = base64_decode($appKeyString[1]);
$signature = hash('sha256', $user->name . $secret);
return response()->json([
'me' => [
'id' => $user->id,
'name' => $user->name,
'picture' => $user->picture,
'csrf_token' => csrf_token(),
'sig' => $signature,
]
]);
}
--- Redacted to keep it short ---
Route
Route::get('/me', [MeController::class, 'me'])->name('profiles-me')->middleware('auth:profile');