Welcome!

Blog Feed Post

Learn How to Create User Registration & Login Backend Using Server Code and Database

Appery.io platform provides a cloud database for storing any app data. The database also has a built-in user management feature. This means if your apps needs user login or user registration – this capability is available out-of-the-box.

Screen Shot 2015-12-31 at 12.56.22 PM

Appery.io database built-in user management

The Users collection has two default columns: username and password. In most cases you will need to store additional users information such as email, address, or anything else. One option is to create additional custom columns in the Users collection. Another option is to store any user information in a different database collection and then link the Users collection to that collection. In this blog post I’m going to show you how use a separate collection to store user information and how use Pointer column type to link the two collections.

Setting up the database

The first step is to setup the database.

  1. Create a new database. The Users collection is automatically available in every new database.
  2. Now you need to create a new custom collection where additional user information will be stored. Create a collection called UserProfile.
  3. Add two columns to this collection: email and country. Both of string type.
  4. Go back to Users collection and add a column called to_userProfile, set its type to Pointer and select the UserProfile collection.
Screen Shot 2015-12-31 at 1.21.59 PM

UserProfile collection with sample data

That’s all the setup you need to do.

Next you are going to create two Server Code scripts to register a user with additional information and login a user and retrieve the additional information.

Creating the register script

Using Server Code script allows you to combine all the logic into a single and invoke it via a single REST API request. Doing the same thing from the client would require multiple requests which would slow down the app and in general not recommended.

  1. Create a new Server Code script called registerUser.
  2. Copy and paste the code below into your newly created script.
// Database API key can be found in Settings tab in Database
var databaseId = "your_database_id";
var username = request.get("username");
var password = request.get("password");
var email = request.get("email");
var country = request.get("country");

// create user
var userLoginInfo = DatabaseUser.signUp(databaseId, {
  "username": username,
  "password": password
});

// create UserProfile
var userProfileInfo = Collection.createObject(databaseId, "UserProfile", {"email":email,"country":country});

// link
DatabaseUser.update(databaseId, userLoginInfo._id,{"to_userProfile":{"collName":"UserProfile", "_id":userProfileInfo._id}},userLoginInfo.sessionToken);

var user = {"username":username,"email":email,"country":country};

response.success(user, "application/json");

Let’s review the code.

  • Line 2 is where you set your database API key. The API key can be found in Settings tab in Database.
  • Line 3-6 is where the information submitted by the user is going to be passed to the script.
  • Line 9 you are using Server Code API to create a user new in the database (in the Users collection).
  • Line 15 you are populating the UserProfile information with email and country data.
  • Line 18 you are linking the newly created user to his/her profile information. You are linking a record in Users collection with a record in UserProfile collection via the Pointer data type.
  • Line 20 you are collecting together all the user information.
  • At the very end the script returns user information. This is the service (script) response body.

It’s always a good idea to test the script and that’s easily done.

First provide script input parameters:

Screen Shot 2015-12-31 at 1.47.10 PM

Script input parameters before testing

Switch to Run tab and click Save and run button to invoke the script.

Every script you create is automatically a REST API service. To find out its URL, simply switch to REST information tab

Screen Shot 2015-12-31 at 1.49.17 PM.png

Script REST API information

When you need to use this script in your app, you can quickly setup the service by using Create new > Server Code Services > your script.

Screen Shot 2015-12-31 at 1.51.23 PM

Setting up REST API for Server Code script in the App  Builder

One more thing left to do is to create the script to login.

Creating the login script

  1. Create a new script and call it loginUser
  2. Copy and paste the following script code:
// Database API key can be found in Settings tab in Database
var databaseId = "your_database_id";
var username = request.get("username");
var password = request.get("password");

var userLoginInfo = DatabaseUser.login(databaseId, username, password);

var user = DatabaseUser.retrieve(databaseId, userLoginInfo._id,"to_userProfile", userLoginInfo.sessionToken);

response.success(user, "application/json");

The login script is much simpler. You login and retrieve information from UserProfile collection in a single request.

  • Line 2 is where you set your database API key. The API key can be found in Settings tab in Database.
  • Line 3-4 is where you pass login information from the page to this script.
  • Line 6 is where you do the actual user login using Server Code database API.
  • Line 8 is where you get the linked information from UserProfile collection.
  • At the very end the script returns user information. This is the service (script) response body.

To test this script, go to Script parameters tab. Enter information for an exiting user, switch to Run tab and test the script.

Summary

Now that you have the user registration script, you can build a page like this and collection all registration information at once:

Screen Shot 2015-12-31 at 5.25.56 PM

Registration page

You created two Server Code scripts. One script for user registration and another script for user login. The reason you are using Server Code to do this is because in a single server-side action, you create a user, save additional user information, and link the Users collection with UserProfile collection. Doing all this from the client would require at least three separate requests: creating a user, saving user information and linking. Using Server Code is more elegant and will increase app performance.

Using Server Code reduces the number of requests sent from the client, simplifies app logic by placing it into a server-side script, and increases performance.


Read the original blog entry...

More Stories By Max Katz

Max Katz heads Developer Relations for Appery.io, a cloud-based mobile app platform. He loves trying out new and cool REST APIs in mobile apps. Max is the author of two books “Practical RichFaces” (Apress 2008, 2011), DZone MVB (Most Valuable Blogger), and is a frequent speaker at developer conferences. You can find out what Max is up to on his blog: http://maxkatz.org and Twitter: @maxkatz.

