-
-
Save denihs/9d15b0202365ee24c957432593c5bc98 to your computer and use it in GitHub Desktop.
Using 2FA in your Meteor app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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