Currently, I am in the process of developing a web application with Angular 6, incorporating Google sign-in using the SocialLogin library. Below is the code snippet that I have implemented:
public socialSignIn(socialPlatform : string) {
let socialPlatformProvider;
if(socialPlatform == "facebook"){
socialPlatformProvider = FacebookLoginProvider.PROVIDER_ID;
}else if(socialPlatform == "google"){
socialPlatformProvider = GoogleLoginProvider.PROVIDER_ID;
}
this.socialAuthService.signIn(socialPlatformProvider).then(
(userData) => {
let user: User = new User();
let newuserrequest: NewUserRequest = new NewUserRequest();
user.email = userData.email;
var username;
username = userData.name;
var uuid = this.newGuid();
newuserrequest.requestId = uuid;
newuserrequest.user = user;
newuserrequest.accountId = userData.id;
newuserrequest.accountIdToken = userData.idToken;
this.newuserrequest = Array<NewUserRequest>(newuserrequest);
this.personname2 = userData.name;
this.personname = this.personname2.toString();
console.log('Personname: ' + this.personname);
this.div = document.getElementById("name");
this.div.textContent = this.personname;
var token;
token = userData.idToken;
this.saveValueUser(username, user.email);
this.islogedin = true;
this.saveValue(token);
this.communicator = new CommunicatorService(this.httpClient);
var url;
url = "myurl/newuser";
this.communicator.getDataFromServer(this.newuserrequest, url)
.subscribe(
(data:any) => {
console.log(data);
}
);
}
);
}
The above code works properly, but it has a limitation as the token expires after 1 hour. To address this issue, I developed a method utilizing JWT-helper-service to check the validity and expiration time of the token. Here is the method:
private tokenHandler(){
this.helper = new JwtHelperService();
this.isExpired = this.helper.isTokenExpired(this.token);
if(this.isExpired != true) {
this.expirationDate = this.helper.getTokenExpirationDate(this.token);
}
console.log("Is expired: " + this.isExpired);
console.log("Expiration date: " + this.expirationDate);
if(this.isExpired == true){
this.signOut();
}
}
I am looking for guidance on how to implement token refreshing before it expires.