Routing angular in the same route if the user is logged or not2019 Community Moderator ElectionAngular 2 Routing- Cannot find primary outlet to load 'AppComponent'Angular2 Routing with base selector as Component in index.htmlAngular routing canActivate AuthGuard transmit query paramsAngular 5 EmptyError: no elements in sequence while making child routesAngular 5 canActivate not workingAngular routing to always go to login page if user is not logged inAngular Login Component Redirect if User is Already loggedCan't stop home redirect routing for already logged userAngular Router Ordering?Angular - 404 when navigating to other component

Giving a talk in my old university, how prominently should I tell students my salary?

How do you make a gun that shoots melee weapons and/or swords?

3.5% Interest Student Loan or use all of my savings on Tuition?

Vector-transposing function

PTIJ: Sport in the Torah

How does a sound wave propagate?

Why isn't P and P/poly trivially the same?

How to write a chaotic neutral protagonist and prevent my readers from thinking they are evil?

Is there a logarithm base for which the logarithm becomes an identity function?

Is it a Cyclops number? "Nobody" knows!

How would an energy-based "projectile" blow up a spaceship?

How to recover against Snake as a heavyweight character?

Was it really inappropriate to write a pull request for the company I interviewed with?

Can I negotiate a patent idea for a raise, under French law?

Do I need a return ticket to Canada if I'm a Japanese National?

Where is the License file location for Identity Server in Sitecore 9.1?

How to educate team mate to take screenshots for bugs with out unwanted stuff

Should I apply for my boss's promotion?

What is the purpose of a disclaimer like "this is not legal advice"?

Boss Telling direct supervisor I snitched

Why do we call complex numbers “numbers” but we don’t consider 2-vectors numbers?

Is this Paypal Github SDK reference really a dangerous site?

Will the concrete slab in a partially heated shed conduct a lot of heat to the unconditioned area?

Inorganic chemistry handbook with reaction lists



Routing angular in the same route if the user is logged or not



2019 Community Moderator ElectionAngular 2 Routing- Cannot find primary outlet to load 'AppComponent'Angular2 Routing with base selector as Component in index.htmlAngular routing canActivate AuthGuard transmit query paramsAngular 5 EmptyError: no elements in sequence while making child routesAngular 5 canActivate not workingAngular routing to always go to login page if user is not logged inAngular Login Component Redirect if User is Already loggedCan't stop home redirect routing for already logged userAngular Router Ordering?Angular - 404 when navigating to other component










0















so far, for differenciate the pages I had to play around whit the children route, basically if the user is logged the MainComponent has to be loaded because in that there is the top bar.
Instead when the user goes to login the top bar is not loaded.



I'd like that when the user is logged and he goes to the page loaded would be the dashboard and instead when he is not and he goes to the page loaded would be the login page.



I tried to use the authguard but every time that I'm adding canActivate: [AuthGuard] my site doesn't work anymore.



my file app-routing.module.ts is :



const routes: Routes = [

path: '',
component: MainComponent,
children: [
path: '', component: DashboardComponent ,
path: 'missions', component: MissionComponent ,
path: 'operators', component: OperatorComponent ,
path: 'materials', component: MaterialComponent ,
path: 'bins', component: BinComponent ,
path: 'main', component: MainComponent ,
]
,
path: 'login', component: LoginComponent ,

];


my auth.guard.ts is :



constructor(
public afAuth: AngularFireAuth,
public userService: UserService,
private router: Router
)

canActivate(): Promise<boolean>
return new Promise((resolve, reject) =>
this.userService.getCurrentUser()
.then(user =>
//this.router.navigate(['/operator']);
return resolve(false);
, err =>
return resolve(true);
);
);










share|improve this question






















  • What error do you get? Why the site not working?

    – Maihan Nijat
    2 days ago











  • I don't get any error, simply everithing is blank

    – alessandro buffoli
    2 days ago











  • Check with chrome. The console.

    – Maihan Nijat
    2 days ago






  • 1





    You can use enableTracing to get which route is getting loaded with route guard angular.io/api/router/ExtraOptions#enableTracing . It should help to understand whats going on.

    – Nikhil Walvekar
    2 days ago











  • @MaihanNijat already checked, the console is clear..

    – alessandro buffoli
    2 days ago















0















so far, for differenciate the pages I had to play around whit the children route, basically if the user is logged the MainComponent has to be loaded because in that there is the top bar.
Instead when the user goes to login the top bar is not loaded.



I'd like that when the user is logged and he goes to the page loaded would be the dashboard and instead when he is not and he goes to the page loaded would be the login page.



I tried to use the authguard but every time that I'm adding canActivate: [AuthGuard] my site doesn't work anymore.



my file app-routing.module.ts is :



