Build Secure Login for Your Angular App

Pooja Dabholker

Hi Matt,I got the redirection okta page working.I have a dashboard component which already has a can activate .have also added oktagauthgaurd.
{ path: ‘dashboard’, component: DashboardComponent, canActivate: [ AuthGuardService,OktaAuthGuard] }.
I noticed it goes in both the can activate methods and loads the dashboard but with no user details which is set in ngOnInit .it somehow fails to execute ngOnInit and constructor methods of dashboard
Could you please guide me here.
Thank you in advance

Matt Raible

I’ve never tried to have two auth guards for one component before, so I’m not sure this will work. Does your component implement OnInit? What does the code in your ngOnInit() method look like?

Pooja Dabholker

In ngOnInit() I am getting certain user details from a service

Pooja Dabholker

also on my url after redirection I see http://localhost:4200/ when it should be http://localhost:4200/dashboard

Pooja Dabholker

Hi Matt,
My multifactor authenticatin is working on angular 4 …the redirection method.
Thank you so much for this blog and your help

Pooja Dabholker

Hi Matt,
I had created an account for integrating okta with my test okta app.Now I have use the actual issuer where Okta org acts as an authorization server,hence there is no default authorization server tab present.when I add this new issuer(eg https://subdomain.okta.com) and client ID ,I get this error in img https://uploads.disquscdn.c…

Matt Raible

You need to create an OIDC Single-Page Application for this tutorial to work. If you use the Okta CLI (like this tutorial recommends), everything will work as expected.

Pooja Dabholker

Hi Matt,
yes I have created SPA on okta and integrated with my app.It works as expected.
Now I need to replace my okta configuration with Prod okta configuration .This I noticed did not have the authorization server tab under security->API.
I read this https://support.okta.com/he… and realized I will have to set the issuer as https://subdomain.okta.com instead of https://{yourOktaDomain}/oauth2/default .
After making these changes I get the above error.Any more okta configuration is required?

Pooja Dabholker

I also have the client secret…Does that need to be there in the okta configuration at angular end

Matt Raible

If you don’t have an authorization server under Security > API, you haven’t purchased API Access management and you can’t validate JWTs outside of Okta. Your options are 1) purchase API Access Management or 2) use a developer account (since it comes for free with them).

Matt Raible

No. You should never store a client secret in your JavaScript code.

Pooja Dabholker

Hello Matt,
I am able to get the okta redirection working in my application.I use this method to redirect to okta login,this.oktaAuth.loginRedirect().
Just a last and major issue I am facing is that const AccessToken = this.oktaAuth.getAccessToken().then(
(idToken) => {
alert(1+“idToken”+idToken);
}

);
const isAutheticated = this.oktaAuth.isAuthenticated().then(

(isAuthenticated) => {this.isAuthenticated = isAuthenticated;
alert(1+“isAuthenticated”+isAuthenticated);
}

);
all of this is returning me undefined.I want to use isAuthenticated as a check,but I cannot understand why its not returning true.
I have also added cors setting and the flow works fine otherwise.
Thanks in advance

Matt Raible

I’d suggest comparing your code to our Angular SDK docs. If you can’t get it working, add an issue to that GitHub repo with steps to reproduce the problem.

Pooja Dabholker

Thank you for your reply Matt.I have done the similar change suggested in the SDK doc.I have redirect on many places in my app.So I am assuming the rediect url changes on redirect and hence I get isAuthenticated undefined.Is there a example where later I can call the /v1/token in angular and fetch the token

Matt Raible

You shouldn’t need to call the /token endpoint yourself, the Angular SDK should handle that for you. If you have refresh tokens enabled, and configure “offline_access” as a scope, you should get new access tokens when needed. If you want to see the easiest way to get started with Okta and Angular, see my video: https://www.youtube.com/wat…

Pooja Dabholker

Hi Matt,
Thank you for your valuable time.
I have added the scope.
Still not getting isAuthenticated true .
https://uploads.disquscdn.c…

Matt Raible

To be honest, I haven’t tried our refresh token support in an Angular app. Can you please submit an issue in the Angular SDK project and include steps to reproduce the problem?

Pooja Dabholker

Hi Matt,
Thank you so much for helping me till here .I managed to get it working on my local.The issue was with my routes.
I am left with a final query.
We are moving this to our QA environment.The url is somewhat like https://abc.com/efg/hij .After callback it redirects to BASE ie https://abc.com. How can we change it to redirect to actual redirectURI configured.

Pooja Dabholker

Hi Matt,
The callback uri is called,after that the url is trimmed upfill the base ie https://abc.com.

Pooja Dabholker

i have also set my base.href pointing to the correct production path.Still no luck