Latest Stories
"Digital transformation - what we knew about it in the past has been redefined. Automation is going to play such a huge role in that because the culture, the technology, and the business operations are being shifted now," stated Brian Boeggeman, VP of Alliances & Partnerships at Ayehu, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
The past few years have brought a sea change in the way applications are architected, developed, and consumed—increasing both the complexity of testing and the business impact of software failures. How can software testing professionals keep pace with modern application delivery, given the trends that impact both architectures (cloud, microservices, and APIs) and processes (DevOps, agile, and continuous delivery)? This is where continuous testing comes in. D
"Evatronix provides design services to companies that need to integrate the IoT technology in their products but they don't necessarily have the expertise, knowledge and design team to do so," explained Adam Morawiec, VP of Business Development at Evatronix, in this SYS-CON.tv interview at @ThingsExpo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
"WineSOFT is a software company making proxy server software, which is widely used in the telecommunication industry or the content delivery networks or e-commerce," explained Jonathan Ahn, COO of WineSOFT, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
Smart cities have the potential to change our lives at so many levels for citizens: less pollution, reduced parking obstacles, better health, education and more energy savings. Real-time data streaming and the Internet of Things (IoT) possess the power to turn this vision into a reality. However, most organizations today are building their data infrastructure to focus solely on addressing immediate business needs vs. a platform capable of quickly adapting emerging technologies to address future ...
Mobile device usage has increased exponentially during the past several years, as consumers rely on handhelds for everything from news and weather to banking and purchases. What can we expect in the next few years? The way in which we interact with our devices will fundamentally change, as businesses leverage Artificial Intelligence. We already see this taking shape as businesses leverage AI for cost savings and customer responsiveness. This trend will continue, as AI is used for more sophistica...
There is a huge demand for responsive, real-time mobile and web experiences, but current architectural patterns do not easily accommodate applications that respond to events in real time. Common solutions using message queues or HTTP long-polling quickly lead to resiliency, scalability and development velocity challenges. In his session at 21st Cloud Expo, Ryland Degnan, a Senior Software Engineer on the Netflix Edge Platform team, will discuss how by leveraging a reactive stream-based protocol,...
In his Opening Keynote at 21st Cloud Expo, John Considine, General Manager of IBM Cloud Infrastructure, led attendees through the exciting evolution of the cloud. He looked at this major disruption from the perspective of technology, business models, and what this means for enterprises of all sizes. John Considine is General Manager of Cloud Infrastructure Services at IBM. In that role he is responsible for leading IBM’s public cloud infrastructure including strategy, development, and offering m...
Sanjeev Sharma Joins June 5-7, 2018 @DevOpsSummit at @Cloud Expo New York Faculty. Sanjeev Sharma is an internationally known DevOps and Cloud Transformation thought leader, technology executive, and author. Sanjeev's industry experience includes tenures as CTO, Technical Sales leader, and Cloud Architect leader. As an IBM Distinguished Engineer, Sanjeev is recognized at the highest levels of IBM's core of technical leaders.
Product connectivity goes hand and hand these days with increased use of personal data. New IoT devices are becoming more personalized than ever before. In his session at 22nd Cloud Expo | DXWorld Expo, Nicolas Fierro, CEO of MIMIR Blockchain Solutions, will discuss how in order to protect your data and privacy, IoT applications need to embrace Blockchain technology for a new level of product security never before seen - or needed.
The 22nd International Cloud Expo | 1st DXWorld Expo has announced that its Call for Papers is open. Cloud Expo | DXWorld Expo, to be held June 5-7, 2018, at the Javits Center in New York, NY, brings together Cloud Computing, Digital Transformation, Big Data, Internet of Things, DevOps, Machine Learning and WebRTC to one location. With cloud computing driving a higher percentage of enterprise IT budgets every year, it becomes increasingly important to plant your flag in this fast-expanding busin...
Digital transformation is about embracing digital technologies into a company's culture to better connect with its customers, automate processes, create better tools, enter new markets, etc. Such a transformation requires continuous orchestration across teams and an environment based on open collaboration and daily experiments. In his session at 21st Cloud Expo, Alex Casalboni, Technical (Cloud) Evangelist at Cloud Academy, explored and discussed the most urgent unsolved challenges to achieve f...
SYS-CON Events announced today that Synametrics Technologies will exhibit at SYS-CON's 22nd International Cloud Expo®, which will take place on June 5-7, 2018, at the Javits Center in New York, NY. Synametrics Technologies is a privately held company based in Plainsboro, New Jersey that has been providing solutions for the developer community since 1997. Based on the success of its initial product offerings such as WinSQL, Xeams, SynaMan and Syncrify, Synametrics continues to create and hone inn...
Digital Transformation (DX) is not a "one-size-fits all" strategy. Each organization needs to develop its own unique, long-term DX plan. It must do so by realizing that we now live in a data-driven age, and that technologies such as Cloud Computing, Big Data, the IoT, Cognitive Computing, and Blockchain are only tools. In her general session at 21st Cloud Expo, Rebecca Wanta explained how the strategy must focus on DX and include a commitment from top management to create great IT jobs, monitor ...
In a recent survey, Sumo Logic surveyed 1,500 customers who employ cloud services such as Amazon Web Services (AWS), Microsoft Azure, and Google Cloud Platform (GCP). According to the survey, a quarter of the respondents have already deployed Docker containers and nearly as many (23 percent) are employing the AWS Lambda serverless computing framework. It’s clear: serverless is here to stay. The adoption does come with some needed changes, within both application development and operations. Tha...