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
"We view the cloud not as a specific technology but as a way of doing business and that way of doing business is transforming the way software, infrastructure and services are being delivered to business," explained Matthew Rosen, CEO and Director at Fusion, in this SYS-CON.tv interview at 18th Cloud Expo (http://www.CloudComputingExpo.com), held June 7-9 at the Javits Center in New York City, NY.
"We were founded in 2003 and the way we were founded was about good backup and good disaster recovery for our clients, and for the last 20 years we've been pretty consistent with that," noted Marc Malafronte, Territory Manager at StorageCraft, in this SYS-CON.tv interview at 20th Cloud Expo, held June 6-8, 2017, at the Javits Center in New York City, NY.
The Founder of NostaLab and a member of the Google Health Advisory Board, John is a unique combination of strategic thinker, marketer and entrepreneur. His career was built on the "science of advertising" combining strategy, creativity and marketing for industry-leading results. Combined with his ability to communicate complicated scientific concepts in a way that consumers and scientists alike can appreciate, John is a sought-after speaker for conferences on the forefront of healthcare science,...
WebRTC is great technology to build your own communication tools. It will be even more exciting experience it with advanced devices, such as a 360 Camera, 360 microphone, and a depth sensor camera. In his session at @ThingsExpo, Masashi Ganeko, a manager at INFOCOM Corporation, introduced two experimental projects from his team and what they learned from them. "Shotoku Tamago" uses the robot audition software HARK to track speakers in 360 video of a remote party. "Virtual Teleport" uses a multip...
"Software-defined storage is a big problem in this industry because so many people have different definitions as they see fit to use it," stated Peter McCallum, VP of Datacenter Solutions at FalconStor Software, in this SYS-CON.tv interview at 18th Cloud Expo, held June 7-9, 2016, at the Javits Center in New York City, NY.
As you move to the cloud, your network should be efficient, secure, and easy to manage. An enterprise adopting a hybrid or public cloud needs systems and tools that provide: Agility: ability to deliver applications and services faster, even in complex hybrid environments Easier manageability: enable reliable connectivity with complete oversight as the data center network evolves Greater efficiency: eliminate wasted effort while reducing errors and optimize asset utilization Security: implemen...
Data is the fuel that drives the machine learning algorithmic engines and ultimately provides the business value. In his session at Cloud Expo, Ed Featherston, a director and senior enterprise architect at Collaborative Consulting, discussed the key considerations around quality, volume, timeliness, and pedigree that must be dealt with in order to properly fuel that engine.
In his session at Cloud Expo, Alan Winters, U.S. Head of Business Development at MobiDev, presented a success story of an entrepreneur who has both suffered through and benefited from offshore development across multiple businesses: The smart choice, or how to select the right offshore development partner Warning signs, or how to minimize chances of making the wrong choice Collaboration, or how to establish the most effective work processes Budget control, or how to maximize project result...
"DivvyCloud as a company set out to help customers automate solutions to the most common cloud problems," noted Jeremy Snyder, VP of Business Development at DivvyCloud, in this SYS-CON.tv interview at 20th Cloud Expo, held June 6-8, 2017, at the Javits Center in New York City, NY.
CI/CD is conceptually straightforward, yet often technically intricate to implement since it requires time and opportunities to develop intimate understanding on not only DevOps processes and operations, but likely product integrations with multiple platforms. This session intends to bridge the gap by offering an intense learning experience while witnessing the processes and operations to build from zero to a simple, yet functional CI/CD pipeline integrated with Jenkins, Github, Docker and Azure...
"Our strategy is to focus on the hyperscale providers - AWS, Azure, and Google. Over the last year we saw that a lot of developers need to learn how to do their job in the cloud and we see this DevOps movement that we are catering to with our content," stated Alessandro Fasan, Head of Global Sales at Cloud Academy, in this SYS-CON.tv interview at 20th Cloud Expo, held June 6-8, 2017, at the Javits Center in New York City, NY.
"Akvelon is a software development company and we also provide consultancy services to folks who are looking to scale or accelerate their engineering roadmaps," explained Jeremiah Mothersell, Marketing Manager at Akvelon, 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.
IoT is rapidly becoming mainstream as more and more investments are made into the platforms and technology. As this movement continues to expand and gain momentum it creates a massive wall of noise that can be difficult to sift through. Unfortunately, this inevitably makes IoT less approachable for people to get started with and can hamper efforts to integrate this key technology into your own portfolio. There are so many connected products already in place today with many hundreds more on the h...
As organizations shift towards IT-as-a-service models, the need for managing and protecting data residing across physical, virtual, and now cloud environments grows with it. Commvault can ensure protection, access and E-Discovery of your data – whether in a private cloud, a Service Provider delivered public cloud, or a hybrid cloud environment – across the heterogeneous enterprise. In his general session at 18th Cloud Expo, Randy De Meno, Chief Technologist - Windows Products and Microsoft Part...
DXWorldEXPO LLC announced today that ICC-USA, a computer systems integrator and server manufacturing company focused on developing products and product appliances, will exhibit at the 22nd International CloudEXPO | DXWorldEXPO. DXWordEXPO New York 2018, colocated with CloudEXPO New York 2018 will be held November 11-13, 2018, in New York City. ICC is a computer systems integrator and server manufacturing company focused on developing products and product appliances to meet a wide range of ...