Why angular is not recognizing if a user is logged in with keycloak?
up vote
2
down vote
favorite
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
New contributor
add a comment |
up vote
2
down vote
favorite
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
New contributor
Did you implementAPP_INITIALIZER
?
– User3250
17 hours ago
Yes i did implement it.
– Massimo De Sabbata
15 hours ago
Could you addappModule.ts
in the question?
– User3250
14 hours ago
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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
New contributor
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
javascript angular single-sign-on keycloak
New contributor
New contributor
edited yesterday
User3250
1,4872824
1,4872824
New contributor
asked yesterday
Massimo De Sabbata
111
111
New contributor
New contributor
Did you implementAPP_INITIALIZER
?
– User3250
17 hours ago
Yes i did implement it.
– Massimo De Sabbata
15 hours ago
Could you addappModule.ts
in the question?
– User3250
14 hours ago
add a comment |
Did you implementAPP_INITIALIZER
?
– User3250
17 hours ago
Yes i did implement it.
– Massimo De Sabbata
15 hours ago
Could you addappModule.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
add a comment |
active
oldest
votes
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.
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.
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
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
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
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
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
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