Skip to content

Commit

Permalink
feature(serializer) Adding request and refactor
Browse files Browse the repository at this point in the history
Passport has an undocumented feature, where the request can be passed to the serializeUser/deserializeUser callback as the first argument, and whether it is or not is determined by the arguments in the function (if it accepts 3, the first is the request). This feature is used to get and pass the request to the callback.

The done callback is removed in favor of a promise being expected. A rejected promise (or an exception thrown within the handler) will be passed to passport as an error.

Also added types for everything as generics, defaulting to the type safe "unknown" instead of "any", allowing extenders to enforce type safety on this lower level, instead of just theirs.

Also updated @types/passport to a version where this feature is present in the declarations.
  • Loading branch information
Vasil Rangelov committed Nov 28, 2019
1 parent cc1ae91 commit 1fb8611
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 20 deletions.
45 changes: 39 additions & 6 deletions lib/passport/passport.serializer.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,46 @@
import { IncomingMessage } from 'http';
import * as passport from 'passport';

export abstract class PassportSerializer {
abstract serializeUser(user: any, done: Function);
abstract deserializeUser(payload: any, done: Function);
export abstract class PassportSerializer<
UserType extends unknown = unknown,
PayloadType extends unknown = unknown,
RequestType extends IncomingMessage = IncomingMessage
> {
abstract serializeUser(
user: UserType,
req?: RequestType
): Promise<PayloadType>;
abstract deserializeUser(
payload: PayloadType,
req?: RequestType
): Promise<UserType>;

constructor() {
passport.serializeUser((user, done) => this.serializeUser(user, done));
passport.deserializeUser((payload, done) =>
this.deserializeUser(payload, done)
passport.serializeUser(
async (
req: RequestType,
user: UserType,
done: (err: unknown, payload?: PayloadType) => unknown
) => {
try {
done(null, await this.serializeUser(user, req));
} catch (err) {
done(err);
}
}
);
passport.deserializeUser(
async (
req: RequestType,
payload: PayloadType,
done: (err: unknown, user?: UserType) => unknown
) => {
try {
done(null, await this.deserializeUser(payload, req));
} catch (err) {
done(err);
}
}
);
}
}
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
{
"name": "@nestjs/passport",
"version": "6.1.0",
"description":
"Nest - modern, fast, powerful node.js web framework (@passport)",
"description": "Nest - modern, fast, powerful node.js web framework (@passport)",
"author": "Kamil Mysliwiec",
"license": "MIT",
"scripts": {
"build": "rm -rf dist && tsc -p tsconfig.json",
"build": "rimraf dist && tsc -p tsconfig.json",
"precommit": "lint-staged",
"prepublish:npm": "npm run build",
"publish:npm": "npm publish --access public"
Expand All @@ -18,16 +17,20 @@
"devDependencies": {
"@nestjs/common": "6.8.5",
"@types/node": "11.15.0",
"@types/passport": "1.0.1",
"@types/passport": "1.0.2",
"husky": "3.0.9",
"lint-staged": "9.4.2",
"passport": "0.4.0",
"prettier": "1.18.2",
"reflect-metadata": "0.1.13",
"rimraf": "^3.0.0",
"rxjs": "6.5.3",
"typescript": "3.6.4"
},
"lint-staged": {
"*.ts": ["prettier --write", "git add"]
"*.ts": [
"prettier --write",
"git add"
]
}
}

0 comments on commit 1fb8611

Please sign in to comment.