const routes: Routes = [

path: '',
component: MainComponent,
children: [
path: '', component: DashboardComponent ,
path: 'missions', component: MissionComponent ,
path: 'operators', component: OperatorComponent ,
path: 'materials', component: MaterialComponent ,
path: 'bins', component: BinComponent ,
path: 'main', component: MainComponent ,
]
,
path: 'login', component: LoginComponent ,

];


my auth.guard.ts is :



constructor(
public afAuth: AngularFireAuth,
public userService: UserService,
private router: Router
)

canActivate(): Promise<boolean>
return new Promise((resolve, reject) =>
this.userService.getCurrentUser()
.then(user =>
//this.router.navigate(['/operator']);
return resolve(false);
, err =>
return resolve(true);
);
);










share|improve this question






















  • What error do you get? Why the site not working?

    – Maihan Nijat
    2 days ago











  • I don't get any error, simply everithing is blank

    – alessandro buffoli
    2 days ago











  • Check with chrome. The console.

    – Maihan Nijat
    2 days ago






  • 1





    You can use enableTracing to get which route is getting loaded with route guard angular.io/api/router/ExtraOptions#enableTracing . It should help to understand whats going on.

    – Nikhil Walvekar
    2 days ago











  • @MaihanNijat already checked, the console is clear..

    – alessandro buffoli
    2 days ago













0












0








0








so far, for differenciate the pages I had to play around whit the children route, basically if the user is logged the MainComponent has to be loaded because in that there is the top bar.
Instead when the user goes to login the top bar is not loaded.



I'd like that when the user is logged and he goes to the page loaded would be the dashboard and instead when he is not and he goes to the page loaded would be the login page.



I tried to use the authguard but every time that I'm adding canActivate: [AuthGuard] my site doesn't work anymore.



my file app-routing.module.ts is :



const routes: Routes = [

path: '',
component: MainComponent,
children: [
path: '', component: DashboardComponent ,
path: 'missions', component: MissionComponent ,
path: 'operators', component: OperatorComponent ,
path: 'materials', component: MaterialComponent ,
path: 'bins', component: BinComponent ,
path: 'main', component: MainComponent ,
]
,
path: 'login', component: LoginComponent ,

];


my auth.guard.ts is :



constructor(
public afAuth: AngularFireAuth,
public userService: UserService,
private router: Router
)

canActivate(): Promise<boolean>
return new Promise((resolve, reject) =>
this.userService.getCurrentUser()
.then(user =>
//this.router.navigate(['/operator']);
return resolve(false);
, err =>
return resolve(true);
);
);










share|improve this question














so far, for differenciate the pages I had to play around whit the children route, basically if the user is logged the MainComponent has to be loaded because in that there is the top bar.
Instead when the user goes to login the top bar is not loaded.



I'd like that when the user is logged and he goes to the page loaded would be the dashboard and instead when he is not and he goes to the page loaded would be the login page.



I tried to use the authguard but every time that I'm adding canActivate: [AuthGuard] my site doesn't work anymore.



my file app-routing.module.ts is :



const routes: Routes = [

path: '',
component: MainComponent,
children: [
path: '', component: DashboardComponent ,
path: 'missions', component: MissionComponent ,
path: 'operators', component: OperatorComponent ,
path: 'materials', component: MaterialComponent ,
path: 'bins', component: BinComponent ,
path: 'main', component: MainComponent ,
]
,
path: 'login', component: LoginComponent ,

];


my auth.guard.ts is :



constructor(
public afAuth: AngularFireAuth,
public userService: UserService,
private router: Router
)

canActivate(): Promise<boolean>
return new Promise((resolve, reject) =>
this.userService.getCurrentUser()
.then(user =>
//this.router.navigate(['/operator']);
return resolve(false);
, err =>
return resolve(true);
);
);







angular routing






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 days ago









alessandro buffolialessandro buffoli

758




758












  • What error do you get? Why the site not working?

    – Maihan Nijat
    2 days ago











  • I don't get any error, simply everithing is blank

    – alessandro buffoli
    2 days ago











  • Check with chrome. The console.

    – Maihan Nijat
    2 days ago






  • 1





    You can use enableTracing to get which route is getting loaded with route guard angular.io/api/router/ExtraOptions#enableTracing . It should help to understand whats going on.

    – Nikhil Walvekar
    2 days ago











  • @MaihanNijat already checked, the console is clear..

    – alessandro buffoli
    2 days ago

















  • What error do you get? Why the site not working?

    – Maihan Nijat
    2 days ago











  • I don't get any error, simply everithing is blank

    – alessandro buffoli
    2 days ago











  • Check with chrome. The console.

    – Maihan Nijat
    2 days ago






  • 1





    You can use enableTracing to get which route is getting loaded with route guard angular.io/api/router/ExtraOptions#enableTracing . It should help to understand whats going on.

    – Nikhil Walvekar
    2 days ago











  • @MaihanNijat already checked, the console is clear..

    – alessandro buffoli
    2 days ago
















