Skip to content

Instantly share code, notes, and snippets.

@onedebos
Last active March 12, 2021 01:55
Show Gist options
  • Save onedebos/a4fd8761d2ae391c7867a407153fb773 to your computer and use it in GitHub Desktop.
Save onedebos/a4fd8761d2ae391c7867a407153fb773 to your computer and use it in GitHub Desktop.
pages/api/pusher/auth.js
import {pusher} from '../../../../lib/pusher'
export default async function handler( req, res ) {
// see https://pusher.com/docs/channels/server_api/authenticating-users
const { socket_id, channel_name, username, userLocation } = req.body;
// use JWTs here to authenticate users before continuing
const randomString = Math.random().toString(36).slice(2);
const presenceData = {
user_id: randomString,
user_info: {
username: "@" + username,
userLocation
},
};
try {
const auth = pusher.authenticate(socket_id, channel_name, presenceData);
res.send(auth);
} catch (error) {
console.error(error)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment