Learn How to Create User Registration & Login Backend Using Server Code and Database
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.
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.
- Create a new database. The Users collection is automatically available in every new database.
- Now you need to create a new custom collection where additional user information will be stored. Create a collection called UserProfile.
- Add two columns to this collection: email and country. Both of string type.
- Go back to Users collection and add a column called to_userProfile, set its type to Pointer and select the UserProfile collection.
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.
- Create a new Server Code script called registerUser.
- 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:
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
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.
One more thing left to do is to create the script to login.
Creating the login script
- Create a new script and call it loginUser
- 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:
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.
Komentar
Posting Komentar