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
| Name | Type | What it holds |
|---|---|---|
username | Text | What's typed in the username field |
password | Text | What's typed in the password field |
signingIn | True / False | On while the request is on its way |
signInError | Text | Why signing in failed, or empty |
session | Anything | The API's whole answer to signing in |
accessToken | Text | The token from that answer |
me | Anything | The 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.
#Steps
#Make the two screens
- In the Library's Screens tab, tap the first screen's name and call it
Sign in. - Tap Add screen. Call the new one
Profile. Behind the scenes it'sscreen2(in an app that had one screen), and that's the name step menus show. - Open the Sign in screen again.
#The sign-in form
- Add a Text Field. Set its Title to
Usernameand pickusernamein 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. - Add a Password Field. Set its Title to
Passwordand pickpasswordin its Text menu. It hides what's typed. - Add a Text for the error. Next to Content, tap ⚡︎ → App data →
signInError. Under Show & enable, set Show when tosignInError != '', and give it a red Color. 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.
- In Data & Logic, under Reusable actions, tap New action. Add these steps:
| Step | Settings |
|---|---|
| Set Value | State signingIn, To on |
| Set Value | State signInError, To left empty |
| Try | Its steps come next. |
| Set Value | State signingIn, To off |
- Open the Try. Under Do, add:
| Step | Settings |
|---|---|
| Call API | Url https://dummyjson.com/auth/login, Method POST, Body below, Into session |
| Set Value | State accessToken, To ⚡︎ → Formula… session.accessToken |
| Go to Screen | To Screen2 (the Profile screen) |
- For the Body, tap the { } button next to it (Use named fields, like a JSON body). A field called
field1appears. Rename itusernameand, with its ⚡︎, pick Formula…trim(username). Tap Add field → Text, rename the new fieldpassword, and pick ⚡︎ → App data →password. Under the Try's Catch, add Set Value: State
signInError, To ⚡︎ → Formula…:error.body.message ?? error.message- Tap Done, then tap the action's name,
newAction, and change it tosignIn.
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
- Select the Sign in button. In its Actions tab, tap Add action → Run Action and pick
signInunder Action. - Select the Password Field. In its Actions tab, tap the When card and choose When Return Is Pressed. Add Run Action with
signInhere too.
#Load the profile
- Open the Profile screen and tap the empty canvas around the device, so the Inspector shows the screen.
- 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 typeBearer {accessToken}as its value. - Into
me
- Url
- Add a Column. Under Show & enable, set its Show when to
me != null, so it waits for the profile. - 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}.
- A Web Image (under Images & Media). Next to Url, tap ⚡︎ → Formula… →
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 Intosession, sosession.accessTokenreads 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.messagesays what went wrong,error.statusis the status code anderror.bodyis the server's answer. DummyJSON puts its reason inmessage, soerror.body.message ?? error.messageshows 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
- Call API and Try
- Go to Screen
- Reusable actions and Run Action
- APIs and Secrets
- Triggers
- Screens
- Text Field, Password Field and Web Image
- Debugger: see each request and its answer.