Skip to main content
Skip table of contents

Prevent page access if not logged in and redirect to login page

Use case: In your NgxApp application you have multiple pages. You want to prevent a user to access directly theses pages if the user is not logged and in this case redirect the user to a login page.

We will make use of the https://doc.convertigo.com/documentation/latest/reference-manual/convertigo-objects/mobile-application/ngx-components/control-components/appguard/ component.

Add the AppGuard event to the NgxApp set it to onCanActivate event.

As a child, add a customAction component. The following code is a usage example:

TYPESCRIPT
let e = event;
console.log("e:", e);
if (e["pageName"] == "Login"){
	resolve(true)
}else{
	if(page.global.isConnected){
		resolve(true);
	}else {
		page.router.setRoot("Login", {}, null);
		resolve(false);
	}
}

Create a “Login” Page with a form. The onSubmit event will call a login sequence and if access is granted you will set a Global component isConnected to true (tested in the customAction code).

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.