Hi!
Here’s my issue. I’m trying to find the best way to handle this.
I have my back-end which is an API that communicates with my SQL database. When I login I query my API to get validate user/password and return a JWT token. I store this token inside SqlStorage on device. All of this works perfectly!
My main problem is what is the best practice to validate that the user is still logged in to hide/show some stuff like menu items. I tried few thing but I’m not 100% convinced is the best approach.
I would like to have a function that return true/false if the user is logged in or not to use it with a *ngIf for example. Because right now if I query the sqlstorage that return a promise so I can’t use it inside *ngIf. From what I’ve tried right now when I login I can successfully insert my data in my sqlstorage and set my loggedIn property to true inside my auth-service. But when I leave the page and come back (from browser) it’s set back to it’s default value.
Also how would you handle the loggedIn thingy? I don’t want to kick out (logout) the user if for some reason the token is expired.
auth-service.ts
constructor(private http: Http, private platform: Platform) {
this.platform = platform;
this.platform.ready().then(() => {
this.storage = new Storage(SqlStorage);
});
this.http = http;
}
.......................
login(loginData) {
return new Promise(resolve => {
this.http.post(this.baseUrl + 'Login', JSON.stringify(loginData), {headers: ApiHeaders})
.subscribe(response => {
var data = response.json();
console.log("login reponse", data);
if(data.Result) {
console.log("loggedin", data);
this.authSuccess(data.TokenValue);
}
resolve(data);
},
error => {
console.log("error login", error);
resolve(false);
});
});
}
....................
authSuccess(token) {
this.storage.query("INSERT INTO Auths (userId, authToken) VALUES ("+this.jwtHelper.decodeToken(token).UserId+", '"+token+"')").then(data => {
console.log("inserted", data.res);
this.userId = this.jwtHelper.decodeToken(token).UserId;
this.loggedIn = true;
}, error => {
console.log("error", error);
});
}
......................
logout() {
return new Promise(resolve => {
console.log("userid logout", this.userId);
this.storage.query("DELETE FROM Auths WHERE userId="+this.userId).then(data => {
console.log("deleted", data.res);
this.userId = null;
this.loggedIn = false;
resolve(true);
}, error => {
console.log("error", error);
resolve(false);
});
});
}
I hope I’m clear enough and can’t wait to see your solutions!
Thanks a lot!