question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Expose the Auth state to outside world

See original GitHub issue

Is your feature request related to a problem? Please describe.

WRITE:

When using UI components (in React), I need to customize some aspect of the UI further than basic changes. For example I use the secondary-footer-content web component slot of Sign-In component to create a custom Forgot password link. But to override it with a link I build myself I need to have a way to change the authState without having access to this of the UI component:

<AmplifySignIn slot="sign-in">
                <span slot="secondary-footer-content">
                  <amplify-button
                    variant="anchor"
                    onClick={() => this.updateAuthState(AuthState.ForgotPassword)}
                  >
                    {"Forgot your password?"}
                  </amplify-button>
                </span>
</AmplifySignIn>

note: this here is my app this not the AmplifySignIn this, therefore I cannot write the same code than in https://github.com/aws-amplify/amplify-js/blob/main/packages/amplify-ui-components/src/components/amplify-sign-in/amplify-sign-in.tsx and especially I cannot do this.handleAuthStateChange(AuthState.ForgotPassword)

READ:

Today to read the current AuthState I have to listen onAuthUIStateChange and persist the information of the authState myself (in my app state for example). It would have been easier to have access to the authstate directly.

Describe the solution you’d like

I like to have a simple way to invoke a AuthState change from outside. I found a workaround (see alternative) but this is not ideal. Ideally I like to have dispatchAuthStateChangeEvent exported and usable from outside, as well as constants (in particular UI_AUTH_CHANNEL and AUTH_STATE_CHANGE_EVENT)

For the read, not sure what can be done? A static variable exposed? I tried using React Ref to access the information but it does not work.

Describe alternatives you’ve considered I use the following workaround (which is inelegant and not safe):

import { Hub } from '@aws-amplify/core';

class App extends React.Component {

updateAuthState(nextAuthState){
    Hub.dispatch('UI Auth', {
      event: 'AuthStateChange',   <--- here is the trick I hardcoded the CHANNEL values
      message: nextAuthState
    });
  }

render() {
    return (
       <AmplifyAuthenticator usernameAlias="email" style={{ textAlign: 'center' }} initialAuthState={AuthState.SignIn}>
           <AmplifySignIn usernameAlias="email" slot="sign-in">
              <span slot="secondary-footer-content">
                  <amplify-button
                    variant="anchor"
                    onClick={() => this.updateAuthState(AuthState.ForgotPassword)}
                  >
                    {"Forgot your password?"}
                  </amplify-button>
                </span>
              </AmplifySignIn>

Hardcoding constants is risky and inelegant.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
wlee221commented, Oct 12, 2021

Hello @xavierraffin, thank you for your detailed feature request! Looking over this now. This is something we want to support in our @next release as well!

Totally agreed on difficulty with reading and writing authState. Adding this to our new Authenticator GA milestone to track this.

On the new Authenticator, we’ll provide mechanisms that provide you access to readonly authState and helpers that’ll enable you to transition between different authStates. For React, it’ll likely be a hook:

const { authState, toForgotPassword } = useAuthenticator(); 

which is a one-liner instead of onAuthUIStateChange callback that we used to have!

Edit: didn’t read your thread well! Apologies.

2reactions
xavierraffincommented, Sep 23, 2020

An additional feature will be to have access to current Auth.state. This way it is simpler for the developer to hide/display custom components based on that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is the best way to access redux store outside a react ...
Export the store from the module you called createStore with. Then you are assured it will both be created and will not pollute...
Read more >
Make User State Globally Accessible in Next.js with React ...
In this video, we create a useUser hook that exposes the state of our Supabase user to our tree of React components. To...
Read more >
Authentication in React Native, Easy, Secure, and Reusable ...
A guide to build a generic solution that handles most of the authentication use cases and easy to copy-paste in your next project....
Read more >
ASP.NET Core Blazor authentication and authorization
Expose the authentication state as a cascading parameter ... If user.Identity.IsAuthenticated is true , claims can be enumerated and membership in ...
Read more >
TIFU by exposing my Pi to the outside world with default user ...
Left mine with an internet facing SSH port, 2 weeks later checked the auth.log files and I had over 200,000 connection attempts.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found