What this sample offers is a very basic login and sign-up endpoint that interacts with the Stream Feeds and Chat API to supply you with user access tokens. With these tokens, you can then integrate using one of our client SDKs or Rest API.
Getting Started
First, have a look at the git repository containing the sample code.
What you want is all contained in the server
directory.
123git clone https://github.com/GetStream/stream-node-simple-integration-sample.git cd stream-node-simple-integration-sample ls
As you can see in the server/routes/auth.js
file, this codebase is very straightforward.
12345678910const express = require("express"); const { signup, login } = require("../controllers/auth.js"); const router = express.Router(); router.post("/signup", signup); router.post("/login", login); module.exports = router;
Just two routes, and you can probably guess the purpose of each.
Before you can run our server, make sure to add your Stream credentials to the .env
file. Copy the sample and replace the placeholders in the .env
file.
I am using vi
(I should say vim
), but use your own editor of choice. You can get out of Vim by pressing esc
and then :q!
followed by <enter>
.
12cp server/.env.sample server/.env vi server/.env
The .env
file contains placeholders:
STREAM_APP_ID = [stream-app-id]
STREAM_API_KEY = [stream-api-key]
STREAM_API_SECRET = [stream-api-secret]
Make sure to replace the placeholder including the brackets.
The end result should look somewhat like this:
Note, the credentials in the next snippet are NOT valid.
STREAM_APP_ID = 999xx99
STREAM_API_KEY = bcdefgssdad
STREAM_API_SECRET =
dlfjslf78sdfslmdfu283rfndslf934alsjf239u84raslkdfj0239uriwesflsd
Once you configured your credentials it is time to run things.
The steps are straightforward. CD into the server directory, run yarn install
or npm install
and then run yarn start
or npm start
.
I'm going with the NPM route myself.
123cd server npm install npm start
If everything went correctly you should see this at the bottom of your terminal:
Server running on port 8080
Testing the integration with your new backend
You can use Postman or any other API testing tool for this as well. But to keep things simple, I will stick with the terminal.
Try and run a curl command that accesses the login
endpoint.
123456curl -X "POST" "http://localhost:8080/auth/login" \ -H 'Content-Type: application/json; charset=utf-8' \ -d $'{ "username": "jeroen", "password": "password" }'
You will get a message back stating the user does not exist.
1{"message":"User with this username doesn't exist."}
Which is correct, I did not register yet.
So let's try that next.
123456curl -X "POST" "http://localhost:8080/auth/signup" \ -H 'Content-Type: application/json; charset=utf-8' \ -d $'{ "username": "jeroen", "password": "password" }'
The output of this command looks a whole lot more interesting:
123456{ "feedToken":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiYWE2NmM0MjEzMzJlM2JmMzdiYWQ2ZWRiMDYyZWY4OTYifQ.3u7tBgtXq4TeO15sRSsQwlorFim0BQGKJlw3JP2cxJo", "chatToken":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiYWE2NmM0MjEzMzJlM2JmMzdiYWQ2ZWRiMDYyZWY4OTYifQ.3u7tBgtXq4TeO15sRSsQwlorFim0BQGKJlw3JP2cxJo", "username":"jeroen", "userId":"aa66c421332e3bf37bad6edb062ef896" }
You should get something similar on your backend as well.
Now let's try that login command again.
123456curl -X "POST" "http://localhost:8080/auth/login" \ -H 'Content-Type: application/json; charset=utf-8' \ -d $'{ "username": "jeroen", "password": "password" }'
123456{ "feedToken":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiYWE2NmM0MjEzMzJlM2JmMzdiYWQ2ZWRiMDYyZWY4OTYifQ.3u7tBgtXq4TeO15sRSsQwlorFim0BQGKJlw3JP2cxJo", "chatToken":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiYWE2NmM0MjEzMzJlM2JmMzdiYWQ2ZWRiMDYyZWY4OTYifQ.3u7tBgtXq4TeO15sRSsQwlorFim0BQGKJlw3JP2cxJo", "username":"jeroen", "userId":"aa66c421332e3bf37bad6edb062ef896" }
It now works.
Notice the chatToken
and feedToken
. Those are the credentials you need to initialize one of our SDKs from the user side.
Happy coding.