How to set token Login and how to remove token Logout

how to set token Login And and how to remove token Logout

signIn(){
var $$ = this.Dom7
const self = this;
self.$f7.dialog.preloader();
var that = this;
this.$f7.request.promise.postJSON(‘http://localhost:50682/api/Auth/ClinetLogin’, { clinet_Phone:this.username, clinet_Password: this.password })
.then(function (res) {
if(res){
localStorage.setItem(‘token’, res.data.data);
localStorage.setItem(‘name’, res.data.message);
that.$f7router.navigate("/home/");
self.$f7.dialog.close();
}
}).catch(function (err) {
f7.dialog.alert(‘خطا في معلومات الحساب’ );
self.$f7.dialog.close();
}) }

logout(){
var that = this;
localStorage.clear(‘token’);
localStorage.clear(‘name’);
that.$f7router.navigate("/");
}

when i login agine the requset.setup send old token

app.js
var token = localStorage.getItem(‘token’)
Framework7.request.setup({
headers: {
‘Authorization’: 'Bearer '+ token
}
})

What you do in app.js seems not correct.

  1. Without login, the token variable can occur and is undefined.
  2. After logout, the token variable is defined, but you need to clear the variable even if you delete the variable from local storage.

I recommend using LocalForage. It supports the asynchronous and synchronous operations you need. You can also safely store more meaningful data sets.

Also, you should not ignore the exiting user pressing the back key. If you are using framework7 pushState feature, you should clear your past routes or do async checks on routes.

https://framework7.io/docs/view.html#router-api-methods-properties
https://framework7.io/docs/routes.html#async-route

1 Like

Can you Give me Example Code ?