I've encountered a challenge while working on my Angular 14 and Ionic 6 app. I want to implement a "Welcome" screen that only appears the first time a user opens the app, and never again after that.
I'm struggling to figure out how to save the state of whether the user has already seen the screen or not, so the app won't display it on subsequent openings...
My idea was to create a storage service to store this state, and then use a route guard for redirection and displaying the page...
However, I'm now facing an error and I'm unsure how to resolve it. Am I correctly storing the value in the storage service, or is there something fundamentally wrong with my code? ERROR:
Error: src/app/guards/first-load.guard.ts:17:50 - error TS2339: Property 'then' does not exist on type 'void'. [ng] [ng] 17
this.storageService.getValue('first_time').then((value) => { [ng]
Here's my code snippet:
storage.service.ts:
export class StorageService {
constructor(
private storage: Storage
) {
this.init();
}
async init() {
await this.storage.create();
}
setValue(key: 'first_time', value: 'done') {
this.storage.set(key, value);
}
getValue(key: 'first_time') {
this.storage.get(key).then((value) => {
console.log(value);
});
}
}
In the first-load.guard.ts file, I am using this service as follows:
export class FirstLoadGuard implements CanActivate {
constructor(
private storageService: StorageService,
private router: Router
) {}
canActivate(route: ActivatedRouteSnapshot): Promise<boolean>
{
return new Promise(resolve => {
this.storageService.getValue('first_time').then((value) => {
if (value !== null) {
this.router.navigateByUrl('/login');
resolve(false);
}
else {
this.storageService.setValue('first_time', 'done');
resolve(true);
}
});
});
}
}
If more code snippets are needed, please feel free to leave a comment :) Hoping for some assistance!