Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@LukeSmetham
Created September 23, 2019 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save LukeSmetham/cc7cb7d8147a0577588406c3bdbd88b1 to your computer and use it in GitHub Desktop.
Save LukeSmetham/cc7cb7d8147a0577588406c3bdbd88b1 to your computer and use it in GitHub Desktop.
import dotenv from 'dotenv';
import md5 from 'md5';
import { StreamChat } from 'stream-chat';
dotenv.config();
exports.token = async (req, res) => {
try {
const data = req.body;
let apiKey;
let apiSecret;
if (process.env.STREAM_URL) {
[apiKey, apiSecret] = process.env.STREAM_URL.substr(8)
.split('@')[0]
.split(':');
} else {
apiKey = process.env.STREAM_API_KEY;
apiSecret = process.env.STREAM_API_SECRET;
}
const client = new StreamChat(apiKey, apiSecret);
const user = Object.assign({}, data, {
id: md5(data.username),
role: 'admin',
image: `https://robohash.org/${data.username}`,
});
const token = client.createToken(user.id);
await client.updateUsers([user]);
res.status(200).json({ user, token, apiKey });
} catch (error) {
console.log(error);
res.status(500).json({ error: error.message });
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment