Skip to content

Instantly share code, notes, and snippets.

@CaptainN
Last active March 12, 2020 21:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CaptainN/67b142c19481bedd08765c43cc4d2731 to your computer and use it in GitHub Desktop.
Save CaptainN/67b142c19481bedd08765c43cc4d2731 to your computer and use it in GitHub Desktop.
Meteor Robust useTracker
import { useTracker } from 'meteor/react-meteor-data'
// Hook, basic use, everything in one component
const MyProtectedPage = (pageId) => {
const { user, isLoggedIn, page } = useTracker(() => {
// The publication must also be secure
const subscription = Meteor.subscribe('page', pageId)
const page = Pages.findOne({ _id: pageId })
const user = Meteor.user()
const userId = Meteor.userId()
const isLoggingIn = Meteor.loggingIn()
return {
page,
isLoading: !subscription.ready(),
user,
userId,
isLoggingIn,
isLoggedIn: !!userId
}
}, [pageId])
if (!isLoggedIn) {
return <div>
<Link to="/register">Create an Account</Link>
<Link to="/login">Log in</Link>
</div>
}
return <div>
<h1>{page.title}</h1>
<p>{page.content}</p>
<a href="#" onClick={(e) => { e.preventDefault(); Meteor.logout(); }}>Log out ({user.username})</a>
</div>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment