Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: nestjs/passport
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 8.1.1
Choose a base ref
...
head repository: nestjs/passport
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Loading
18 changes: 7 additions & 11 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -3,31 +3,28 @@ version: 2
aliases:
- &restore-cache
restore_cache:
key: dependency-cache-{{ checksum "package.json" }}
key: dependency-cache-{{ checksum "package.json" }}
- &install-deps
run:
name: Install dependencies
command: npm ci
name: Install dependencies
command: npm ci --ignore-scripts
- &build-packages
run:
name: Build
command: npm run build
name: Build
command: npm run build

jobs:
build:
working_directory: ~/nest
docker:
- image: circleci/node:17
- image: cimg/node:21.6
steps:
- checkout
- run:
name: Update NPM version
command: 'sudo npm install -g npm@latest'
- restore_cache:
key: dependency-cache-{{ checksum "package.json" }}
- run:
name: Install dependencies
command: npm ci
command: npm ci --ignore-scripts
- save_cache:
key: dependency-cache-{{ checksum "package.json" }}
paths:
@@ -44,4 +41,3 @@ workflows:
build-and-test:
jobs:
- build

1 change: 0 additions & 1 deletion .eslintignore

This file was deleted.

26 changes: 0 additions & 26 deletions .eslintrc.js

This file was deleted.

1 change: 1 addition & 0 deletions .husky/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_
1 change: 1 addition & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
npx --no-install commitlint --edit $1
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
npx --no-install lint-staged
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ Stack Overflow is a much better place to ask questions since:

To save your and our time, we will systematically close all issues that are requests for general support and redirect people to Stack Overflow.

If you would like to chat about the question in real-time, you can reach out via [our gitter channel][gitter].
If you would like to chat about the question in real-time, you can reach out via [our discord channel][discord].

## <a name="issue"></a> Found a Bug?
If you find a bug in the source code, you can help us by
@@ -244,7 +244,7 @@ changes to be accepted, the CLA must be signed. It's a quick process, we promise
[corporate-cla]: http://code.google.com/legal/corporate-cla-v1.0.html
[dev-doc]: https://github.com/nestjs/nest/blob/master/docs/DEVELOPER.md
[github]: https://github.com/nestjs/nest
[gitter]: https://gitter.im/nestjs/nest
[discord]: https://discord.gg/nestjs
[individual-cla]: http://code.google.com/legal/individual-cla-v1.0.html
[js-style-guide]: https://google.github.io/styleguide/jsguide.html
[jsfiddle]: http://jsfiddle.net
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<p align="center">
<a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo_text.svg" width="320" alt="Nest Logo" /></a>
<a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo-small.svg" width="120" alt="Nest Logo" /></a>
</p>

[travis-image]: https://api.travis-ci.org/nestjs/nest.svg?branch=master
@@ -12,9 +12,6 @@
<a href="https://www.npmjs.com/~nestjscore"><img src="https://img.shields.io/npm/v/@nestjs/core.svg" alt="NPM Version" /></a>
<a href="https://www.npmjs.com/~nestjscore"><img src="https://img.shields.io/npm/l/@nestjs/core.svg" alt="Package License" /></a>
<a href="https://www.npmjs.com/~nestjscore"><img src="https://img.shields.io/npm/dm/@nestjs/core.svg" alt="NPM Downloads" /></a>
<a href="https://travis-ci.org/nestjs/nest"><img src="https://api.travis-ci.org/nestjs/nest.svg?branch=master" alt="Travis" /></a>
<a href="https://travis-ci.org/nestjs/nest"><img src="https://img.shields.io/travis/nestjs/nest/master.svg?label=linux" alt="Linux" /></a>
<a href="https://coveralls.io/github/nestjs/nest?branch=master"><img src="https://coveralls.io/repos/github/nestjs/nest/badge.svg?branch=master#5" alt="Coverage" /></a>
<a href="https://discord.gg/G7Qnnhy" target="_blank"><img src="https://img.shields.io/badge/discord-online-brightgreen.svg" alt="Discord"/></a>
<a href="https://opencollective.com/nest#backer"><img src="https://opencollective.com/nest/backers/badge.svg" alt="Backers on Open Collective" /></a>
<a href="https://opencollective.com/nest#sponsor"><img src="https://opencollective.com/nest/sponsors/badge.svg" alt="Sponsors on Open Collective" /></a>
39 changes: 39 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// @ts-check
import eslint from '@eslint/js';
import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended';
import globals from 'globals';
import tseslint from 'typescript-eslint';

