Skip to content

Instantly share code, notes, and snippets.

View PatrickKalkman's full-sized avatar

Patrick Kalkman PatrickKalkman

View GitHub Profile
@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);
@PatrickKalkman
PatrickKalkman / litetime.js
Created December 4, 2021 10:16
using implements to implement lifetime events
@Component({
selector: 'app-navigation',
templateUrl: './app-navigation.component.html',
styleUrls: ['./app-navigation.component.scss']
})
export class AppNavigationComponent implements OnInit, OnDestroy {
ngOnInit() {
}
ngOnDestroy() {
@PatrickKalkman
PatrickKalkman / movie-input.js
Created December 4, 2021 07:39
creating component input parameters
export class MovieDetailComponent implements OnInit {
@Input() public movieId: string;
@Input() public movieName: string;
@PatrickKalkman
PatrickKalkman / date-time-component.js
Created December 4, 2021 07:24
prefix component selector
@Component({
selector: 'app-date-time',
templateUrl: './date-time.component.html',
styleUrls: ['./date-time.component.scss']
})
export class DateTimeComponent { }