-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(serializer) Adding request and refactor
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
Showing
3 changed files
with
56 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters