Back

[SOLVED] Getting 401 Error from Cloud When Attempting to Preview Images From Cloud

  • 0
  • Web
aaronksaunders
1 May, 2023, 19:24

Only is an issue on IOS

This is an Ionic Capacitor App and I believe the issue is that the API call is being made from the device. The same code works correctly when running from web/local host.

Everything else works fine, the images are uploaded properly, I can authenticate and retrieve documents, it is just when using the preview URL that is returned I get the 401 Error

Is this a configuration issue? Do I need to set the hostnames value in the project configuration?

I am currently out of ideas, Thanks In Advance

TL;DR
Issue: Getting a 401 Error when attempting to preview images from the cloud, specifically on iOS devices. The code works fine on web/local host. Solution: The issue seems to be with cookies not being sent in the network request for the image. Two possible solutions are suggested: 1. Grant any user read access to the bucket. 2. Generate a JWT token and include it in the headers of the network request. Additional Details: - There is a discussion on GitHub about capacitor iOS cookies that may provide more insights. - The SDK for Web uses cookies for session management, but some runtime environments may cause problems. - The issue is only
Drake
1 May, 2023, 19:47

the SDK for Web uses cookies for session management. Typically, the cookie is sent automatically, but in some cases 3rd party cookies or some other runtime environments can cause problems.

In those cases, you might need to generate a JWT token and include that in the headers of the network request

aaronksaunders
1 May, 2023, 19:48

that is interesting because it works fine with Android...

Drake
1 May, 2023, 19:48

a quick search on "capacitor ios cookies" gave: https://github.com/ionic-team/capacitor/discussions/3537

aaronksaunders
1 May, 2023, 19:48

since i ma using the javascript API, I would need to generate the toke and pass it with the URL that was returned from the getPreview call?

aaronksaunders
1 May, 2023, 19:49

i read all that already, but that did seemed to be discussing auth

Drake
1 May, 2023, 19:49

yes...this is an auth problem...

aaronksaunders
1 May, 2023, 19:49

the rest of the application works fine... it is only when i try to rended the preview url

Drake
1 May, 2023, 19:51

are you rendering any other images using storage.getFileView() or storage.getFileDownload()?

aaronksaunders
1 May, 2023, 19:51

i was about to switch to the FileDownload call to see if i get different results

Drake
1 May, 2023, 19:52

go ahead. You'll probably get the same error. Then, you can try to grant any user read access to your bucket and try again

aaronksaunders
1 May, 2023, 19:52

i believe it is the client hostname is getting rejected by the server when i request using the url

Drake
1 May, 2023, 19:53

we don't return a 401 for origin validation

Drake
1 May, 2023, 19:53

Please try with bucket read permissions set to any

aaronksaunders
1 May, 2023, 19:54

setting it to any works

Drake
1 May, 2023, 19:55

right so it's an access problem because the cookie is not being sent in the network request for the image

aaronksaunders
1 May, 2023, 19:55

hmm.... i would like to find a better solution

Drake
1 May, 2023, 19:56

if you want to go down the cookie route, you'll need to find out what's wrong with capacitor and the cookies. otherwise, you can try to see how to pass a JWT token in the header when fetching the image...although im not sure how to do that using capacitor

aaronksaunders
1 May, 2023, 19:56

i am passing the url to an image tag... not sure how i would be able to provide a token?

aaronksaunders
1 May, 2023, 19:59

thanks

Drake
1 May, 2023, 20:00

right...maybe capacitor exposes some special way to pass headers? πŸ€·πŸΌβ€β™‚οΈ

ideally, if you get cookies to work, it would work automatically

aaronksaunders
1 May, 2023, 20:29

Leaving this here for anyone else who comes across this problem. I solved it by modifying my capacitor.config.ts

TypeScript
import { CapacitorConfig } from "@capacitor/cli";

const config: CapacitorConfig = {
  appId: "io.ionic.starter",
  appName: "appwrite_1",
  webDir: "dist",
  bundledWebRuntime: false,
  plugins: {
    CapacitorCookies: {
      enabled: true,
    },
  },
};

export default config;

I added the plugins configuration

aaronksaunders
2 May, 2023, 18:24

here is a video tutorial on the first part of the project , source code is linked in description - https://youtu.be/3d71O1zvlaw

aaronksaunders
2 May, 2023, 20:54

[SOLVED] Getting 401 Error from Cloud When Attempting to Preview Images From Cloud

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more