# Angular Sign-in widget - Check if user is already authenticated

**URL:** <https://devforum.okta.com/t/angular-sign-in-widget-check-if-user-is-already-authenticated/20816>\
**Category:** Questions\
**Created:** [May 31, 2022, 1:22am UTC](https://devforum.okta.com/t/angular-sign-in-widget-check-if-user-is-already-authenticated/20816 "2022-05-31T01:22:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![WillSutton](https://avatars.discourse-cdn.com/v4/letter/w/848f3c/32.png) [@WillSutton](https://devforum.okta.com/u/WillSutton)\
**Post date:** [May 31, 2022, 1:22am UTC](https://devforum.okta.com/t/angular-sign-in-widget-check-if-user-is-already-authenticated/20816/1 "2022-05-31T01:22:54Z")

</div>

I am developing an application in Angular using the Okta Sign-In widget. The logic for accepting a user response is below:

```auto
ngOnInit() {

    this.widget.renderEl(
      {
        el: '#okta-signin-container',
      },
      (res) => {
        if (res.status === 'SUCCESS') {
          this.signIn.loginRedirect('/dashboard', {
            sessionToken: res.session.token,
          });
          // Hide the widget
          this.widget.hide();
        }
      },
      (err) => {
        throw err;
      }
    );
  }

```

I want to introduce a check in the logic that prevents a log-in and displays a login if a user is already logged in (a valid session exists). This user would have been logged in from another method while the page with the widget has already been loaded. This would look something like below with a dummy isAuthenticated() method:

```auto
ngOnInit() 
{
    this.widget.renderEl(
    {
        el: '#okta-signin-container',
    },
    (res) => {
        if (res.status === 'SUCCESS') {
            // If user is already logged in
            if(isAuthenticated())
            {
                // Display a pop-up and prevent login
            }
            else
            {
                sessionToken: res.session.token,
            }
            this.signIn.loginRedirect('/dashboard', {
          });
          // Hide the widget
          this.widget.hide();
        }
      },
      (err) => {
        throw err;
      }
    );
}

```

Is this possible? Please let me know if I need to clarify anything or add any extra information 🙂

Package versions:  
@okta/okta-angular: 2.2.1  
@okta/okta-signin-widget: 5.16.1

The full code for this section is as below:

```auto
signIn;

widget = new OktaSignIn({
    baseUrl: '{OKTA_URL}',
    registration: {
      click: function () {
        window.location.href = '/sign-ups';
      },
    },
    features: {
      registration: true,
    },
    authParams: {
      pkce: true,
    },
  });

constructor(oktaAuthService: OktaAuthService,
              router: Router,
              private actvRoute: ActivatedRoute)
  {
    this.signIn = oktaAuthService;

    // Show the widget when prompted, otherwise remove it from the DOM.
    router.events.forEach((event) => {
      if (event instanceof NavigationStart) {
        switch (event.url) {
          case '/login':
            break;
          case '/protected':
            break;
          default:
            this.widget.remove();
            break;
        }
      }
    });
  }

  ngOnInit() {

    this.widget.renderEl(
      {
        el: '#okta-signin-container',
      },
      (res) => {
        if (res.status === 'SUCCESS') {
          this.signIn.loginRedirect('/dashboard', {
            sessionToken: res.session.token,
          });
          // Hide the widget
          this.widget.hide();
        }
      },
      (err) => {
        throw err;
      }
    );
  }

```

---

<div class="post-metadata">

**Author:** ![andrea](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/andrea/32/2172_2.png) [@andrea](https://devforum.okta.com/u/andrea)\
**Post date:** [February 12, 2024, 7:48pm UTC](https://devforum.okta.com/t/angular-sign-in-widget-check-if-user-is-already-authenticated/20816/2 "2024-02-12T19:48:56Z")

</div>

Closed on February 12, 2024, 7:48pm UTC


