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
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
|
show 1 more comment
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
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
|
show 1 more comment
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
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
angular routing
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
|
show 1 more comment
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
|
show 1 more comment
1 Answer
1
active
oldest
votes
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.
Thankyou for your reply, this helped a lot! It was not the solution but I could debug fromconsole.log
!
– alessandro buffoli
yesterday
1
@alessandrobuffoli I am glad you solved your problem.
– Maihan Nijat
yesterday
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Thankyou for your reply, this helped a lot! It was not the solution but I could debug fromconsole.log
!
– alessandro buffoli
yesterday
1
@alessandrobuffoli I am glad you solved your problem.
– Maihan Nijat
yesterday
add a comment |
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.
Thankyou for your reply, this helped a lot! It was not the solution but I could debug fromconsole.log
!
– alessandro buffoli
yesterday
1
@alessandrobuffoli I am glad you solved your problem.
– Maihan Nijat
yesterday
add a comment |
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.
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.
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 fromconsole.log
!
– alessandro buffoli
yesterday
1
@alessandrobuffoli I am glad you solved your problem.
– Maihan Nijat
yesterday
add a comment |
Thankyou for your reply, this helped a lot! It was not the solution but I could debug fromconsole.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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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