0.1.0 • Published 1 year ago
passport-simple-webauthn v0.1.0
passport-simple-webauthn
This library makes it easier to implement webauthn authentication thanks to passport and SimpleWebAuthN.
It manages the verification part of SimpleWebAuthN, understanding how it works is needed.
List of content
Installation
# npm
npm install passport-simple-webauthn @simplewebauthn/server
npm install -D @simplewebauthn/typescript-types
# yarn
yarn add passport-simple-webauthn @simplewebauthn/server
yarn add -D @simplewebauthn/typescript-types
# pnpm
pnpm add passport-simple-webauthn @simplewebauthn/server
pnpm add -D @simplewebauthn/typescript-types
Usage
Setup strategy
import WebauthnStrategy from 'passport-simple-webauthn';
passport.use(
new WebauthnStrategy(
{
expectedRPID: 'example.com',
expectedOrigin: 'https://example.com',
extractor: (req) => req.session.webauthn,
requireUserVerification: true,
},
async (req, credentialId, userHandle) => {
const user = await User.findOne({ where: { id: userHandle } });
if (!user) {
throw new Error('User not found');
}
const authenticator = await Authenticator.findOne({
where: { credentialId, user: user.id },
});
return {
user,
authenticator,
};
},
async (req, user, registrationInfo, conclude) => {
try {
const newUser = await User.create(user, registrationInfo);
if (!newUser) {
conclude(new Error("Couldn't register user"));
}
conclude(null, newUser);
} catch (err) {
conclude(err);
}
},
),
);
Note: you can use
conclude
callback function or anasync
function or normal function with areturn
statement to return the data
Authenticate Requests
app.get('/profile', passport.authenticate('webauthn'), function (req, res) {
res.json(req.user);
});
Recipes
0.1.0
1 year ago