Fixed obtaining username in web socket docs to work for new auth.

This commit is contained in:
Martin Sosic 2024-01-23 11:43:32 +01:00
parent eb93a89e9a
commit b19907b431

View File

@ -71,10 +71,11 @@ This is how we can define our `webSocketFn` function:
```ts title=src/server/webSocket.js
import { v4 as uuidv4 } from 'uuid'
import { getFirstProviderUserId } from '@wasp/auth/user.js'
export const webSocketFn = (io, context) => {
io.on('connection', (socket) => {
const username = socket.data.user?.email || socket.data.user?.username || 'unknown'
const username = getFirstProviderUserId(socket.data.user) ?? 'Unknown'
console.log('a user connected: ', username)
socket.on('chatMessage', async (msg) => {
@ -92,10 +93,11 @@ export const webSocketFn = (io, context) => {
```ts title=src/server/webSocket.ts
import type { WebSocketDefinition, WaspSocketData } from '@wasp/webSocket'
import { v4 as uuidv4 } from 'uuid'
import { getFirstProviderUserId } from '@wasp/auth/user.js'
export const webSocketFn: WebSocketFn = (io, context) => {
io.on('connection', (socket) => {
const username = socket.data.user?.email || socket.data.user?.username || 'unknown'
const username = getFirstProviderUserId(socket.data.user) ?? 'Unknown'
console.log('a user connected: ', username)
socket.on('chatMessage', async (msg) => {
@ -332,4 +334,4 @@ The `webSocket` dict has the following fields:
- `autoConnect: bool`
Whether to automatically connect to the WebSocket server. Default: `true`.
Whether to automatically connect to the WebSocket server. Default: `true`.