Skip to content

Instantly share code, notes, and snippets.

@denihs
Last active February 17, 2022 18:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denihs/9d15b0202365ee24c957432593c5bc98 to your computer and use it in GitHub Desktop.
Save denihs/9d15b0202365ee24c957432593c5bc98 to your computer and use it in GitHub Desktop.
Using 2FA in your Meteor app
export const Component = () => {
const [qrCode, setQrCode] = useState(null);
return (
<div>
<button
onClick={() => {
Accounts.generate2faActivationQrCode(
"My app name",
(err, svg) => {
if (err) {
console.error("Error getting QR code", err);
return;
}
setQrCode(Buffer.from(svg).toString('base64'));
})
}}
>
Generate a new code
</button>
<img
width="20"
src={`data:image/svg+xml;base64,${qrCode}`}
/>
</div>
)
}
<button onClick={() => {
Meteor.loginWithPasswordAnd2faCode(username, password, code, error => {
if (error) {
console.error("Error trying to log in (user with 2fa)", error);
}
})}
}>
Validate and log in
</button>
<button
onClick={() => {
Meteor.loginWithPassword(username, password, error => {
if (error) {
if (error.error === 'no-2fa-code') {
// send user to a page or show a component
// where they can provide a 2FA code
setShouldAskCode(true);
return;
}
console.error("Error trying to log in (user without 2fa)", error);
}
});
}
}>
Login
</button>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment