Why angular is not recognizing if a user is logged in with keycloak?











up vote
2
down vote

favorite
1












So i'm trying to set up a sso system with keycloak on my angular app.



I have downloaded the keycloak library keycloak-angular@4.0.2, setted up a keycloak server at localhost:8080/auth, added the keycloak.json on the root of the app, that keycloak made for me:



   {
"realm": "my-app",
"auth-server-url": "http://localhost:8080/auth",
"ssl-required": "external",
"resource": "my-app",
"public-client": true,
"confidential-port": 0
}


Also I added the keycloak initi function in the main.ts:



import { enableProdMode } from '@angular/core';
//....some other import....
import { KeycloakService } from 'keycloak-angular';


var injector = ReflectiveInjector.resolveAndCreate([KeycloakService]);
var keycloak = injector.get(KeycloakService)

if (environment.production) {
enableProdMode();
}



keycloak.init()
.then(() => platformBrowserDynamic().bootstrapModule(AppModule))
.catch(e => {
console.error(e);
});


Now, if i try to make a registration page, someting like this:



export class registration implements OnInit {


constructor(keycloak:KeycloakService) {

keycloak.register();

}

ngOnInit() {

}

}


it works and sends me to a registration page and if i create an account it apears in the keycloak server.
BUT... even if i am not logged in with any account it lets me access any page, also, if i try :



keycloak.isLoggedIn().then(() => console.log("logged"), ()=>console.log("Not logged"));


The result is "logged", but if i try:



keycloak.loadUserProfile();


Then it says



The user profile was not loaded as the user is not logged in.


I can't understand how it works, am i missing someting?
So my question is:
-How do i prevent pages to show if i am not logged in?
-how do a make a user log and stay logged and angular to recognize who is logged and hwo is not.



Sorry if the question is stupid or the text doesn't matck this site best practices, i'm new around here.



thank you all!










share|improve this question









New contributor




Massimo De Sabbata is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Did you implement APP_INITIALIZER?
    – User3250
    17 hours ago










  • Yes i did implement it.
    – Massimo De Sabbata
    15 hours ago










  • Could you add appModule.ts in the question?
    – User3250
    14 hours ago















up vote
2
down vote

favorite
1












So i'm trying to set up a sso system with keycloak on my angular app.



I have downloaded the keycloak library keycloak-angular@4.0.2, setted up a keycloak server at localhost:8080/auth, added the keycloak.json on the root of the app, that keycloak made for me:



   {
"realm": "my-app",
"auth-server-url": "http://localhost:8080/auth",
"ssl-required": "external",
"resource": "my-app",
"public-client": true,
"confidential-port": 0
}


Also I added the keycloak initi function in the main.ts:



import { enableProdMode } from '@angular/core';
//....some other import....
import { KeycloakService } from 'keycloak-angular';


var injector = ReflectiveInjector.resolveAndCreate([KeycloakService]);
var keycloak = injector.get(KeycloakService)

if (environment.production) {
enableProdMode();
}



keycloak.init()
.then(() => platformBrowserDynamic().bootstrapModule(AppModule))
.catch(e => {
console.error(e);
});


Now, if i try to make a registration page, someting like this:



export class registration implements OnInit {


constructor(keycloak:KeycloakService) {

keycloak.register();

}

ngOnInit() {

}

}


it works and sends me to a registration page and if i create an account it apears in the keycloak server.
BUT... even if i am not logged in with any account it lets me access any page, also, if i try :



keycloak.isLoggedIn().then(() => console.log("logged"), ()=>console.log("Not logged"));


The result is "logged", but if i try:



keycloak.loadUserProfile();


Then it says



The user profile was not loaded as the user is not logged in.


I can't understand how it works, am i missing someting?
So my question is:
-How do i prevent pages to show if i am not logged in?
-how do a make a user log and stay logged and angular to recognize who is logged and hwo is not.



Sorry if the question is stupid or the text doesn't matck this site best practices, i'm new around here.



thank you all!










share|improve this question









New contributor




Massimo De Sabbata is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Did you implement APP_INITIALIZER?
    – User3250
    17 hours ago










  • Yes i did implement it.
    – Massimo De Sabbata
    15 hours ago










  • Could you add appModule.ts in the question?
    – User3250
    14 hours ago













up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





So i'm trying to set up a sso system with keycloak on my angular app.



I have downloaded the keycloak library keycloak-angular@4.0.2, setted up a keycloak server at localhost:8080/auth, added the keycloak.json on the root of the app, that keycloak made for me:



   {
"realm": "my-app",
"auth-server-url": "http://localhost:8080/auth",
"ssl-required": "external",
"resource": "my-app",
"public-client": true,
"confidential-port": 0
}


Also I added the keycloak initi function in the main.ts:



import { enableProdMode } from '@angular/core';
//....some other import....
import { KeycloakService } from 'keycloak-angular';


var injector = ReflectiveInjector.resolveAndCreate([KeycloakService]);
var keycloak = injector.get(KeycloakService)

if (environment.production) {
enableProdMode();
}



keycloak.init()
.then(() => platformBrowserDynamic().bootstrapModule(AppModule))
.catch(e => {
console.error(e);
});


Now, if i try to make a registration page, someting like this:



export class registration implements OnInit {


constructor(keycloak:KeycloakService) {

keycloak.register();

}

ngOnInit() {

}

}


it works and sends me to a registration page and if i create an account it apears in the keycloak server.
BUT... even if i am not logged in with any account it lets me access any page, also, if i try :



keycloak.isLoggedIn().then(() => console.log("logged"), ()=>console.log("Not logged"));


The result is "logged", but if i try:



keycloak.loadUserProfile();


Then it says



The user profile was not loaded as the user is not logged in.


I can't understand how it works, am i missing someting?
So my question is:
-How do i prevent pages to show if i am not logged in?
-how do a make a user log and stay logged and angular to recognize who is logged and hwo is not.



Sorry if the question is stupid or the text doesn't matck this site best practices, i'm new around here.



thank you all!










share|improve this question









New contributor




Massimo De Sabbata is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











So i'm trying to set up a sso system with keycloak on my angular app.



I have downloaded the keycloak library keycloak-angular@4.0.2, setted up a keycloak server at localhost:8080/auth, added the keycloak.json on the root of the app, that keycloak made for me:



   {
"realm": "my-app",
"auth-server-url": "http://localhost:8080/auth",
"ssl-required": "external",
"resource": "my-app",
"public-client": true,
"confidential-port": 0
}


Also I added the keycloak initi function in the main.ts:



import { enableProdMode } from '@angular/core';
//....some other import....
import { KeycloakService } from 'keycloak-angular';


var injector = ReflectiveInjector.resolveAndCreate([KeycloakService]);
var keycloak = injector.get(KeycloakService)

if (environment.production) {
enableProdMode();
}



keycloak.init()
.then(() => platformBrowserDynamic().bootstrapModule(AppModule))
.catch(e => {
console.error(e);
});


Now, if i try to make a registration page, someting like this:



export class registration implements OnInit {


constructor(keycloak:KeycloakService) {

keycloak.register();

}

ngOnInit() {

}

}


it works and sends me to a registration page and if i create an account it apears in the keycloak server.
BUT... even if i am not logged in with any account it lets me access any page, also, if i try :



keycloak.isLoggedIn().then(() => console.log("logged"), ()=>console.log("Not logged"));


The result is "logged", but if i try:



keycloak.loadUserProfile();


Then it says



The user profile was not loaded as the user is not logged in.


I can't understand how it works, am i missing someting?
So my question is:
-How do i prevent pages to show if i am not logged in?
-how do a make a user log and stay logged and angular to recognize who is logged and hwo is not.



Sorry if the question is stupid or the text doesn't matck this site best practices, i'm new around here.



thank you all!







javascript angular single-sign-on keycloak






share|improve this question









New contributor




Massimo De Sabbata is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Massimo De Sabbata is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited yesterday









User3250

1,4872824




1,4872824






New contributor




Massimo De Sabbata is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









Massimo De Sabbata

111




111




New contributor




Massimo De Sabbata is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Massimo De Sabbata is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Massimo De Sabbata is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Did you implement APP_INITIALIZER?
    – User3250
    17 hours ago










  • Yes i did implement it.
    – Massimo De Sabbata
    15 hours ago










  • Could you add appModule.ts in the question?
    – User3250
    14 hours ago


















  • Did you implement APP_INITIALIZER?
    – User3250
    17 hours ago










  • Yes i did implement it.
    – Massimo De Sabbata
    15 hours ago










  • Could you add appModule.ts in the question?
    – User3250
    14 hours ago
















Did you implement APP_INITIALIZER?
– User3250
17 hours ago




Did you implement APP_INITIALIZER?
– User3250
17 hours ago












Yes i did implement it.
– Massimo De Sabbata
15 hours ago




Yes i did implement it.
– Massimo De Sabbata
15 hours ago












Could you add appModule.ts in the question?
– User3250
14 hours ago




Could you add appModule.ts in the question?
– User3250
14 hours ago

















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
});


}
});






Massimo De Sabbata is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265092%2fwhy-angular-is-not-recognizing-if-a-user-is-logged-in-with-keycloak%23new-answer', 'question_page');
}
);

Post as a guest





































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes








Massimo De Sabbata is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















Massimo De Sabbata is a new contributor. Be nice, and check out our Code of Conduct.













Massimo De Sabbata is a new contributor. Be nice, and check out our Code of Conduct.












Massimo De Sabbata is a new contributor. Be nice, and check out our Code of Conduct.















 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265092%2fwhy-angular-is-not-recognizing-if-a-user-is-logged-in-with-keycloak%23new-answer', 'question_page');
}
);

Post as a guest




















































































Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Can I use Tabulator js library in my java Spring + Thymeleaf project?