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.

WARNING onHubCapsule is Deprecated (React)

See original GitHub issue

I was using the Hub API with my React app to handle authentication. I just upgraded to the latest aws-amplify version and am now getting a warning in the console:

[WARN] 38:57.297 Hub - WARNING onHubCapsule is Deprecated. Please pass in a callback.

Note the three numbers at the beginning (i.e. 38:57:297) seem to be random. Every time I reload they are different. I don’t know what the message means by ‘pass in a callback’. Pass into where? And what should be in the callback?

Here’s my code:

const App = () => {
	const [user, setUser] = useState(null)

	const getUserData = async () => {
		const currentUser = await Auth.currentAuthenticatedUser()
		setUser(currentUser)
	}

	const hubObj = {
		onHubCapsule: capsule => {
			switch (capsule.payload.event) {
				case 'signIn':
					getUserData()
					break
				case 'signOut':
					setUser(null)
					break
				default:
					return
			}
		},
	}

	useEffectAsync(() => {
		getUserData()
		Hub.listen('auth', hubObj, 'onHubCapsule')
	}, [])

	const handleSignOut = async () => {
		try {
			await Auth.signOut({ global: true })
		} catch (err) {
			console.error('Error signing out user', err)
		}
	}

	return user ? (
		<AuthContext.Provider
			value={{ username: user.username, handleSignOut, user }}>
			<Router user={user} />
		</AuthContext.Provider>
	) : (
		<Authenticator theme={theme} />
	)
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ghostcommented, Apr 9, 2019

Hi thanks @medelman17 for sharing your solution for my code instead of:

const hubObj = {
		onHubCapsule: capsule => {
			switch (capsule.payload.event) {
				case 'signIn':
					getUserData()
					break
				case 'signOut':
					setUser(null)
					break
				default:
					return
			}
		},
	}

	useEffectAsync(() => {
		getUserData()
		Hub.listen('auth', hubObj, 'onHubCapsule')
	}, [])

I changed my code to:

useEffectAsync(() => {
		getUserData()

		Hub.listen('auth', data => {
			switch (data.payload.event) {
				case 'signIn':
					getUserData()
					break
				case 'signOut':
					setUser(null)
					break
				default:
					return
			}
		})
	}, [])

And now it’s working without the warning message.

1reaction
medelman17commented, Apr 8, 2019

Ok, but, for the record, it does not appear to be backwards compatible. Previously, I had this code which was working fine.

  onHubCapsule(capsule: AuthFlowHubEvent) {
    const { channel, payload } = capsule;
    if (channel === 'authflow') {
      this.handleAuthFlow(payload);
    }
  }

After the upgrade, I started to get this.handleAuthFlow is not a function errors. Changing to the new API solved it:

constructor(props: AuthenticatorProps) {
    super(props);
    Hub.listen('authflow', (data) => {
      const { payload } = data;
      this.handleAuthFlow(payload);
    });
  }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Utilities - Hub - JavaScript - AWS Amplify Docs
While still possible, this is no longer considered best practice and we have begun deprecating the method. Please define an explicit callback and...
Read more >
Authenticator | amplify-js
Defined in packages/aws-amplify-react-native/src/Auth/Authenticator.tsx:77 ... onHubCapsule(capsule: HubCapsule): any.
Read more >
Warning message "DeprecationWarning: Invalid 'main' field in ...
This warning message will only show on Node 16. You can safely ignore the warning, but to get rid of it you can...
Read more >
aws-amplify-react - UNPKG
This is deprecated ' +\n 'and will throw in the standalone `prop-types` package. ' +\n 'You may be seeing this warning due to...
Read more >
Announcing AWS Amplify JavaScript library version 5
Deprecated legacy UI packages (You can migrate to the new UI ... Get started with building JavaScript Web and React Native apps with...
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