Skip to content

How to authenticate API calls and use conditional logic to obtain the token only when required? #2316

Discussion options

You must be logged in to vote

To solve it, you'll need a conditional statement to Check and an expectation that the token is not already present.

Let's assume that the structure of your notes() is defined as follows:

interface MyNotes {
  authToken: string;
  // ... other notes
}

And that the authentication API returns a response like this:

interface AuthResponse {
  token: string;
}

You can create a task to fetch and remember the auth token like so:

import { Task, notes } from '@serenity-js/core'
import { Send, GetRequest, LastResponse } from '@serenity-js/assertions'

const ObtainAuthToken = () =>
  Task.where(`#actor obtains the authentication token`,
    Send.a(GetRequest.to('https://login.mycompany.com/')),
    n…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by jan-molak
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants