PadCodedocs

Sign in with an API

Send a username and password to a web API, keep the token it returns, and use it to load a profile.

What you'll build: a sign-in screen, like the DummyJSON Sign In app. People type a username and password and tap Sign in. The app sends them to DummyJSON, a free test API, and keeps the token it sends back. If that works, a profile screen opens and loads the person's name and photo with the token. If it doesn't, the reason shows under the fields.

What you'll learn:

  • Send typed values to an API with Call API.
  • Catch a failed call with Try, and show why.
  • Keep part of the answer, like a token, and send it with the next call.
  • Open another screen with Go to Screen, and load data when it appears.

#The variables

NameTypeWhat it holds
usernameTextWhat's typed in the username field
passwordTextWhat's typed in the password field
signingInTrue / FalseOn while the request is on its way
signInErrorTextWhy signing in failed, or empty
sessionAnythingThe API's whole answer to signing in
accessTokenTextThe token from that answer
meAnythingThe signed-in person's profile

Make them in Data & Logic with Add variable, and rename each one. session and me are Anything because the API decides what's in them; they start empty.

Some of DummyJSON Sign In's variables in Data & Logic: accessToken and password are Text, catalog and me are Anything and start empty, and two loading flags are True / False.

#Steps

#Make the two screens

  1. In the Library's Screens tab, tap the first screen's name and call it Sign in.
  2. Tap Add screen. Call the new one Profile. Behind the scenes it's screen2 (in an app that had one screen), and that's the name step menus show.
  3. Open the Sign in screen again.

#The sign-in form

  1. Add a Text Field. Set its Title to Username and pick username in its Text menu. So the keyboard doesn't change what's typed, add two modifiers under All modifiers: Auto Capitals with Autocapitalization set to Never, and Autocorrect Off.
  2. Add a Password Field. Set its Title to Password and pick password in its Text menu. It hides what's typed.
  3. Add a Text for the error. Next to Content, tap ⚡︎ → App data → signInError. Under Show & enable, set Show when to signInError != '', and give it a red Color.
  4. Add a Button. Set its Title to:

    {signingIn ? 'Signing in…' : 'Sign in'}

#The sign-in steps

The button and the password field's Return key both sign in, so the steps go in a reusable action.

  1. In Data & Logic, under Reusable actions, tap New action. Add these steps:
StepSettings
Set ValueState signingIn, To on
Set ValueState signInError, To left empty
TryIts steps come next.
Set ValueState signingIn, To off
  1. Open the Try. Under Do, add:
StepSettings
Call APIUrl https://dummyjson.com/auth/login, Method POST, Body below, Into session
Set ValueState accessToken, To ⚡︎ → Formula… session.accessToken
Go to ScreenTo Screen2 (the Profile screen)
  1. For the Body, tap the { } button next to it (Use named fields, like a JSON body). A field called field1 appears. Rename it username and, with its ⚡︎, pick Formula… trim(username). Tap Add field → Text, rename the new field password, and pick ⚡︎ → App data → password.
  2. Under the Try's Catch, add Set Value: State signInError, To ⚡︎ → Formula…:

    error.body.message ?? error.message

  3. Tap Done, then tap the action's name, newAction, and change it to signIn.
The signIn action: Set Value signingIn to On, Set Value signInError, a Try, and Set Value signingIn to Off.
The Try open: under Do, Call API posts to dummyjson.com/auth/login, then Set Value steps keep the tokens and the time, and Reset Value clears me.

DummyJSON Sign In's Try does a little more than these steps: it also keeps a refresh token and the time of sign-in, and clears what an earlier sign-in loaded.

#Run it from the button and the Return key

  1. Select the Sign in button. In its Actions tab, tap Add action → Run Action and pick signIn under Action.
  2. Select the Password Field. In its Actions tab, tap the When card and choose When Return Is Pressed. Add Run Action with signIn here too.

#Load the profile

  1. Open the Profile screen and tap the empty canvas around the device, so the Inspector shows the screen.
  2. In the Actions tab, tap the When card and choose When Loading. Add Call API:
    • Url https://dummyjson.com/auth/me
    • Headers: tap Add field, name it Authorization, and type Bearer {accessToken} as its value.
    • Into me
  3. Add a Column. Under Show & enable, set its Show when to me != null, so it waits for the profile.
  4. Inside the Column, add:
    • A Web Image (under Images & Media). Next to Url, tap ⚡︎ → Formula… → me.image.
    • A Text with {me.firstName} {me.lastName}.
    • A Text with {me.email}.

Tap Preview, sign in with the test account, and the profile opens. Go back, change the password and try again: the error shows under the fields.

#How it works

  • Call API sends the request and waits for the answer before the next step runs. Its Body fields become JSON: {"username": "emilys", "password": "emilyspass"}. The answer is saved Into session, so session.accessToken reads one part of it.
  • Try: if the server answers with an error, Call API fails, the rest of Do is skipped and Catch runs. Inside Catch, error.message says what went wrong, error.status is the status code and error.body is the server's answer. DummyJSON puts its reason in message, so error.body.message ?? error.message shows the server's words when there are some.
  • signingIn is on only while the steps run: the last Set Value runs after the Try either way, because Try catches the failure instead of stopping.
  • Go to Screen is the last step in Do, so the profile opens only when signing in worked.
  • When Loading runs when the screen appears. The header Bearer {accessToken} sends the token, which is how DummyJSON knows who's asking.

#See also