android - How to clear all session cookies in an Ionic app on Android?
问题描述
I'm having a weird problem in my Ionic 5 React app on Android. The app logs in to a Drupal 8 backend and gets a JWT token for authentication.
However, after making some recent changes to my app and Drupal, the app is now getting a session cookie as well (the Drupal session cookie). This would normally be fine, but when I log out of the app, even though I see the session cookie get deleted from Cookies: http://localhost in Chrome on my dev machine, when I try to log back in on the app, the app is using the Drupal session cookie, which results in an error: {"message":"This route can only be accessed by anonymous users."}
.
Obviously, I should not be trying to log in when I am already logged in.
But my question is-- where is this "ghost" cookie? After logging in on the app, in the Chrome Dev Tools, I can see the session cookie in Application-> Cookies -> http://localhost, but when I log out in the app, this cookie is deleted. I checked all the other options under storage and there are no cookies there, either.
Even if I close the app and re-open it, when I try to log in, it is using this session cookie, but I can't figure out where that session cookie is getting stored (it's not under Cookies or Local Storage).
I can "reset" everything by uninstalling the app and then reinstalling it. This will allow me to log in once, and I get a session cookie (which appears in Application -> Cookies-> http://localhost). But when I log out of the app and try to log back in again, I see that axios somehow still has my session cookie (which has been cleared from Chrome Dev Tools upon logout) and so log in fails This route can only be accessed by anonymous users
).
How can I clear this phantom cookie, or how can I at least see where the cookie is?
Here's the login code for my Ionic app:
export const login = async (email: string, password: string): Promise<void> => (
axios.post(`${baseUrl}/user/login?_format=json`, {
mail: email,
pass: password,
}, {
withCredentials: true,
}).then((response) => {
if (response.data.access_token) {
storageSet(jwtTokenName, response.data.access_token);
} else {
Promise.reject(Error('Could not fetch JWT token.'));
}
}));
Update
Ionic hosts the app from http://localhost
. In the Chrome dev tools on my desktop, under Application -> Cookies, normally I only see the cookies for http://localhost
. However, if I navigate to a page in my app that has an iframe that embeds a page from my Drupal site, I then see the session cookie for the Drupal site! So at least I found the phantom cookie.
So now my question is, how do I delete the session cookie, or even better, how do I prevent the session cookie from being saved in the first place?
解决方案
如果我理解你,你只需要清除所有 cookie
在 Android 的 Ionic 项目中,我们使用了cordova-plugin-cookiemaster
角的例子:
import {Injectable} from '@angular/core';
declare var cookieMaster;
@Injectable({
providedIn: 'root'
})
export class AuthService {
public removeUser(): void {
cookieMaster.clearCookies(() => {}, () => {
alert('Error');
});
}
}
推荐阅读
- python - Pytorch - 推断线性层 in_features
- oracle - 进一步处理给定 dbms_output 中返回的数据
- css - 我的布局有问题
- r - 增加图例键之间的空间并在 ggplot 中对齐它们
- go - 在函数内部定义函数
- python - 如何在python的新列中将id拆分为多个值
- javascript - 有没有办法在 es6 过滤器或 lodash 中获得相同的结果?
- tensorflow - 如何在keras中查看层输出的张量值
- kubernetes - 如何使用 Kubernetes Go 客户端调用 Pod 代理动词?
- c# - 尝试设置 GridViewColumn 宽度时出现异常