#[SOLVED] Fetching from storage return 401 unauthorize with fetch

1 messages · Page 1 of 1 (latest)

polar harness
#

hello !

i'm trying to play a video stored on a storage bucket, where the bucket has the read permissions only for user with the premium label. I'm authenticated everything is working, i see the labels using account.get().

const url = storage.getFileView("videos", file_id); this call is working, i can get the url with url.href but when fetching it i get a 401 unauthorized.
Looking at the request (see attachment), no cookies or header to authenticate are sent, so it's normal that i get a 401.

everything was working before the cloud upgrade to v1.15, and i'm using createemailpasswordsession to auth.

how can i fetch the video file ?

thanks !

hallow topaz
#

How do you fetch it? manually?

#

If so, because you're on localhost you won't get the cookies

polar harness
polar harness
hallow topaz
#

Try using the SDK

polar harness
#

it was working before the upgrade of the cloud version tho

polar harness
hallow topaz
#

Can you share your code?

polar harness
#

i'm using the nuxt-appwrite module. everything is working well, i can call private functions, account.get and such.
const url = storage.getFileView("videos", file_id); and then i'm using url.href as a src for a video html attribute, the same thing as a fetch, i'm looking at the network tab anyway.

#

i guess one way of doing it fetch with a special header or cookie ?

#

well it's working now no on a different computer

#

wtf

#

maybe corruped data from my laptop cookies something like that

hallow topaz
#

At least its working now

polar harness
#

yes i'll see if it work on laptop

#

have a good day !

#

[SOLVED] Fetching a storage document that have permissions restrictions

#

update, the firefox session on my laptop doesn't work.

#

but a private tab work

hallow topaz
#

Try to clear the storage for the website

polar harness
#

i tried deleteing all data on firefox but still doesn't work on the instance

#

yep done that

#

from 3 different places lol

#

the only thing left is nuke the firefox profile

hallow topaz
#

Oh yes

polar harness
#

i'll try that last hope

hallow topaz
#

One sec

polar harness
#

but weird

#

like nothing was stored no cookies no pref and it still didn't work

hallow topaz
#

Go to Firefox settings

#

And update the Cookies settings

polar harness
#

whole afternoon on this, sometime i hate software lol

hallow topaz
#

And try again

#

This is again, because you're using localhost
When you are in production, make sure your app and Appwrite share the same apex domain
You'll need to add a custom domain to your Appwrite project.

polar harness
polar harness
#

if i don't find a fix for my laptop instance i'll nuke the firefox profile, thanks for the infos !

hallow topaz
#

Is it like this?

#

Change it to be like this
And reload all tabs to test

#

Make sure to set it back to standard after as is it not safe at all

polar harness
#

hey @hallow topaz sorry to bump you, there is no way to test fetch a bucket behind permissions without hosting it on a owned domain (localhost) ?

hallow topaz
polar harness
#

i want to fetch (download the blob) of a document on a bucket that needs special permissions.
how can i test my code etc without having a custom domain for my website and appwrite ?

#

for now i don't have a domain as i'm building the website

hallow topaz
#

So add a dummy domain in your /etc/hosts file and point it to 127.0.0.1

#

And add that domain in the console

polar harness
#

ohh nice thank you

#

127.0.0.1 inspire
added inspire as a hostname in integrations > platform

#

is there something else to do ?

polar harness
hallow topaz
#

Yes

#

You'll need to add two domains

#
  1. For the testing like you've done
  2. Another subdomain for Appwrite itself
polar harness
#

oh also appwrite.inspire on etc hosts ?

#

and use it as appwrite api url on my project

#

thanks !

hallow topaz
#

Make sure both domains are of the same domain so

127.0.0.1 inspire.com
127.0.0.1 appwrite.inspire.com
polar harness
#

cors fail

polar harness
#

in network tab http://appwrite.inspire.com/account for example fails

#

my app is on http://inspire.com:3000/

#

same domain

#

the port doesn't matter i think

#

tried with https and tried with just inspire instaed of .com

hallow topaz
#

So, now it works?

polar harness
#

no

hallow topaz
#

Where is your appwrite hosted?

polar harness
#

cloud

#

tried 127.0.0.1 inspire.com cloud.appwrite.io appwrite.inspire.com

hallow topaz
#

Ohhh
So create domain only for your localhost

127.0.0.1 test11111.appwrite.com

and add it as a pltform

polar harness
#

i have a * in platform

#

127.0.0.1 inspire.appwrite.io in hosts

#

using https://cloud.appwrite.io/v1 as appwriten endpoint

#

don't work

hallow topaz
#

And now try in incognito

polar harness
#

cors works tho

hallow topaz
#

You might have some cache

polar harness
#

nop same with incognito, but maybe it's because the api call are https and my dev server http?

#

just to be on the same page, the problem is back to fetch not sending cookies or a auth header to the appwrite endpoint

#

update: no still doesn't work with both on https (tho the dev server don't have the same certificate)

hallow topaz
#

Alright,
Let me check something

#

What is the error you're getting now?

polar harness
#

while the call made with the appwrite sdk all works

#

and are on the same domain

#

btw if using the result.href on a video tag it works, but not with fetch

#

oh fuck me

hallow topaz
#

Oh, I forgot you're doing it with fetch

#

Is that solved thte problem

polar harness
#

i thought that would be it but no

#

still no cookies

#

await fetch(url, { credentials: "same-origin" }) where url is result.href

#

from storage.getfileview

#

IT WORKS

#

with include

#

await fetch(url, { credentials: "include" })

#

honestly i'm quite sad about all of this 😂

#

thanks a lot for all the help !

#

[SOLVED] Fetching from storage return 401 unauthorize with fetch

hallow topaz
#

Great

polar harness
#

it even works on localhost

#

with everything by default without custom hosts

hallow topaz
#

Great
It means that it was able to save the cookies

polar harness
#

yeah the only problem was fetch 🥹

#

i hate js why would that be a good default behaviour for fetch

hallow topaz