Skip to content

Instantly share code, notes, and snippets.

@ifindev
Last active February 11, 2021 02:19
Show Gist options
  • Save ifindev/b7181057038e84d848148e978146a500 to your computer and use it in GitHub Desktop.
Save ifindev/b7181057038e84d848148e978146a500 to your computer and use it in GitHub Desktop.
A Simple Public API for User Creation

userAPI

This simple API is a self improvement of the example given by You Don't Know Js book about closure and modules. In the closure and module section, the author made an example of a simple public API for user creation. After reading that, I was inspired to add some more publicAPI functions for more general use cases. This is the original code from the book:

function User(){
	var username, password;

	function doLogin(user,pw) {
		username = user;
		password = pw;

		// do the rest of the login work
	}

	var publicAPI = {
		login: doLogin
	};

	return publicAPI;
}

// create a `User` module instance
var fred = User();

fred.login( "fred", "12Battery34!" );

For those who don't yet understood what closure is, basically it is a javascript thing which can be used for a function to remember what was given in the first variable scope. I don't really know how to describe it using words, but I understand what it means by looking at codes that implement it.

/*
Subject: userAPI for user registration, login, add user bio, and show user info and bio.
Author : Muhammad Arifin (@ifindev)
*/
"use strict";
function User() {
var username, password;
var email, userBio;
/* register new user account */
function doRegister(user, pw, mail) {
// TODO: Add email & password validations
// example: email must contain @. Password length at least 8 characters
username = user;
password = pw;
email = mail;
/* return success message */
let successMessage = "Account created successfully!";
return successMessage;
}
/* check whether user has been registered or not */
function isUserAvailable(){
if (typeof username !== "undefined") {
return true;
} else {
return false;
}
}
/* function for user login */
function doLogin(user, pw) {
if(isUserAvailable()) {
if(user === username && pw == password){
let successMessage = `Welcome again ${username}! Glad to have you back`;
return successMessage;
} else {
let errorMessage = "Sorry, your username and password doesn't match. Try again please.";
return errorMessage;
}
} else {
let errorMessage = "oops... it seems that you haven't registered your username yet.";
return errorMessage;
}
}
/* function to add user's bio */
function setBio(bio) {
if(isUserAvailable()) {
userBio = bio;
let successMessage = `Awesome ${username}! Your bio is created successfully.`;
return successMessage;
} else {
let errorMessage = "oops... it seemes that you haven't registered your username yet.";
return errorMessage;
}
}
/* function to display user's bio */
function showBio(){
if(isUserAvailable()) {
let userBioInfo = `${username}'s bio: ` + userBio;
return userBioInfo;
} else {
let notAvailableMessage = "oops... sorry. You haven't write a bio yet :)";
return notAvailableMessage;
}
}
/* function to display user's username */
function showDetails(){
if(isUserAvailable()) {
let userInfo = `Username is ${username}`;
if(typeof userBio !== 'undefined') {
userInfo = userInfo + `\n${showBio()}`;
}
return userInfo;
} else {
let notAvailableMessage = "oops... sorry. We couldn't find that user's info.";
return notAvailableMessage;
}
}
/* list of available APIs*/
var publicAPI = {
/* user registration */
signup:doRegister,
/* user login */
login: doLogin,
/* add user bio */
setBio:setBio,
/* display user's bio */
showBio:showBio,
/* display username */
details: showDetails
};
return publicAPI;
};
/* create first user instance */
let userLoginStatus, registerStatus, createBioStatus;
// user registration
var user1 = User();
registerStatus = user1.signup("John Doe", "password123", "[email protected]");
console.log(registerStatus);
console.log("\n");
// login
userLoginStatus = user1.login("John Doe", "12battery34"); // wrong login info
console.log(userLoginStatus);
console.log("\n");
userLoginStatus = user1.login("John Doe", "password123"); // valid login info
console.log(userLoginStatus);
console.log("\n");
// display user info
console.log(user1.details());
console.log("\n");
// write user's bio
createBioStatus = user1.setBio("John Doe is an experienced Javascript developers who also love Kpop");
console.log(createBioStatus);
console.log(user1.showBio());
console.log("\n");
console.log(user1.details());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment