Skip to content

Instantly share code, notes, and snippets.

View iMichaelOwolabi's full-sized avatar
✌️
Impossible is Nothing!

Michael Owolabi iMichaelOwolabi

✌️
Impossible is Nothing!
View GitHub Profile
@iMichaelOwolabi
iMichaelOwolabi / pa-final-index.js
Created September 30, 2022 23:34
Final index.js code for the passwordless auth app
import express from 'express';
import { config } from 'dotenv';
import { authRouter } from './src/routes/authRouter.js';
config();
const app = express();
app.use(express.json());
const port = process.env.PORT ?? 3000;
@iMichaelOwolabi
iMichaelOwolabi / pa-auth-email-transporter.js
Created September 30, 2022 20:46
Email transporter code for passwordless auth app
import nodemailer from 'nodemailer';
import { config } from 'dotenv';
config()
export const emailSender = async (to, firstName, token) => {
const trasporter = await nodemailer.createTransport({
host: process.env.MAILGUN_SMTP_HOSTNAME,
port: process.env.SMTP_PORT,
secure: false,
@iMichaelOwolabi
iMichaelOwolabi / pa-auth-controller.js
Last active September 30, 2022 23:49
AUth controller functions for the passwordless auth
import { generateToken } from '../utils/jwtHelper.js';
import { userRepository } from '../repository/user.js';
import { emailSender } from '../utils/emailTransporter.js';
const createAccount = async (req, res) => {
try {
const { firstName, lastName, email, userName } = req.body;
// Fetch user information from Redis
const existingUser = await userRepository
@iMichaelOwolabi
iMichaelOwolabi / pa-authRouter.js
Created September 30, 2022 17:34
Routes file for the passwordless auth app
import { Router } from 'express';
import {
createAccount,
login,
verifyUser,
} from '../controllers/authController.js';
import { authGuard } from '../middleware/index.js';
const authRouter = Router();
@iMichaelOwolabi
iMichaelOwolabi / pa-user-repo.js
Created September 30, 2022 17:30
User repo sample code for the passwordless auth
import { Entity, Schema } from 'redis-om';
import { redisClient } from '../db/index.js';
class UserRepository extends Entity {}
const userSchema = new Schema(UserRepository, {
firstName: { type: 'string' },
lastName: { type: 'string' },
email: { type: 'string' },
userName: { type: 'string' },
@iMichaelOwolabi
iMichaelOwolabi / event-mngr-init-authController.js
Created July 4, 2022 06:05
Event manager auth controller code
import { ulid } from 'ulid';
import { hash, compare } from 'bcrypt';
import { redisClient } from '../db/index.js';
import { generateToken } from '../utils/jwtHelper.js';
const createAccount = async (req, res) => {
try {
const { firstName, lastName, email, password, displayName } = req.body;
const userId = ulid(); // Generate id for the user which will be used as the key in Redis
@iMichaelOwolabi
iMichaelOwolabi / redis-enterprise-dbConnect.js
Last active September 25, 2022 22:20
Redis enterprise connection file
import { config } from 'dotenv';
import { Client } from 'redis-om';
config();
const redisCloudConnectionString = `redis://${process.env.REDIS_DB_USER}:${process.env.REDIS_DB_PASS}@${process.env.REDIS_DB_URL}`;
const redisClient = new Client();
await redisClient.open(redisCloudConnectionString);
@iMichaelOwolabi
iMichaelOwolabi / event-mngr-init-app.js
Created July 3, 2022 20:59
init server setup event mngr
import express from 'express';
import { config } from 'dotenv';
config();
const app = express();
app.use(express.json());
const port = process.env.PORT ?? 3000;
@iMichaelOwolabi
iMichaelOwolabi / nonEventLoopBlockingFileReading.js
Created February 13, 2022 20:07
How to read file in Node.js in a way that it doesn't block the event loop code sample
// A better way of reading file that doesn't block the event loop
const fs = require('fs');
fs.readFile('countries.csv', (error, data) => {
if (error) {
throw new Error(error);
}
console.log(data.toString());
});
// Other JavaScript code below this line will not be blocked which is good.
@iMichaelOwolabi
iMichaelOwolabi / eventLoopBlockingFileREading.js
Last active February 13, 2022 20:08
Sample event loop blocking code
/**
* Event loop blocking code sample
*/
const fs = require('fs');
const countries = fs.readFileSync('countries.csv');
console.log(countries.toString());
// Other JavaScript code below this line will be blocked until the file reading completes.
console.log('Other JAVASCRIPT THAT IS BLOCKED');