Redirect Non-Authenticated Users
Protection of your UI routes from non-authenticated users is performed using the built-in capabilities of our UI libraries. The component will validate that the user is authenticated, will redirect the user to the login dialog (in case of non-authenticated user), and will redirect back to the original route after the login.
Route protection is possible using the following code:
import { Route as PublicRoute, Switch } from "react-router";
import { ProtectedRoute } from '@frontegg/react-auth';
...
<Switch>
<PublicRoute exact path="/">
<br />
<br />
<br />
Welcome!
This is a public page which doesnt require authentication
<br />
</PublicRoute>
<ProtectedRoute path="/profile">
This is a page which requires authentication
</ProtectedRoute>
</Switch>
import { NgModule } from '@angular/core';
import { AuthGuard } from '@frontegg/ng-auth';
import { withFronteggRoutes } from '@frontegg/ng-core';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = withFronteggRoutes([
{ path: 'public', component: PublicComponent },
{
path: 'private',
canActivate: [AuthGuard],
children: [
{
path: '**',
component: PrivateComponent,
},
],
},
]);
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
})
export class AppRoutingModule {
}
Updated 4 months ago