Skip to content

Instantly share code, notes, and snippets.

View PatrickKalkman's full-sized avatar
🏠
Working from home

Patrick Kalkman PatrickKalkman

🏠
Working from home
View GitHub Profile
@PatrickKalkman
PatrickKalkman / store-validate-token.js
Created January 15, 2022 14:42
The validate token method in the Vuex store
validateToken({ commit }, credentials) {
return apiClient
.post('/user/validatetoken', credentials)
.then(({ data }) => {
commit('SET_TWOFACTOR_LOGIN', data.validated);
});
},
@PatrickKalkman
PatrickKalkman / validate-token.js
Created January 15, 2022 14:29
The validateToken method of the LoginUser.vue component
validateToken() {
this.$store
.dispatch('validateToken', {
token: this.token,
})
.then(() => {
if (this.$store.state.twofactorvalidated) {
this.$router.push({ name: 'dashboard' });
} else {
this.error = 'The provided token was not valid, please try again';
@PatrickKalkman
PatrickKalkman / login-template.js
Created January 15, 2022 14:18
template part of the login vue component
<template>
<div>
<h3>{{ title }}</h3>
<form v-if="!showTwoFactorPanel" @submit.prevent="login">
<label for="email"> Email: </label>
<input v-model="email" type="email" name="email" value />
<label for="password"> Password: </label>
<input v-model="password" type="password" name="password" value />
<button type="submit" name="button">Login</button>
<p>{{ error }}</p>
@PatrickKalkman
PatrickKalkman / validate-token.js
Created January 15, 2022 13:50
Validate token when logging in
userController.validateToken = function (req, reply) {
tokenVerification.extractAndVerifyJwtToken(req, (err, isValidJwtToken, email) => {
if (!err && isValidJwtToken) {
const user = db.getUser(email);
if (typeof user !== 'undefined') {
const validated = speakeasy.totp.verify({
secret: user.secret,
encoding: 'base32',
token: req.body.token,
});
<template>
<div>
<h1>Dashboard</h1>
<template v-if="!isLoading">
<CustomerCard
v-for="customer in customers"
:key="customer.id"
:customer="customer"
/>
</template>
@PatrickKalkman
PatrickKalkman / user-controller-enable-2fa-step2.js
Created January 9, 2022 19:08
Enabling two factor authentication step 2
userController.enableTwoFactorAuthStep2 = function (req, reply) {
tokenVerification.extractAndVerifyToken(req, (err, isValidJwtToken, email) => {
if (!err && isValidJwtToken) {
const user = db.getUser(email);
if (typeof user !== 'undefined') {
log.info(req.body);
const base32secret = req.body.base32;
const userToken = req.body.token;
const verified = speakeasy.totp.verify({ secret: base32secret, encoding: 'base32', token: userToken });
if (verified) {
@PatrickKalkman
PatrickKalkman / enable-2fa-step1-response.json
Last active January 9, 2022 18:56
Enable two factor authentication step 1 response
{
"qr": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAALQAAAC0CAYAAAA9zQYyAAAAAklEQVR4Aew==",
"secret": {
"ascii": "WMi%T54iXy@cx203AoOzBf0xD]$/mb@D",
"hex": "574d6925543534695879406378323033416f4f7a42663078445d242f6d624044",
"base32": "K5GWSJKUGU2GSWDZIBRXQMRQGNAW6T32IJTDA6CELUSC63LCIBCA",
"otpauth_url": "otpauth://totp/SecretKey?secret=K5GWSJKUGU2GSWDZIBRXQMRQGNAW6T32IJTDA6CELUSC63LCIBCA"
}
}
@PatrickKalkman
PatrickKalkman / user-controller-enable-2fa-step1.js
Last active January 9, 2022 19:08
Enabling two factor authentication step 1
userController.enableTwoFactorAuthStep1 = function (req, reply) {
tokenVerification.extractAndVerifyToken(req, (err, isValidToken, email) => {
if (!err && isValidToken) {
const secret = speakeasy.generateSecret();
qrcode.toDataURL(secret.otpauth_url, function (err, qrImage) {
if (!err) {
reply.code(200).send({ qr: qrImage, secret: secret });
} else {
reply.internalServerError(err);
}
@PatrickKalkman
PatrickKalkman / user-controller-login.js
Last active January 9, 2022 16:06
First layer of authentication
userController.login = function (req, reply) {
if (isValidUserRequest(req)) {
let user = db.getCleanedUser(req.body.email);
if (typeof user !== 'undefined') {
if (bcrypt.compareSync(req.body.password, user.password)) {
delete user.password;
const token = jwt.sign(user, config.jwt.secret);
const newUser = { ...user, token };
reply.code(200).send(newUser);
}
@PatrickKalkman
PatrickKalkman / immutable.js
Created December 4, 2021 10:21
using immutable.js
const { Map } = require('immutable');
const map1 = Map({ a: 1, b: 2, c: 3 });
const map2 = map1.set('b', 50);