Authentication 离子2注销时清除应用程序缓存
我的登录功能由一个http请求(一个与检查无关的请求)和输入的凭据组成。通过这种方式,我可以通过不将下一个组件与NavController一起推到堆栈上来解决请求或得到拒绝 注销时,保存在Ionic存储器中的凭据将被删除。现在开始出现问题:现在保存在存储中的任何凭据似乎都不会被登录请求使用,因为该请求不会引发身份验证异常。只有在清除浏览器缓存后,它才能再次工作 这一切都是通过在网络浏览器和手机上提供应用程序实现的 如何使用Ionic 2清除应用程序缓存(不仅仅是视图/组件缓存)以防止这种行为?目前没有关于这个问题的文件或问题 身份验证服务:Authentication 离子2注销时清除应用程序缓存,authentication,caching,ionic-framework,ionic2,Authentication,Caching,Ionic Framework,Ionic2,我的登录功能由一个http请求(一个与检查无关的请求)和输入的凭据组成。通过这种方式,我可以通过不将下一个组件与NavController一起推到堆栈上来解决请求或得到拒绝 注销时,保存在Ionic存储器中的凭据将被删除。现在开始出现问题:现在保存在存储中的任何凭据似乎都不会被登录请求使用,因为该请求不会引发身份验证异常。只有在清除浏览器缓存后,它才能再次工作 这一切都是通过在网络浏览器和手机上提供应用程序实现的 如何使用Ionic 2清除应用程序缓存(不仅仅是视图/组件缓存)以防止这种行为?目
@Injectable()
export class AuthService {
HAS_LOGGED_IN = 'hasLoggedIn';
constructor(private storage: Storage) {
// this.rest = rest;
console.log('auth');
}
setCredentials(credentials) {
this.storage.set('username', credentials.username);
this.storage.set('password', credentials.password);
}
logout(): void {
this.storage.remove('username');
this.storage.remove('password');
this.storage.remove(this.HAS_LOGGED_IN);
}
hasLoggedIn() {
return this.storage.get(this.HAS_LOGGED_IN).then( value => {
console.log('hasLoggedIN value: ' + value);
return value === true;
});
}
}
登录组件:
@Component({
selector: 'page-login',
templateUrl: 'login.html',
})
export class LoginPage {
model: any;
HAS_LOGGED_IN: string = 'hasLoggedIn';
constructor(private navCtrl: NavController,
private viewCtrl: ViewController,
private auth: AuthService,
private toastCtrl: ToastController,
private rest: RestService,
private storage: Storage) {
}
ionViewDidLoad() {
this.model = {};
}
ionViewWillEnter() {
this.viewCtrl.showBackButton(false);
this.displayTab(false);
}
login() {
console.log(this.model);
console.log('login() claled');
this.displayTab(true);
this.auth.setCredentials(this.model);
this.rest.getEntryPoint().then(data => {
console.log(data);
this.storage.set(this.HAS_LOGGED_IN, true);
this.navCtrl.push(OverviewPage);
}).catch(err => {
this.storage.set(this.HAS_LOGGED_IN, false);
console.log('Error:');
console.log(err);
this.navCtrl.push(LoginPage).then(response => {
console.log(response);
console.log(this.navCtrl);
console.log('pushed login 1');
});
});
}
validate(items: boolean) {
if (items) {
let toast = this.toastCtrl.create({
message: 'Passwort und Benutzername sind zwingend',
duration: 3000,
position: 'bottom',
});
toast.onDidDismiss(() => {
console.log('Dismissed toast');
});
toast.present();
}
}
private displayTab(display: boolean) {
let elements = document.querySelectorAll('.tabbar');
if (elements != null) {
Object.keys(elements).map((key) => {
elements[key].style.transform = display ? 'translateY(0)' : 'translateY(70px)';
});
}
}
}
拦截Http方法(此服务用于另一个进行实际Rest调用的服务):
@Injectable()
导出类HttpInterceptorService{
构造函数(@Inject(Http)private Http:Http,private storage:storage){
this.http=http;
console.log('interceptor');
}
获取(url){
返回新承诺((解决、拒绝)=>{
let headers=新的headers();
这个.createAuthorizationHeader(headers)。然后(()=>{
返回此.http.get(url{
标题:标题,
}).订阅(数据=>{
解析(data.json());
},err=>{
拒绝(错误);
});
});
});
}
put(url:字符串,属性?){
返回新承诺((解决、拒绝)=>{
let headers=新的headers();
这个.createAuthorizationHeader(headers)。然后(()=>{
返回this.http.put(url,(attributes)?attributes:{}{
标题:标题,
}).订阅(数据=>{
解析(data.json());
},err=>{
拒绝(错误);
});
});
});
}
post(url:字符串、数据){
返回新承诺((解决、拒绝)=>{
let headers=新的headers();
这个.createAuthorizationHeader(headers)。然后(()=>{
返回此.http.post(url、数据、{
标题:标题,
}).subscribe(输出=>{
解析(output.json());
},err=>{
拒绝(错误);
});
});
});
}
私有createAuthorizationHeader(头:头):承诺{
log('creating auth header');
返回新承诺(解决=>{
this.storage.get('username'))
。然后(用户名=>{
this.storage.get('密码')
。然后(密码=>{
headers.append('Authorization','Basic'+
btoa(用户名+':'+密码));
解决();
});
});
});
}
}
其他组件正在使用
hasLoggedIn()
函数检查我们在切换回应用程序时是否登录。如果不再登录(以任何方式清理存储),我们将返回LoginPage组件。问题在于响应中的会话头,而我之前不知何故没有看到
我的解决方案是简单的Cookies清除:
window.cookies.clear(function() {});
多亏这个人否决了我的问题,但没有留下任何信息说明是什么让他/她这么做的。你能给我们提供一些代码吗?可能是您的本地存储incorrectly@LeRoy希望这能帮上点忙:)那句话中的
window
是什么?你是如何在你的Ionic应用程序中获得它的?
window.cookies.clear(function() {});