feat: initial signup route

This commit is contained in:
Wyatt Johnson
2018-07-10 13:56:20 -06:00
parent c0da4e97aa
commit c5dbe4659b
4 changed files with 41 additions and 27 deletions
@@ -1,4 +1,4 @@
import { NextFunction, Response } from "express";
import { NextFunction, RequestHandler, Response } from "express";
import { Db } from "mongodb";
import passport, { Authenticator } from "passport";
@@ -10,7 +10,7 @@ import { Request } from "talk-server/types/express";
export type VerifyCallback = (
err?: Error | null,
user?: User | null,
info?: object
info?: { message: string }
) => void;
export interface PassportOptions {
@@ -32,28 +32,33 @@ export function createPassport({
return auth;
}
export const handle = (
err: Error | null,
user: User | null
): RequestHandler => (req: Request, res, next) => {
if (err) {
// TODO: wrap error?
return next(err);
}
// Set the cache control headers.
res.header("Cache-Control", "private, no-cache, no-store, must-revalidate");
res.header("Expires", "-1");
res.header("Pragma", "no-cache");
// Send back the details!
// TODO: return the token instead of the user.
res.json({ user });
};
export const authenticate = (
authenticator: passport.Authenticator,
name: string
) => (req: Request, res: Response, next: NextFunction) =>
name: string,
options?: any
): RequestHandler => (req: Request, res, next) =>
authenticator.authenticate(
name,
{ session: false },
(err: Error | null, user: User | null) => {
if (err) {
// TODO: wrap error?
return next(err);
}
// Set the cache control headers.
res.header(
"Cache-Control",
"private, no-cache, no-store, must-revalidate"
);
res.header("Expires", "-1");
res.header("Pragma", "no-cache");
// Send back the details!
res.json({ user });
}
{ ...options, session: false },
(err: Error | null, user: User | null) => handle(err, user)(req, res, next)
)(req, res, next);
@@ -8,13 +8,12 @@ import {
} from "talk-server/models/user";
import { Request } from "talk-server/types/express";
async function verify(
db: Db,
const verifyFactory = (db: Db) => async (
req: Request,
email: string,
password: string,
done: VerifyCallback
) {
) => {
try {
// The tenant is guaranteed at this point.
const tenant = req.tenant!;
@@ -42,7 +41,7 @@ async function verify(
} catch (err) {
return done(err);
}
}
};
export interface LocalStrategyOptions {
db: Db;
@@ -56,6 +55,6 @@ export function createLocalStrategy({ db }: LocalStrategyOptions) {
session: false,
passReqToCallback: true,
},
verify.bind(null, db)
verifyFactory(db)
);
}