export default tseslint.config(
{
ignores: ['tests/**'],
},
eslint.configs.recommended,
...tseslint.configs.recommendedTypeChecked,
eslintPluginPrettierRecommended,
{
languageOptions: {
globals: {
...globals.node,
...globals.jest,
},
ecmaVersion: 5,
sourceType: 'module',
parserOptions: {
projectService: true,
tsconfigRootDir: import.meta.dirname,
},
},
},
{
rules: {
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-unsafe-assignment': 'off',
'@typescript-eslint/no-unsafe-call': 'off',
'@typescript-eslint/no-unsafe-member-access': 'off',
'@typescript-eslint/no-unsafe-function-type': 'off',
'@typescript-eslint/no-unsafe-argument': 'off',
'@typescript-eslint/no-unsafe-return': 'off',
},
},
);
5 changes: 3 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module.exports = {
moduleFileExtensions: ['js', 'json', 'ts'],
rootDir: '.',
testMatch: ['<rootDir>/test/*.e2e-spec.ts'],
testMatch: ['<rootDir>/test/**/*.e2e-spec.ts'],
transform: {
'^.+\\.ts$': 'ts-jest'
},
testEnvironment: 'node',
collectCoverage: true
collectCoverage: true,
collectCoverageFrom: ['lib/**/*.ts']
};
35 changes: 26 additions & 9 deletions lib/auth.guard.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/prefer-promise-reject-errors */
import {
CanActivate,
ExecutionContext,
@@ -21,22 +22,35 @@ export type IAuthGuard = CanActivate & {
logIn<TRequest extends { logIn: Function } = any>(
request: TRequest
): Promise<void>;
handleRequest<TUser = any>(err, user, info, context, status?): TUser;
getAuthenticateOptions(context): IAuthModuleOptions | undefined;
handleRequest<TUser = any>(
err: any,
user: any,
info: any,
context: ExecutionContext,
status?: any
): TUser;
getAuthenticateOptions(
context: ExecutionContext
): IAuthModuleOptions | undefined;
getRequest(context: ExecutionContext): any;
};

export const AuthGuard: (type?: string | string[]) => Type<IAuthGuard> =
memoize(createAuthGuard);

const NO_STRATEGY_ERROR = `In order to use "defaultStrategy", please, ensure to import PassportModule in each place where AuthGuard() is being used. Otherwise, passport won't work correctly.`;
const authLogger = new Logger('AuthGuard');

function createAuthGuard(type?: string | string[]): Type<CanActivate> {
function createAuthGuard(type?: string | string[]): Type<IAuthGuard> {
class MixinAuthGuard<TUser = any> implements CanActivate {
@Optional()
@Inject(AuthModuleOptions)
protected options: AuthModuleOptions;
protected options: AuthModuleOptions = {};

constructor(@Optional() options?: AuthModuleOptions) {
this.options = options ?? this.options;
if (!type && !this.options.defaultStrategy) {
new Logger('AuthGuard').error(NO_STRATEGY_ERROR);
authLogger.error(NO_STRATEGY_ERROR);
}
}

@@ -74,7 +88,9 @@ function createAuthGuard(type?: string | string[]): Type<CanActivate> {
): Promise<void> {
const user = request[this.options.property || defaultOptions.property];
await new Promise<void>((resolve, reject) =>
request.logIn(user, (err) => (err ? reject(err) : resolve()))
request.logIn(user, this.options, (err) =>
err ? reject(err) : resolve()
)
);
}

@@ -92,11 +108,12 @@ function createAuthGuard(type?: string | string[]): Type<CanActivate> {
}
}
const guard = mixin(MixinAuthGuard);
return guard;
return guard as Type<IAuthGuard>;
}

const createPassportContext =
(request, response) => (type, options, callback: Function) =>
(request: any, response: any) =>
(type: string | string[], options: any, callback: Function) =>
new Promise<void>((resolve, reject) =>
passport.authenticate(type, options, (err, user, info, status) => {
try {
@@ -105,5 +122,5 @@ const createPassportContext =
} catch (err) {
reject(err);
}
})(request, response, (err) => (err ? reject(err) : resolve()))
})(request, response, (err: any) => (err ? reject(err) : resolve()))
);
2 changes: 1 addition & 1 deletion lib/interfaces/auth-module.options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ModuleMetadata, Type } from '@nestjs/common';

export interface IAuthModuleOptions<T = any> {
export interface IAuthModuleOptions {
defaultStrategy?: string | string[];
session?: boolean;
property?: string;
10 changes: 10 additions & 0 deletions lib/interfaces/type.interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
export interface Type<T = any> extends Function {
new (...args: any[]): T;
}

export type WithoutCallback<T extends any[]> =
// T extends any makes this distributive
T extends any
? T extends [...infer U, infer C]
? C extends Function
? U
: T
: T
: never;
4 changes: 2 additions & 2 deletions lib/passport/passport.serializer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as passport from 'passport';

export abstract class PassportSerializer {
abstract serializeUser(user: any, done: Function);
abstract deserializeUser(payload: any, done: Function);
abstract serializeUser(user: any, done: Function): any;
abstract deserializeUser(payload: any, done: Function): any;

constructor() {
const passportInstance = this.getPassportInstance();
Loading