Giter Club home page Giter Club logo

Comments (15)

kjellski avatar kjellski commented on July 28, 2024 5

Did this ever really make it into the package? I can't find it...

from amplify-ui.

undefobj avatar undefobj commented on July 28, 2024 4

Reopening cc @ericclemmons @sammartinez

from amplify-ui.

akeithly avatar akeithly commented on July 28, 2024 4

This would be particularly handy for those of us not using the UI Components. They already have the onAuthUIStateChange to work with but many of need to build custom login components.

from amplify-ui.

matt-d-webb avatar matt-d-webb commented on July 28, 2024 2

I'd love to know this too? I can't find any other references.

from amplify-ui.

ericclemmons avatar ericclemmons commented on July 28, 2024 1

@Ajit-Singh-Brar So I understand, you'd like a hook or provider for the Auth category that you can use on its own?

(This sounds related to #254)

The @next release introduces an Authenticator.Provider & useAuthenticator() hook for a truly customized experience (see: #532).

Until then, I've personally used the following useAuth() hook in my code:

import { Auth, Hub } from 'aws-amplify';

import { useEffect, useState } from 'react';

export default function useAuth() {
  const [isLoading, setIsLoading] = useState(true);
  const [user, setUser] = useState();

  const handleAuth = ({ payload }) => {
    switch (payload.event) {
      case 'signIn':
        return setUser(payload.data);
      case 'signOut':
        return setUser();
      default:
    }
  };

  useEffect(() => {
    Auth.currentAuthenticatedUser()
      .then(setUser)
      .catch(console.error)
      .then(() => setIsLoading(false));

    Hub.listen('auth', handleAuth);

    return () => Hub.remove('auth', handleAuth);
  }, []);

  return {
    Auth,
    isLoading,
    owner: user ? user.username : null,
    user
  };
}

It doesn't require a provider, so you would be able to use it like:

function App() {
  const { isLoading, user } = useAuth()

  if (isLoading) return "Loading..."

  if (user) {
    return <h1>Welcome {user.username}!</h1>
  }

  return <MyCustomSignInScreen />
}

from amplify-ui.

jordanranz avatar jordanranz commented on July 28, 2024

This seems like a necessary improvement. Definitely open to reviewing a PR.

I am currently working on an RFC to introduce web components with StencilJS. With this project we would ideally implement a similar design using Stencil's state tunnel. These components could then be used in any framework including the major ones we support.

from amplify-ui.

stale avatar stale commented on July 28, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

from amplify-ui.

stale avatar stale commented on July 28, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

from amplify-ui.

stale avatar stale commented on July 28, 2024

This issue has been automatically closed because of inactivity. Please open a new issue if are still encountering problems.

from amplify-ui.

brendonparker avatar brendonparker commented on July 28, 2024

Bump

from amplify-ui.

lebull avatar lebull commented on July 28, 2024

@VicStor Thanks a ton for the example implementation for this, using it in my codebase for now.

Why in the world does this still not come with the amplify react library? This should be a no-brainer.

from amplify-ui.

Ajit-Singh-Brar avatar Ajit-Singh-Brar commented on July 28, 2024

I was following the same template for the Provider pattern for AuthProvider. The following commented line:

useEffect(() => {
		Hub.listen('auth', ({ payload }) => {
			const { event, data } = payload
			if (event === 'signOut') {
				setAuth({ state: event, user: null })
				localStorage.deleteItem(AUTHENTICATOR_AUTHSTATE)
				return
			}
			localStorage.setItem(AUTHENTICATOR_AUTHSTATE, 'signedIn')
			setAuth({ state: event, user: data })    //     when this statement gets executed it is causing problem at my forgotPassword component which never shows me the ResetPassword component when the user receives the confirmation code.  
		})
	}, [])
That statement with comment, if it is actually commented then it is affecting the authState. When I am trying to perform the signIn. It gets authenticated but doesn't redirects to the page . Once refreshed it, it shows the actual page, until unless not. How can I solve this issue

from amplify-ui.

Ajit-Singh-Brar avatar Ajit-Singh-Brar commented on July 28, 2024

Hi @ericclemmons , can you help me with this issue to solve the problem which is happening on the Sign In and ForgotPassword component. If I am commenting the "setAuth" statement, the ForgotPassword works properly, but authentication on Sign IN, afetr signing In doesn't redirects to the actual page after login.

It will be very kind, if you help me out on this

from amplify-ui.

Ajit-Singh-Brar avatar Ajit-Singh-Brar commented on July 28, 2024

Thanks. It has solved my problem.

from amplify-ui.

ericclemmons avatar ericclemmons commented on July 28, 2024

That's great to hear! In the meantime, I'll close this as resolved via #532 and we'll get the documentation added for useAuthenticator as part of #552.

from amplify-ui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.