ignite icon indicating copy to clipboard operation
ignite copied to clipboard

Async Api doesn't resolve when i'm trying to add Authorization Header with token from AsyncStorage

Open momolafrooo opened this issue 3 years ago • 1 comments

Hello Guys, i'm new on ignite. I'm trying to make an api call with laravel sanctum as authentication. But i have an issue with api sauce. The async call doesn't resolve when i try to add authorization to the header. Can someone help me ?

Here is the code

  await authApi.setAuthorization()
    const result = await authApi.getCurrentUser()

    async setAuthorization() {
      const token = await loadString("token")
      this.api.apisauce.setHeader("Authorization", `Bearer ${token}`)
      return token
    }

    async getCurrentUser(): Promise<GetCurrentUserResult> {
    try {
      // make the api call
      // const token = await this.setAuthorization()
      const response: ApiResponse<any> = await this.api.apisauce.get("/current-user")

      // the typical ways to die when calling an api
      if (!response.ok) {
        const problem = getGeneralApiProblem(response)
        console.tron.log(response)
        if (problem) return { ...problem, messages: response.data }
      }

      const currentUser = response.data

      return { kind: "ok", currentUser }
    } catch (e) {
      __DEV__ && console.tron.log(e.message)
      return { kind: "bad-data" }
    }
  }

momolafrooo avatar Aug 10 '21 11:08 momolafrooo

Quick tip, @momolafrooo, if you add three backticks and then tsx to your code block, it'll syntax highlight the code.

Like this:

(three backticks)tsx

(code here)

(three backticks)

jamonholmgren avatar Aug 10 '21 16:08 jamonholmgren

You should make sure that your token is actually set when you try to call it.

I'm going to close this issue since it's more of a question instead of an issue with Ignite. If you are still having issues, please say hi in our community slack! I'm sure you'll be able to get some good help from someone at Infinite Red or a community member :)

joshuayoes avatar Oct 04 '22 17:10 joshuayoes