Authorize Users

introduction

This simple guide will show you how to authenticate users with a simple nodejs/express service using the cyxth node sdk, you can also do this with our the REST api.

If you don’t have a cyxth instance already go to the cyxth console and create one with the features all you need and follow this guide to create and manage your channels, users and more.

this and other examples are in the cyxth examples repository here. you can clone the repo and run the examples locally or follow a long this tutorial.

authenticating users

let’s start by creating a simple server with express and use it to authorize users. If you are not using node feel free to skip this section and checkout the authentication examples repo on github to implement it using your language of choice.

# lets create a simple auth service with node
$ mkdir auth-service && cd auth-service
$ npm init -y
$ touch server.js
$ npm i express cors body-parser @cyxth/node

Add this code to your server.js

import express from 'express';
import * as BodyParser from 'body-parser';
import * as cors from 'cors';
import Cyxth from '@cyxth/node';

const APP_ID = 'your_app_id';
const APP_SECRET = process.env.YOUR_APP_SECRET;

const app = express();
app.use(BodyParser.default.json());
app.use(cors.default());
const cyxth = new Cyxth(APP_ID, APP_SECRET);

// auth
app.post('/authorize', (req, res) => {
	let userInfo = req.body;
	let token_data = cyxth.createToken({
		id: userInfo.id,
		name: userInfo.name,
		avatar: userInfo.avatar
	});

	res.json(token_data);
});

app.listen(8902, () => console.log('started server on port 8902'));

start the server

$ node server.js
started server on port 8902

Let’s test the service with cURL.

$ curl -X POST http://localhost:8902/authorize
    -H 'Content-Type: application/json'
    -d '{"name":"alice","id":"alice","avatar":"_"}'

This is the expected output.

{
	"token": "eyJ***some long jwt token***0",
	"code_challenge": "i81I46-EhLtn_o_wW5baaDk8YdM7pXyRjJFXDSvO27Q",
	"code_verifier": "WfQ_oNtNFRrtTKPu2VkLt7ocj6NU2CVvcUAIaRe_Rwg"
}
// your values will be different

If you are building a single page app you can deploy this simple auth service to a serverless platform such as lamba,deno deploy, google cloud functions etc.

we will be using this token data in the next part of this guide to authorize users

next on quickstart chat