DEV Community

Tom Liang
Tom Liang

Posted on • Updated on


User validation with Passport.js and fp-ts

I had a first try on using fp-ts to do user validation with passport.js.

import argon2 from "argon2";
import * as E from "fp-ts/lib/Either";
import { flow, pipe } from "fp-ts/lib/function";
import * as O from "fp-ts/lib/Option";
import * as TE from "fp-ts/lib/TaskEither";
import * as passport from "passport";
import * as PassportLocal from "passport-local";
import logger from "./logger";
import {
} from "../db/entities/user_profile";

const verifyPassport: PassportLocal.VerifyFunction = (
    userName: string,
    password: string,
) => {
    const onUserFound = (optionUser: O.Option<UserProfile>) => {
        const onSome = (user: UserProfile) => () =>
                .verify(user.userPassword, password)
                .then((valid) =>
                        ? E.right(user)
                        : E.left(new Error(`invalid user name or password`))
        const onNone = () =>
            TE.left(new Error(`user name ${userName} doesn't exist`));
        return pipe(optionUser, O.match(onNone, onSome));
        UserProfileRepository.findOneByUserName /*returns TE.TaskEither<Error, O.Option<UserProfile>> */,
            ({ message }) => done(null, false, { message }),
            (user) => done(null, user)

Enter fullscreen mode Exit fullscreen mode

Oldest comments (0)

An Animated Guide to Node.js Event Loop

Node.js doesn’t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.