Ionic4 Angular AuthGuard works for home page and not for sign in page









up vote
0
down vote

favorite












I have created an Authguard for my web app and have assigned into several routes like Home, Sign-in, Sign-up etc.



Expected Behavior:



  1. Home page - the intention is to show the login page if the user is not logged in

  2. Signin , Signup, Forgot Password etc - the intention is to redirect the user to the home page if he has already logged in (to avoid displaying the Sign-in page to a logged in user, if the user types the url in the address bar)

Actual Behavior:



  1. If a user who has not logged in visits the home page, he is being redirected to Sign in page, but the page is not displayed.


  2. If a user visits the Sign-in page by URL, again the page is not displayed. [ No matter if the user is logged in or not]


  3. If I remove the auth guard for the Sign-in page, Loading the home page redirects the user to Sign in page and the page is displayed properly.


My AuthGuard code



 @Injectable()
export class AuthGuard implements CanActivate boolean

return this.afAuth.authState.pipe(
take(1),
map(user => !!user),
tap(loggedIn =>
const url = route.url.toString();
if (url === ROUTE_SIGNIN )
);




EDIT:
Routes:



 const routes: Routes = [

path: '',
redirectTo: ROUTE_HOME,
pathMatch: 'full'
,
path: ROUTE_HOME, loadChildren: './pages/home/home.module#HomePageModule', canActivate: [AuthGuard] ,
path: ROUTE_NAV_LIST, loadChildren: './pages/list/list.module#ListPageModule', canActivate: [AuthGuard] ,
path: ROUTE_SIGNIN, loadChildren: './pages/signin/signin.module#SigninPageModule', canActivate: [AuthGuard] ,
path: ROUTE_SIGNUP, loadChildren: './pages/signup/signup.module#SignupPageModule', canActivate: [AuthGuard] ,
path: ROUTE_FORGOT_PASSWORD, loadChildren: './pages/forgot-password/forgot-password.module#ForgotPasswordPageModule'
, canActivate: [AuthGuard] ,
path: ROUTE_PHONE_VERIFICATION, loadChildren: './pages/phone-verification/phone-verification.module#PhoneVerificationPageModule'
, canActivate: [AuthGuard] ,

];


I can understand that the problem here is with assigning the Authguard to the login page. But I couldn't identify what is causing the problem. Can any of you point me in the right direction?










share|improve this question























  • instead of returning return of(false) or return of(true) just return true or false
    – Suryan
    Nov 11 at 6:25










  • tried that already. didn't work either
    – Aravind
    Nov 11 at 6:30










  • Show your routes as well. In your place, to reduce complexity, I would make 2 guards, AuthenticatedGuard - any route attached will require user to be authenticated and UnauthenticatedGuard - any route attached will require user to be un-authenticated. This way it easier to manage, no need to do this complex ifs etc.
    – muradm
    Nov 11 at 7:05










  • @muradm I have edited the question and added the routes. I also tried adding a separate UnAuthGuard which would just check the loggedin status and redirect it to home if it was true. But it did not make any difference. I'm not sure what I'm missing in that simple piece of code.
    – Aravind
    Nov 11 at 8:07











  • did you try to put your authentication inside the app.component.ts file and inside the platform ready function?
    – nyx97
    Nov 12 at 0:54














up vote
0
down vote

favorite












I have created an Authguard for my web app and have assigned into several routes like Home, Sign-in, Sign-up etc.



Expected Behavior:



  1. Home page - the intention is to show the login page if the user is not logged in

  2. Signin , Signup, Forgot Password etc - the intention is to redirect the user to the home page if he has already logged in (to avoid displaying the Sign-in page to a logged in user, if the user types the url in the address bar)

Actual Behavior:



  1. If a user who has not logged in visits the home page, he is being redirected to Sign in page, but the page is not displayed.


  2. If a user visits the Sign-in page by URL, again the page is not displayed. [ No matter if the user is logged in or not]


  3. If I remove the auth guard for the Sign-in page, Loading the home page redirects the user to Sign in page and the page is displayed properly.


My AuthGuard code



 @Injectable()
export class AuthGuard implements CanActivate boolean

return this.afAuth.authState.pipe(
take(1),
map(user => !!user),
tap(loggedIn =>
const url = route.url.toString();
if (url === ROUTE_SIGNIN )
);




EDIT:
Routes:



 const routes: Routes = [

path: '',
redirectTo: ROUTE_HOME,
pathMatch: 'full'
,
path: ROUTE_HOME, loadChildren: './pages/home/home.module#HomePageModule', canActivate: [AuthGuard] ,
path: ROUTE_NAV_LIST, loadChildren: './pages/list/list.module#ListPageModule', canActivate: [AuthGuard] ,
path: ROUTE_SIGNIN, loadChildren: './pages/signin/signin.module#SigninPageModule', canActivate: [AuthGuard] ,
path: ROUTE_SIGNUP, loadChildren: './pages/signup/signup.module#SignupPageModule', canActivate: [AuthGuard] ,
path: ROUTE_FORGOT_PASSWORD, loadChildren: './pages/forgot-password/forgot-password.module#ForgotPasswordPageModule'
, canActivate: [AuthGuard] ,
path: ROUTE_PHONE_VERIFICATION, loadChildren: './pages/phone-verification/phone-verification.module#PhoneVerificationPageModule'
, canActivate: [AuthGuard] ,

];


I can understand that the problem here is with assigning the Authguard to the login page. But I couldn't identify what is causing the problem. Can any of you point me in the right direction?










share|improve this question























  • instead of returning return of(false) or return of(true) just return true or false
    – Suryan
    Nov 11 at 6:25










  • tried that already. didn't work either
    – Aravind
    Nov 11 at 6:30










  • Show your routes as well. In your place, to reduce complexity, I would make 2 guards, AuthenticatedGuard - any route attached will require user to be authenticated and UnauthenticatedGuard - any route attached will require user to be un-authenticated. This way it easier to manage, no need to do this complex ifs etc.
    – muradm
    Nov 11 at 7:05










  • @muradm I have edited the question and added the routes. I also tried adding a separate UnAuthGuard which would just check the loggedin status and redirect it to home if it was true. But it did not make any difference. I'm not sure what I'm missing in that simple piece of code.
    – Aravind
    Nov 11 at 8:07











  • did you try to put your authentication inside the app.component.ts file and inside the platform ready function?
    – nyx97
    Nov 12 at 0:54












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have created an Authguard for my web app and have assigned into several routes like Home, Sign-in, Sign-up etc.



Expected Behavior:



  1. Home page - the intention is to show the login page if the user is not logged in

  2. Signin , Signup, Forgot Password etc - the intention is to redirect the user to the home page if he has already logged in (to avoid displaying the Sign-in page to a logged in user, if the user types the url in the address bar)

Actual Behavior:



  1. If a user who has not logged in visits the home page, he is being redirected to Sign in page, but the page is not displayed.


  2. If a user visits the Sign-in page by URL, again the page is not displayed. [ No matter if the user is logged in or not]


  3. If I remove the auth guard for the Sign-in page, Loading the home page redirects the user to Sign in page and the page is displayed properly.


My AuthGuard code



 @Injectable()
export class AuthGuard implements CanActivate boolean

return this.afAuth.authState.pipe(
take(1),
map(user => !!user),
tap(loggedIn =>
const url = route.url.toString();
if (url === ROUTE_SIGNIN )
);




EDIT:
Routes:



 const routes: Routes = [

path: '',
redirectTo: ROUTE_HOME,
pathMatch: 'full'
,
path: ROUTE_HOME, loadChildren: './pages/home/home.module#HomePageModule', canActivate: [AuthGuard] ,
path: ROUTE_NAV_LIST, loadChildren: './pages/list/list.module#ListPageModule', canActivate: [AuthGuard] ,
path: ROUTE_SIGNIN, loadChildren: './pages/signin/signin.module#SigninPageModule', canActivate: [AuthGuard] ,
path: ROUTE_SIGNUP, loadChildren: './pages/signup/signup.module#SignupPageModule', canActivate: [AuthGuard] ,
path: ROUTE_FORGOT_PASSWORD, loadChildren: './pages/forgot-password/forgot-password.module#ForgotPasswordPageModule'
, canActivate: [AuthGuard] ,
path: ROUTE_PHONE_VERIFICATION, loadChildren: './pages/phone-verification/phone-verification.module#PhoneVerificationPageModule'
, canActivate: [AuthGuard] ,

];


I can understand that the problem here is with assigning the Authguard to the login page. But I couldn't identify what is causing the problem. Can any of you point me in the right direction?










share|improve this question















I have created an Authguard for my web app and have assigned into several routes like Home, Sign-in, Sign-up etc.



Expected Behavior:



  1. Home page - the intention is to show the login page if the user is not logged in

  2. Signin , Signup, Forgot Password etc - the intention is to redirect the user to the home page if he has already logged in (to avoid displaying the Sign-in page to a logged in user, if the user types the url in the address bar)

Actual Behavior:



  1. If a user who has not logged in visits the home page, he is being redirected to Sign in page, but the page is not displayed.


  2. If a user visits the Sign-in page by URL, again the page is not displayed. [ No matter if the user is logged in or not]


  3. If I remove the auth guard for the Sign-in page, Loading the home page redirects the user to Sign in page and the page is displayed properly.


My AuthGuard code



 @Injectable()
export class AuthGuard implements CanActivate boolean

return this.afAuth.authState.pipe(
take(1),
map(user => !!user),
tap(loggedIn =>
const url = route.url.toString();
if (url === ROUTE_SIGNIN )
);




EDIT:
Routes:



 const routes: Routes = [

path: '',
redirectTo: ROUTE_HOME,
pathMatch: 'full'
,
path: ROUTE_HOME, loadChildren: './pages/home/home.module#HomePageModule', canActivate: [AuthGuard] ,
path: ROUTE_NAV_LIST, loadChildren: './pages/list/list.module#ListPageModule', canActivate: [AuthGuard] ,
path: ROUTE_SIGNIN, loadChildren: './pages/signin/signin.module#SigninPageModule', canActivate: [AuthGuard] ,
path: ROUTE_SIGNUP, loadChildren: './pages/signup/signup.module#SignupPageModule', canActivate: [AuthGuard] ,
path: ROUTE_FORGOT_PASSWORD, loadChildren: './pages/forgot-password/forgot-password.module#ForgotPasswordPageModule'
, canActivate: [AuthGuard] ,
path: ROUTE_PHONE_VERIFICATION, loadChildren: './pages/phone-verification/phone-verification.module#PhoneVerificationPageModule'
, canActivate: [AuthGuard] ,

];


I can understand that the problem here is with assigning the Authguard to the login page. But I couldn't identify what is causing the problem. Can any of you point me in the right direction?







angular ionic-framework routing angular-router-guards auth-guard






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 8:06

























asked Nov 11 at 5:52









Aravind

185




185











  • instead of returning return of(false) or return of(true) just return true or false
    – Suryan
    Nov 11 at 6:25










  • tried that already. didn't work either
    – Aravind
    Nov 11 at 6:30










  • Show your routes as well. In your place, to reduce complexity, I would make 2 guards, AuthenticatedGuard - any route attached will require user to be authenticated and UnauthenticatedGuard - any route attached will require user to be un-authenticated. This way it easier to manage, no need to do this complex ifs etc.
    – muradm
    Nov 11 at 7:05










  • @muradm I have edited the question and added the routes. I also tried adding a separate UnAuthGuard which would just check the loggedin status and redirect it to home if it was true. But it did not make any difference. I'm not sure what I'm missing in that simple piece of code.
    – Aravind
    Nov 11 at 8:07











  • did you try to put your authentication inside the app.component.ts file and inside the platform ready function?
    – nyx97
    Nov 12 at 0:54
















  • instead of returning return of(false) or return of(true) just return true or false
    – Suryan
    Nov 11 at 6:25










  • tried that already. didn't work either
    – Aravind
    Nov 11 at 6:30










  • Show your routes as well. In your place, to reduce complexity, I would make 2 guards, AuthenticatedGuard - any route attached will require user to be authenticated and UnauthenticatedGuard - any route attached will require user to be un-authenticated. This way it easier to manage, no need to do this complex ifs etc.
    – muradm
    Nov 11 at 7:05










  • @muradm I have edited the question and added the routes. I also tried adding a separate UnAuthGuard which would just check the loggedin status and redirect it to home if it was true. But it did not make any difference. I'm not sure what I'm missing in that simple piece of code.
    – Aravind
    Nov 11 at 8:07











  • did you try to put your authentication inside the app.component.ts file and inside the platform ready function?
    – nyx97
    Nov 12 at 0:54















instead of returning return of(false) or return of(true) just return true or false
– Suryan
Nov 11 at 6:25




instead of returning return of(false) or return of(true) just return true or false
– Suryan
Nov 11 at 6:25












tried that already. didn't work either
– Aravind
Nov 11 at 6:30




tried that already. didn't work either
– Aravind
Nov 11 at 6:30












Show your routes as well. In your place, to reduce complexity, I would make 2 guards, AuthenticatedGuard - any route attached will require user to be authenticated and UnauthenticatedGuard - any route attached will require user to be un-authenticated. This way it easier to manage, no need to do this complex ifs etc.
– muradm
Nov 11 at 7:05




Show your routes as well. In your place, to reduce complexity, I would make 2 guards, AuthenticatedGuard - any route attached will require user to be authenticated and UnauthenticatedGuard - any route attached will require user to be un-authenticated. This way it easier to manage, no need to do this complex ifs etc.
– muradm
Nov 11 at 7:05












@muradm I have edited the question and added the routes. I also tried adding a separate UnAuthGuard which would just check the loggedin status and redirect it to home if it was true. But it did not make any difference. I'm not sure what I'm missing in that simple piece of code.
– Aravind
Nov 11 at 8:07





@muradm I have edited the question and added the routes. I also tried adding a separate UnAuthGuard which would just check the loggedin status and redirect it to home if it was true. But it did not make any difference. I'm not sure what I'm missing in that simple piece of code.
– Aravind
Nov 11 at 8:07













did you try to put your authentication inside the app.component.ts file and inside the platform ready function?
– nyx97
Nov 12 at 0:54




did you try to put your authentication inside the app.component.ts file and inside the platform ready function?
– nyx97
Nov 12 at 0:54

















active

oldest

votes











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',
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%2f53246211%2fionic4-angular-authguard-works-for-home-page-and-not-for-sign-in-page%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53246211%2fionic4-angular-authguard-works-for-home-page-and-not-for-sign-in-page%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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto