This is an example of using BankID with server-side JavaScript and OIDC code flow.
- Ensure you have NodeJS v14 or newer
- Ensure you have Yarn installed (can be substituted with NPM)
- Navigate into the nodejs directory
- Run
yarn installto install dependencies - Run
yarn start:loginoryarn start:kycto start example applications
login.js uses Express HTTP server with Passport authentication middleware and openid-client. This example uses authentication only and does not call userinfo or profile EndPoints. This shows how to use the Connect product on it's own.
Make sure to update CLIENT_ID and CLIENT_SECRET with values provided to you through the BankID developer portal.
const CLIENT_ID = '72fda011-0479-4a4c-9fff-0a6c7f584e1e';
const CLIENT_SECRET =
'TsGlMQro488YSwc0h9NWydZAqHir13PPW2cDMEQBcLgFvaGOnXzOt9MWBhTDBEU7PaXtn9H7Y0QHdcVZolJOsg';
const SCOPE = 'openid';
const bankidIssuer = await Issuer.discover('https://oidc.sandbox.bankid.cz/');
const bankidClient = new bankidIssuer.Client({
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
redirect_uris: ['http://localhost:3000/'],
response_types: ['code'],
id_token_signed_response_alg: 'PS512',
});Authorized routes are protected with passport.authenticate('bankid') and sub from id_token is available via req.user
app.get('/', passport.authenticate('bankid'), (req, res) => res.json(req.user));You can try this example by running yarn start:login in the nodejs directory and navigating to https://localhost:3000/.
This example is the same as the Login example, but it calls /userinfo and /profile EndPoints (documentation) during the authentication process.
async (tokenSet, done) => {
const userinfo = await bankidClient.userinfo(tokenSet.access_token);
const profile = await axios.get(bankidIssuer.profile_endpoint, {
headers: { Authorization: 'Bearer ' + tokenSet.access_token },
});
return done(null, { userinfo, profile: profile.data });
};You can try this example by running yarn start:identify in the nodejs directory and navigating to https://localhost:3000/.