LEARNING

Userless/Passwordless Authentication Flow

Read First

If you have not already set up and configured the Circle REST API, please read:

In order to understand how Circle Credential-free Authentication works, and some of the things you should consider in deciding how to implement it, please read in our Learning Center:

 

Creating Userless/Passwordless authentication flow for a website

Before we start, let's define some words we will use during this example:

  • Our Server or server-side (our backend, in our case, a NodeJS server) - not part of Circle Service
  • Our DB (any Database we have that is accessed on the server-side) - not part of Circle Service

 

Saving the user information to retrieve later

First, let's sign the user up to get the information we need to create his user_id For this example, we will require only:

  • His nickname

After he provides his nickname (Bob), make sure he has the Circle Service installed and running (check First Application)

Let's save the nickname (Bob) he provided and create in our server his user_id

Now, using Circle Service, let's create a Circle. Let's suppose that his user_id is 123

// Global variables for this example
let _circleId, _topicId, _userInfo
const userCircle = await Circle.createCircle("Bob's Circle", "");
if (userCircle && userCircle.Status.Result) {
  _circleId = userCircle.CircleId;
  // _circleId above is now the just created Circle id
  // if we want, we could also save in our DB, his Circle ID, but that is not needed in this example
}
									

Now that we have a Circle, let's create a private topic to store his information later, the variable circleIdOk, now with his information, 

const userPvtTopic = await Circle.createTopic(_circleId, "Bob private topic", "", true);
if (userPvtTopic && userPvtTopic.Status.Result) {
  _topicId = userPvtTopic.TopicId;
  // _topicId above is now the just created Topic ID
  // if we want, we could also save in our DB, his Topic ID, but that is not needed in this example
}			

Ok, now with his information, we create an object and save it inside the topic, so we can use it later:

_userInfo = {
  "nickname": "Bob",
  "user-id": "123"
}
const addMessage = await Circle.addMessage(_circleId, _topicId, 100, 20, "user-info", "", "", "", JSON.stringify(_userInfo));
if (addMessage && addMessage.Status.Result) {
  const messageId = addMessage.Message.MessageId;
  // messageId above is the id of the message
  // if we want, we could also save that in our DB, but that is not needed as well
}
					
				

Ok, now we have all that we need for that user.

 

Retrieving the user information

Let's suppose he returns to our website the next day. We just need to:

  • Make sure he has the Circle Service installed and running (check First Application)
  • Get his Circle
  • Get his private topic
  • Get the message and read the content

Let's see one by one:

 

Get his Circle

const enumCircles = await Circle.enumCircles();
if (enumCircles && enumCircles.Status.Result) {
  const circles = enumCircles.CircleMeta;
  if (circles.length > 0) {
   _circleId = circles[0].CircleId
  }
  // here we could also check the circles against our DB to see if we find his circle id
  // but as he will have only one circle, that is enough
}					
				

 

Get his private topic

Now, with the _circleId we can fetch the topics (in this case, the topic)

const enumTopics = await Circle.enumTopics(_circleId);
if (enumTopics && enumTopics.Status.Result) {
  const topics = enumTopics.Topics;
  if (topics.length > 0) {
   _topicId = topics[0].TopicId
  }
  // here we could also check the topics against our DB to see if we find his topic id
  // but as he will have only one topic, that is enough
}				

 

Get the message and read the content

With the _circleId and _topicId we can fetch the message to get his info back

const getMessages = await Circle.getMessages(_circleId, _topicId, [100]);
if (getMessages && getMessages.Status.Result) {
  const messages = getMessages.Messages;
  if (messages.length > 0) {
   _userInfo = JSON.parse(messages[0].AdditionalJson)
  }
  // Now you can welcome your user
  alert("Welcome, " + _userInfo.nickname)
}				

Ok, as you can see, we now have the _userInfo with the nickname and user-id, we can use those information to do whatever we want.

Hope you guys enjoyed it!
Have a great day and continue developing safely and securely with Circle Service.