wry
wry copied to clipboard
want an API can get webview cookie
Is your feature request related to a problem? Please describe. I want use wry load a remote url, wait user login, and get cookie to use user cookie to call thrid-service api, so I need get cookie on user login sucesss.
Describe the solution you'd like
I use electron session to finish it, the sesion can give all webview cookies, cache, proxy setting... but I can't found tauri have similar API
Describe alternatives you've considered I found wry can preload script , I try init script and send event to main process, but the remote url will run 302 redict and callback , my init script will loss.
Would you assign yourself to implement this feature?
- [ x ] No
Additional context electron session : https://www.electronjs.org/docs/latest/api/session
related to https://github.com/tauri-apps/wry/issues/444.
I think you can access non secure cookie from HTTP request by using custom protocol but can't get secure cookie. Because wry is not supporting HTTPS request yet. See related issue for more details.
Do we need to implement feature to access webview cookie even if secure cookie can not be handled?
This is useful.
I need this one as well.
I need this one as well.
please just tick the thumbs up button at the top instead of adding "+1" comments that will notify all users subscribed to this issue!
I need like this https://developer.chrome.com/docs/extensions/reference/cookies/#method-getAll
Hello :). Hey is this still open? I have exactly the same problem. We have an authentication server that renders a login mask. The client will open this webpage for login (tauri opens the external webpage and renders it). As soon as the user logins it set's a private cookie that can not be obtained by javascript and redirect to the webpages from tauri (localhost).
In another electron app we are using it already and it is quite simple to get the cookie then in the backend and handle all secure relevant authentication functions inside the electron backend. I want to implement the same in tauri. How can I achieve it if I have no possibility to obtain the cookie itself?
for example in electron I can obtain the cookies like this:
Thx in advance <3
For those who absolutely want to read cookies from a window's browsing context, you can still do the following:
let window = WindowBuilder::new(
app, // your AppHandle
"some-window",
WindowUrl::External("https://somewhere.com".parse().unwrap()),
)
.data_directory("/home/me/session-data") // pass custom path here
.build()
.expect("could not create window");
let cookies = std::fs::read_to_string("/home/me/session-data/cookies").expect("could not read cookies");
println!("{cookies}");
This code would display something like that:
somewhere.com FALSE / FALSE 1722097323 COOKIE_1 COOKIE_VALUE None
somewhere.com FALSE / FALSE 1722097323 COOKIE_2 COOKIE_VALUE None
somewhere.com FALSE / FALSE 1722097323 COOKIE_3 COOKIE_VALUE None
... etc
I don't know what all the fields are but at least you can easily retrieve the name and value for each cookie.
It's a terrible solution but it works for me !
I believe the feature should really be available, just like in Electron.
I would really love this feature as well. Love the tauri "ecosystem" btw thanks for everything.
Is there any progress on this issue?
Is there any progress on this issue?
Not on our side at least, meaning no maintainer is actively working on it. No idea if there's a community member working on it but at least nobody reached out to us :)
For those who absolutely want to read cookies from a window's browsing context, you can still do the following:
let window = WindowBuilder::new( app, // your AppHandle "some-window", WindowUrl::External("https://somewhere.com".parse().unwrap()), ) .data_directory("/home/me/session-data") // pass custom path here .build() .expect("could not create window"); let cookies = std::fs::read_to_string("/home/me/session-data/cookies").expect("could not read cookies"); println!("{cookies}");
This code would display something like that:
somewhere.com FALSE / FALSE 1722097323 COOKIE_1 COOKIE_VALUE None somewhere.com FALSE / FALSE 1722097323 COOKIE_2 COOKIE_VALUE None somewhere.com FALSE / FALSE 1722097323 COOKIE_3 COOKIE_VALUE None ... etc
I don't know what all the fields are but at least you can easily retrieve the name and value for each cookie.
It's a terrible solution but it works for me !
I believe the feature should really be available, just like in Electron.
It's quite strange, the contents of my data folder are empty.
For those who absolutely want to read cookies from a window's browsing context, you can still do the following:
let window = WindowBuilder::new( app, // your AppHandle "some-window", WindowUrl::External("https://somewhere.com".parse().unwrap()), ) .data_directory("/home/me/session-data") // pass custom path here .build() .expect("could not create window"); let cookies = std::fs::read_to_string("/home/me/session-data/cookies").expect("could not read cookies"); println!("{cookies}");
This code would display something like that:
somewhere.com FALSE / FALSE 1722097323 COOKIE_1 COOKIE_VALUE None somewhere.com FALSE / FALSE 1722097323 COOKIE_2 COOKIE_VALUE None somewhere.com FALSE / FALSE 1722097323 COOKIE_3 COOKIE_VALUE None ... etc
I don't know what all the fields are but at least you can easily retrieve the name and value for each cookie. It's a terrible solution but it works for me ! I believe the feature should really be available, just like in Electron.
It's quite strange, the contents of my data folder are empty.
+1
I tried to do the same thing but the data folder never gets populated.
Is there any progress on the issue? I want to fix my insecure workarounds on our auth server that I had to do in order to make it working with tauri. It really would make me sad to go back to electron for our applications...😢.
For those who absolutely want to read cookies from a window's browsing context, you can still do the following:
let window = WindowBuilder::new( app, // your AppHandle "some-window", WindowUrl::External("https://somewhere.com".parse().unwrap()), ) .data_directory("/home/me/session-data") // pass custom path here .build() .expect("could not create window"); let cookies = std::fs::read_to_string("/home/me/session-data/cookies").expect("could not read cookies"); println!("{cookies}");
This code would display something like that:
somewhere.com FALSE / FALSE 1722097323 COOKIE_1 COOKIE_VALUE None somewhere.com FALSE / FALSE 1722097323 COOKIE_2 COOKIE_VALUE None somewhere.com FALSE / FALSE 1722097323 COOKIE_3 COOKIE_VALUE None ... etc
I don't know what all the fields are but at least you can easily retrieve the name and value for each cookie. It's a terrible solution but it works for me ! I believe the feature should really be available, just like in Electron.
It's quite strange, the contents of my data folder are empty.
Me neither. I also tried to find the default path, since It would also be okay for me if I can use the default window from tauri.config.json. But I was not able to find where the default data_directory is. I used
const appDataDirPath = await appDataDir();
console.log("appDataDirPath", appDataDirPath);
on frontend side and it tells me a path that simply does not exist:
/Users/XXX/Library/Application Support/XXX-app.xyz/
I really don't get it. In another blog post I read that tauri does not preserve cookies for localhost domain when chrom or safari is used as web engine.
https://github.com/tauri-apps/tauri/issues/2490
So is there really no workaround available for this issue?
I asked a half year ago and others even earlier, is there no solution to this issue?
Besides: I had tried the solution proposed by Hakhenaton. But the code gave me compilation errors. I needed to use a pathBuf object. str is not compatible with data_directory() method. Here is my adapted rust code:
tauri::Builder::default()
.setup(|app| {
let path_str = "/Users/xxx/session_data/cookies";
let data_directory: PathBuf = PathBuf::from(path_str);
let path_string: String = data_directory.to_string_lossy().to_string();
let window = tauri::WindowBuilder::new(app, "label", tauri::WindowUrl::App("index.html".into()))
.data_directory(data_directory)
.build()?;
// let cookies = std::fs::read_to_string(path_string).expect("could not read cookies");
// println!("{cookies}");
Ok(())
})
.manage(...)
The command for cookie reading always fails (I assume since the file does not exist, the cookie data does not get populated as yetone and prabhupant stateted), so I commented it out in my tests.
Where are the cookies stored and how do I access them? If I know this, I can write my own get cookie handler on rust side.
The only way for a user to sign in to my app is through a remote URL which I do not control. Being able to extract cookies from the webview is the only way I could ever port my Electron app to Tauri.
It's somtimes possible to grab cookies indirectly by intercepting WebView network requests (Set-Cookie
), but there seems to be no readily available implementation.
@Zihan-Hu Do you think it would be hard to implement cross-platform reliably?
@Zihan-Hu Do you think it would be hard to implement cross-platform reliably?
Sometimes it feels like herding cats and sometimes very easy 🤔 In my opinion, it depends on the tech stack you're using and your target user base.
Getting cookies by reading Chrome's cookie database on your computer. https://github.com/lei4519/extract-chrome-cookies