Building “Log in & Log out” Features on Appsheet

firdauswn
4 min readOct 29, 2022

--

When you’re creating your own app, in certain point, there is a need to make a limitation of users accessing the app. The example is, a same app is used by the students and the teachers. We want the students can access their datas only, not the school’s archive data, but the teachers can access both student’s data and school’s archive data. Then, “Log in & Log out” features are needed to;

  1. Control the access,
  2. Separate accessing level of the users.
Pict 1. Users Login

Let’s imagine we have 2 users (Pict 1): andy & bale. They act as a teacher and a student. The teacher (andy) can access two menus: Student Data & School Archive, while the student (bale) is given a student data access only. We mention this separation as a ‘Level’.

At first, add new table from Pict 1 sheet. Then, go to User Settings and fill the column as displayed in Pict 2. We need to edit row number 2 until row number 5.

Pict 2. User Settings

Now, we have to edit the information inside them

Username

Click edit, in Data Validity,

In Valid If -> add codes like this:

AND(
ISNOTBLANK([username]),
IN([_thisrow_after].[username],Root[userid]),
ISNOTBLANK([password])
)

in Invalid Value Error -> add codes like this:

IFS(
NOT(IN([_thisrow_after].[username],root[userid])),”username not found”,
isblank([password]),” “
)

Those codes mean: The statement will valid if the username is not blank, the username in this row has the same value with values in Userid column (look at Pict. 1) and the password is not blank.

And, if the password filled is not the same as the value in Userid column, will return “username not found”, otherwise return “ “.

Password

Click Edit, Data Validity,

In Valid If -> add codes like this:

[password]=lookup([username],”Root”,”Userid”,”Pass”)

in Invalid Value Error -> add codes like this:

“Wrong Password”

in Require? -> add codes like this:

IN([_thisrow_after].[username],Root[Userid])

This means, in Password settings -> checking the condition if our input in “Password” is true including one of the values from table “Pass” in Root Sheets, otherwise it will return “Wrong Password”. And we give one required condition that the username must be on the list of Userid column in Root.

Name

Click Formula Column -> add codes like this:

lookup([username],”Root”,”Userid”,”Name”)

This means, when condition is true (username has the same value on the list of Userid), it will show the value of Name column from Root.

Level

Click Formula Column -> add codes like this:

lookup([username],”Root”,”Userid”,”Level”)

This means, when condition is true (username has the same value on the list of Userid), it will show the value of Level column from Root.

Now, let’s check on settings (click top-left three lines). That would bring you to login menu (username and password). You can log in by filling the data from Pict 1. But, if we want to log out (click settings), username and password data is still there. It’s much better to reset the data whenever we log in or log out. Go to Username, choose Update behavior and checklist on the “Reset on Edit”. Do the same for Password.

Then, we need to change name Settings into Log in & Log out. After that, the final task is to create the level login (differentiate between teacher and student login).

Login Logout

Go to App (side-left menu), choose Views and find User Settings -> Setting. It is displayed in Pict 3.

Pict 3. Log in Log out Settings

In display name, add codes:

if(isnotblank(usersettings(level)),”Log out”, “Log in”)

This means if value of level in usersettings is not blank then it will return Log out, otherwise it shows Log in.

And, to make button (below Password) to Log in or Log out instead of Save or Cancel we have to create the settings. As shown on Pict 4. Go to settings, choose Views -> Localization.

Pict 4. Log in Log out Buttons

In Save, add codes:

If(and(context(“view”)=”settings”,isnotblank(usersettings(username))), “Log out”,
if(and(context(“view”)=”settings”,isblank(usersettings(username))), “Log in”,
“save”))

In Cancel, add codes:

If(and(context(“view”)=”settings”,isnotblank(usersettings(username))), “ “,
if(and(context(“view”)=”settings”,isblank(usersettings(username))), “ “,
“Cancel”))

Slice

The last one is, create Slice. What Slice is used for in this tutorial? It is used for making a level of users. Users with student level can access the student’s data only, but Users with teacher level can access both student’s data and school’s archive, like we see in Pict 1.

Pict 5. Log in Slice

Go to Data (side-left menu), choose slices, gives name in Slice Name from Root table. In Row Filter condition, add codes:

and([userid]=usersettings(username),[pass]=usersettings(password),[level]=usersettings(level))

This means, what menu is shown after login, is depended on 3 conditions, username value is on the userid column, password is on the pass column, and level is on the level column.

Congratulation! Your Log in & log out features are ready to use.

--

--