ASP.NET CORE & Angular (single solution) .NET 5.0 v10.2.0
Hi, instead of the seperate public site i would like to add a public available page without users have to login first, to the Angular site.
So when users are not authorized i want them to go to a new public landing page in Angular site, and when they want more direct them to the account login page
I was thinking about making a new folder in the root of the src folder besides 'app' and 'account' and name it public
do i have to alter the appguard? is this possible? can you give me some pointers how to do it?
Kind Regards, Rene van Woezik
12 Answer(s)
-
0
Your idea is how I did exactly what you're asking with one exception - "...and name it public..." - I wouldnt use 'public' as a folder identifier but that's just my personal preference
look at account-routing.module.ts file - based on what you said your expected result is, you're just going to have to modify that page as well as the account.component.html accordingly
Depending on how closely you want to integrate your new page changes where you chose to bounce out of the application to go to the other page you're referring to
First thing you might need to look into is possibly root-routing.module.ts - that's usually where I've been adding that functionality and then adding my modules and components under src/{here} I think to save time you can consider copying the entire DIR 'account' and ripping out about 90% of it so you're just left with:
- landing-route-guard.ts
- landing-routing.module.ts
- landing.component.html
- landing.component.ts
- landing.module.ts
and then modify root-routing to just be:
{ path: 'account', loadChildren: () => import('account/account.module').then(m => m.AccountModule), //Lazy load account module data: { preload: true } }, { path: 'landing', loadChildren: () => import('landing/landing.module').then(m => m.LandingModule), //Lazy load landing module data: { preload: true } },
set a breakpoint on the canActivate method in your new Landing route guard to verify expected behavior
-
0
Thnx!
'landing' is much better then 'public'
Is it possible i can take a look at a copy of sourcecode of your angular site?
Kind regards, Rene van Woezik [email protected]
-
0
I wouldn't feel confortable with that, but you don't need it really
Just follow this:
- copy src\account in that same directory
- You should now have src\account - Copy directory
- Rename account - Copy to landing
- Rename src/landing/account-routing.module.ts file
- Rename account.component.html and account.component.ts accordingly
- Add your code here for that landing page
- Rename src/landing/auth/account-route-guard.ts file
- Update selectBestRoute() method to fit your needs
- Review line 20 to fit your needs
- This is if you plan on integrating actions on your new landing page to tenan
- Update root-routing.module.ts to have a 'landing' new route path
- copy src\account in that same directory
-
0
Thnx, working on it now!
-
0
@smry, Besides auth, I dont'need the other sub dir's right?
-
0
I have not done it in a while but reviewing contents of the Account directory, yes - you should only need that one file out of the auth directory - and all the others (email-activation, login, password, and shared) are not needed for what you are doing
-
0
Thanks a lot @smry for sharing your solution :).
-
0
What i don't get at this moment, is when you go to localhost:4200 and you are not logged in you go to : http://localhost:4200/account/login
but i want to redirect when you are nog logged in to http://localhost:4200/landing
Where do i do this?
-
1
found it! auth-route-guard.ts
-
0
Hi @rvanwoezik
Did you implement this ? Can we close the issue ?
-
0
Can you share what you changed in the auth-route-guard.ts file to get the landing page working without logging in?
Thanks!
-
0
if (!this._sessionService.user) { let sessionObservable = new Subject<any>(); this._refreshTokenService.tryAuthWithRefreshToken() .subscribe( (autResult: boolean) => { if (autResult) { sessionObservable.next(true); sessionObservable.complete(); location.reload(); } else { sessionObservable.next(false); sessionObservable.complete(); this._router.navigate(['/landing']); } }, (error) => { sessionObservable.next(false); sessionObservable.complete(); this._router.navigate(['/landing']); } ); return sessionObservable; }
In src folder i have created a landing folder with a landing component
Hope this helps, Kind regards, Rene van Woezik