What error do you get? Why the site not working?

– Maihan Nijat
2 days ago





What error do you get? Why the site not working?

– Maihan Nijat
2 days ago













I don't get any error, simply everithing is blank

– alessandro buffoli
2 days ago





I don't get any error, simply everithing is blank

– alessandro buffoli
2 days ago













Check with chrome. The console.

– Maihan Nijat
2 days ago





Check with chrome. The console.

– Maihan Nijat
2 days ago




1




1





You can use enableTracing to get which route is getting loaded with route guard angular.io/api/router/ExtraOptions#enableTracing . It should help to understand whats going on.

– Nikhil Walvekar
2 days ago





You can use enableTracing to get which route is getting loaded with route guard angular.io/api/router/ExtraOptions#enableTracing . It should help to understand whats going on.

– Nikhil Walvekar
2 days ago













@MaihanNijat already checked, the console is clear..

– alessandro buffoli
2 days ago





@MaihanNijat already checked, the console is clear..

– alessandro buffoli
2 days ago












1 Answer
1






active

oldest

votes


















1














Try this:



canActivate() 
return this.userService.getCurrentUser()
.pipe(
tap(user =>
if (!user)
this.router.navigate(['/operator']);

),
);



If the user is returned, it means it is logged in and if not then it will be redirected where ever you want to redirect.






share|improve this answer























  • Thankyou for your reply, this helped a lot! It was not the solution but I could debug from console.log!

    – alessandro buffoli
    yesterday






  • 1





    @alessandrobuffoli I am glad you solved your problem.

    – Maihan Nijat
    yesterday










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55028076%2frouting-angular-in-the-same-route-if-the-user-is-logged-or-not%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Try this:



canActivate() 
return this.userService.getCurrentUser()
.pipe(
tap(user =>
if (!user)
this.router.navigate(['/operator']);

),
);



If the user is returned, it means it is logged in and if not then it will be redirected where ever you want to redirect.






share|improve this answer























  • Thankyou for your reply, this helped a lot! It was not the solution but I could debug from console.log!

    – alessandro buffoli
    yesterday






  • 1





    @alessandrobuffoli I am glad you solved your problem.

    – Maihan Nijat
    yesterday















1














Try this:



canActivate() 
return this.userService.getCurrentUser()
.pipe(
tap(user =>
if (!user)
this.router.navigate(['/operator']);

),
);



If the user is returned, it means it is logged in and if not then it will be redirected where ever you want to redirect.






share|improve this answer























  • Thankyou for your reply, this helped a lot! It was not the solution but I could debug from console.log!

    – alessandro buffoli
    yesterday






  • 1





    @alessandrobuffoli I am glad you solved your problem.

    – Maihan Nijat
    yesterday













1












1








1







Try this:



canActivate() 
return this.userService.getCurrentUser()
.pipe(
tap(user =>
if (!user)
this.router.navigate(['/operator']);

),
);



If the user is returned, it means it is logged in and if not then it will be redirected where ever you want to redirect.






share|improve this answer













Try this:



canActivate() 
return this.userService.getCurrentUser()
.pipe(
tap(user =>
if (!user)
this.router.navigate(['/operator']);

),
);



If the user is returned, it means it is logged in and if not then it will be redirected where ever you want to redirect.







share|improve this answer












share|improve this answer



share|improve this answer










answered 2 days ago









Maihan NijatMaihan Nijat

2,84722347




2,84722347












  • Thankyou for your reply, this helped a lot! It was not the solution but I could debug from console.log!

    – alessandro buffoli
    yesterday






  • 1





    @alessandrobuffoli I am glad you solved your problem.

    – Maihan Nijat
    yesterday

















  • Thankyou for your reply, this helped a lot! It was not the solution but I could debug from console.log!

    – alessandro buffoli
    yesterday






  • 1





    @alessandrobuffoli I am glad you solved your problem.

    – Maihan Nijat
    yesterday
















Thankyou for your reply, this helped a lot! It was not the solution but I could debug from console.log!

– alessandro buffoli
yesterday





Thankyou for your reply, this helped a lot! It was not the solution but I could debug from console.log!

– alessandro buffoli
yesterday




1




1





@alessandrobuffoli I am glad you solved your problem.

– Maihan Nijat
yesterday





@alessandrobuffoli I am glad you solved your problem.

– Maihan Nijat
yesterday



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55028076%2frouting-angular-in-the-same-route-if-the-user-is-logged-or-not%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme