SYS-CON MEDIA Authors: Pat Romanski, Gary Arora, Zakia Bouachraoui, Yeshim Deniz, Liz McMillan

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
Every organization is facing their own Digital Transformation as they attempt to stay ahead of the competition, or worse, just keep up. Each new opportunity, whether embracing machine learning, IoT, or a cloud migration, seems to bring new development, deployment, and management models. The results are more diverse and federated computing models than any time in our history.
On-premise or off, you have powerful tools available to maximize the value of your infrastructure and you demand more visibility and operational control. Fortunately, data center management tools keep a vigil on memory contestation, power, thermal consumption, server health, and utilization, allowing better control no matter your cloud's shape. In this session, learn how Intel software tools enable real-time monitoring and precise management to lower operational costs and optimize infrastructure...
"Calligo is a cloud service provider with data privacy at the heart of what we do. We are a typical Infrastructure as a Service cloud provider but it's been designed around data privacy," explained Julian Box, CEO and co-founder of Calligo, 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.
Isomorphic Software is the global leader in high-end, web-based business applications. We develop, market, and support the SmartClient & Smart GWT HTML5/Ajax platform, combining the productivity and performance of traditional desktop software with the simplicity and reach of the open web. With staff in 10 timezones, Isomorphic provides a global network of services related to our technology, with offerings ranging from turnkey application development to SLA-backed enterprise support. Leadin...
While a hybrid cloud can ease that transition, designing and deploy that hybrid cloud still offers challenges for organizations concerned about lack of available cloud skillsets within their organization. Managed service providers offer a unique opportunity to fill those gaps and get organizations of all sizes on a hybrid cloud that meets their comfort level, while delivering enhanced benefits for cost, efficiency, agility, mobility, and elasticity.
DevOps has long focused on reinventing the SDLC (e.g. with CI/CD, ARA, pipeline automation etc.), while reinvention of IT Ops has lagged. However, new approaches like Site Reliability Engineering, Observability, Containerization, Operations Analytics, and ML/AI are driving a resurgence of IT Ops. In this session our expert panel will focus on how these new ideas are [putting the Ops back in DevOps orbringing modern IT Ops to DevOps].
Darktrace is the world's leading AI company for cyber security. Created by mathematicians from the University of Cambridge, Darktrace's Enterprise Immune System is the first non-consumer application of machine learning to work at scale, across all network types, from physical, virtualized, and cloud, through to IoT and industrial control systems. Installed as a self-configuring cyber defense platform, Darktrace continuously learns what is ‘normal' for all devices and users, updating its understa...
Enterprises are striving to become digital businesses for differentiated innovation and customer-centricity. Traditionally, they focused on digitizing processes and paper workflow. To be a disruptor and compete against new players, they need to gain insight into business data and innovate at scale. Cloud and cognitive technologies can help them leverage hidden data in SAP/ERP systems to fuel their businesses to accelerate digital transformation success.
Concerns about security, downtime and latency, budgets, and general unfamiliarity with cloud technologies continue to create hesitation for many organizations that truly need to be developing a cloud strategy. Hybrid cloud solutions are helping to elevate those concerns by enabling the combination or orchestration of two or more platforms, including on-premise infrastructure, private clouds and/or third-party, public cloud services. This gives organizations more comfort to begin their digital tr...
Most organizations are awash today in data and IT systems, yet they're still struggling mightily to use these invaluable assets to meet the rising demand for new digital solutions and customer experiences that drive innovation and growth. What's lacking are potent and effective ways to rapidly combine together on-premises IT and the numerous commercial clouds that the average organization has in place today into effective new business solutions.
Keeping an application running at scale can be a daunting task. When do you need to add more capacity? Larger databases? Additional servers? These questions get harder as the complexity of your application grows. Microservice based architectures and cloud-based dynamic infrastructures are technologies that help you keep your application running with high availability, even during times of extreme scaling. But real cloud success, at scale, requires much more than a basic lift-and-shift migrati...
David Friend is the co-founder and CEO of Wasabi, the hot cloud storage company that delivers fast, low-cost, and reliable cloud storage. Prior to Wasabi, David co-founded Carbonite, one of the world's leading cloud backup companies. A successful tech entrepreneur for more than 30 years, David got his start at ARP Instruments, a manufacturer of synthesizers for rock bands, where he worked with leading musicians of the day like Stevie Wonder, Pete Townsend of The Who, and Led Zeppelin. David has ...
Darktrace is the world's leading AI company for cyber security. Created by mathematicians from the University of Cambridge, Darktrace's Enterprise Immune System is the first non-consumer application of machine learning to work at scale, across all network types, from physical, virtualized, and cloud, through to IoT and industrial control systems. Installed as a self-configuring cyber defense platform, Darktrace continuously learns what is ‘normal' for all devices and users, updating its understa...
Dion Hinchcliffe is an internationally recognized digital expert, bestselling book author, frequent keynote speaker, analyst, futurist, and transformation expert based in Washington, DC. He is currently Chief Strategy Officer at the industry-leading digital strategy and online community solutions firm, 7Summits.
Addteq is a leader in providing business solutions to Enterprise clients. Addteq has been in the business for more than 10 years. Through the use of DevOps automation, Addteq strives on creating innovative solutions to solve business processes. Clients depend on Addteq to modernize the software delivery process by providing Atlassian solutions, create custom add-ons, conduct training, offer hosting, perform DevOps services, and provide overall support services.