Skip to content

Commit 32626f8

Browse files
committed
Fix esModuleInterop + update eslint rules.
1 parent 17322ed commit 32626f8

File tree

16 files changed

+798
-692
lines changed

16 files changed

+798
-692
lines changed

.eslintignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
**/node_modules/**
2+
**/dist/**
3+
**/docs/**

.eslintrc

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,13 @@
22
"env": {
33
"node": true
44
},
5-
"parser": "@typescript-eslint/parser",
65
"parserOptions": {
7-
"project": "./tsconfig.json",
8-
"sourceType": "module"
6+
"project": "./tsconfig.json"
97
},
108
"ignorePatterns": ["dist/test/*", "typedoc.js"],
11-
"plugins": ["@typescript-eslint", "prefer-arrow", "import"],
12-
"extends": [
13-
"plugin:@typescript-eslint/eslint-recommended",
14-
"plugin:@typescript-eslint/recommended",
15-
"prettier",
16-
"prettier/@typescript-eslint"
17-
],
9+
"extends": ["standard-with-typescript", "prettier", "prettier/@typescript-eslint"],
1810
"root": true,
1911
"rules": {
20-
"indent": ["error", 4],
2112
"@typescript-eslint/interface-name-prefix": "off",
2213
"@typescript-eslint/explicit-function-return-type": "off",
2314
"@typescript-eslint/no-explicit-any": "off",

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ yarn.lock
1313
.vscode
1414
renovate.json
1515
.eslintrc
16+
.eslintignore
1617
.github
1718
*.tgz

package.json

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nestjs-authentication",
3-
"version": "0.1.1",
3+
"version": "0.1.3",
44
"description": "A nestjs module that support multiple authentications provider",
55
"author": "Alex Hermann <contact@pop-code.com>",
66
"repository": "https://github.com/Pop-Code/nestjs-authentication.git",
@@ -14,7 +14,7 @@
1414
"test:watch": "jest --watch",
1515
"test:cov": "jest --coverage",
1616
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest",
17-
"lint": "eslint ./src/**/*.ts",
17+
"lint": "eslint --ext .js,.ts,.jsx,.tsx .",
1818
"format": "prettier \"**/*.ts\" --ignore-path .prettierignore --write && git status"
1919
},
2020
"peerDependencies": {
@@ -28,31 +28,35 @@
2828
"passport-local": "~1.0.0"
2929
},
3030
"devDependencies": {
31-
"@nestjs/common": "7.0.9",
32-
"@nestjs/core": "7.0.9",
33-
"@nestjs/testing": "7.0.9",
31+
"@nestjs/common": "7.1.3",
32+
"@nestjs/core": "7.1.3",
33+
"@nestjs/testing": "7.1.3",
3434
"@nestjs/passport": "7.0.0",
35-
"@types/jest": "25.2.1",
36-
"@types/node": "13.13.4",
35+
"@types/jest": "25.2.3",
36+
"@types/node": "14.0.9",
3737
"@types/passport": "1.0.3",
38-
"@typescript-eslint/eslint-plugin": "2.31.0",
39-
"@typescript-eslint/parser": "2.31.0",
4038
"commander": "5.1.0",
39+
"@typescript-eslint/eslint-plugin": "3.1.0",
40+
"@typescript-eslint/parser": "3.1.0",
4141
"eslint": "6.8.0",
4242
"eslint-config-prettier": "6.11.0",
43+
"eslint-config-standard-with-typescript": "18.0.2",
4344
"eslint-plugin-import": "2.20.2",
45+
"eslint-plugin-node": "11.1.0",
4446
"eslint-plugin-prefer-arrow": "1.2.1",
45-
"jest": "25.5.4",
46-
"nestjs-console": "3.0.4",
47+
"eslint-plugin-promise": "4.2.1",
48+
"eslint-plugin-standard": "4.0.1",
49+
"jest": "26.0.1",
50+
"nestjs-console": "3.0.5",
4751
"passport": "0.4.1",
4852
"prettier": "2.0.5",
4953
"reflect-metadata": "0.1.13",
5054
"rxjs": "6.5.5",
51-
"ts-jest": "25.4.0",
52-
"ts-node": "8.10.1",
55+
"ts-jest": "26.1.0",
56+
"ts-node": "8.10.2",
5357
"tsconfig-paths": "3.9.0",
54-
"typedoc": "0.17.6",
55-
"typescript": "3.8.3"
58+
"typedoc": "0.17.7",
59+
"typescript": "3.9.3"
5660
},
5761
"jest": {
5862
"moduleFileExtensions": [

src/controller.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export abstract class AuthController<LoginRequest extends ILoginRequest> {
1717
*/
1818
loginAction(req: any, namespace = 'user.ops', redirect?: string): any {
1919
const user: any = req.user;
20-
if (user && user.namespace !== namespace) {
20+
if (typeof user === 'object' && user.namespace !== namespace) {
2121
req.logout();
2222
}
2323
return { data: { namespace, user: req.user, redirect } };
@@ -30,7 +30,7 @@ export abstract class AuthController<LoginRequest extends ILoginRequest> {
3030
*/
3131
loginCheckAction(data: LoginRequest, request: any): any {
3232
const response = { success: true };
33-
if (!request.user) {
33+
if (typeof request.user !== 'object') {
3434
response.success = false;
3535
}
3636
if (request.get('accept') === 'application/json') {
@@ -45,7 +45,7 @@ export abstract class AuthController<LoginRequest extends ILoginRequest> {
4545
* @param req The http request (@Req|@Request)
4646
*/
4747
logoutAction(req: any): any {
48-
if (req.isAuthenticated()) {
48+
if (req.isAuthenticated() !== true) {
4949
req.logOut();
5050
}
5151
if (req.get('accept') === 'application/json') {

src/guard.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { CanActivate, ExecutionContext, UnauthorizedException } from '@nestjs/common';
2-
import passport, { AuthenticateOptions } from 'passport';
2+
import * as passport from 'passport';
33

44
export const scopes = new Map();
55

6-
export interface IAuthGuardOptions extends AuthenticateOptions {
6+
export interface IAuthGuardOptions extends passport.AuthenticateOptions {
77
isAuth?: boolean;
88
strategy: string | string[];
99
}
@@ -17,7 +17,7 @@ export class AuthGuard implements CanActivate {
1717
if (typeof this.options.scope === 'string') {
1818
this.options.scope = [this.options.scope];
1919
}
20-
if (this.options.scope) {
20+
if (Array.isArray(this.options.scope)) {
2121
for (const scope of this.options.scope) {
2222
scopes.set(scope, scope);
2323
}
@@ -32,7 +32,7 @@ export class AuthGuard implements CanActivate {
3232
let [request, response, next] = [httpCtx.getRequest(), httpCtx.getResponse(), httpCtx.getNext()];
3333

3434
// support graphql context
35-
if (!request) {
35+
if (typeof request !== 'object') {
3636
request = context.getArgByIndex(2).request;
3737
response = request.res;
3838
next = (err) => {
@@ -41,27 +41,27 @@ export class AuthGuard implements CanActivate {
4141
}
4242

4343
for (const strategy of this.options.strategy) {
44-
if (strategy === 'local' && request.isAuthenticated()) {
44+
if (strategy === 'local' && request.isAuthenticated() === true) {
4545
return true;
4646
} else {
47-
await new Promise((ok, fail) =>
47+
await new Promise((resolve, reject) =>
4848
passport.authenticate(strategy, this.options, async (err, user, info) => {
4949
request.authInfo = info;
5050
try {
51-
if (!user && this.options.isAuth) {
51+
if (typeof user !== 'object' && this.options.isAuth) {
5252
throw new Error('User not found');
5353
}
54-
if (err) {
54+
if (err !== undefined || err !== null) {
5555
throw err;
5656
}
57-
if (user) {
57+
if (typeof user === 'object') {
5858
await this.login(user, request);
5959
}
60-
ok();
60+
resolve();
6161
} catch (e) {
6262
// TO DO we could simply return false here to let the canActivate do the job
6363
// And just Log the error
64-
fail(new UnauthorizedException(e.message.message || e.message || 'Uncaught error'));
64+
reject(new UnauthorizedException(typeof e.message === 'string' ? e.message : 'Uncaught error'));
6565
}
6666
})(request, response, next)
6767
);
@@ -73,12 +73,12 @@ export class AuthGuard implements CanActivate {
7373

7474
async login<User>(user: any, request: any): Promise<User> {
7575
request.oauth2Scope = this.options.scope;
76-
await new Promise((ok, fail) => {
76+
await new Promise((resolve, reject) => {
7777
request.logIn(user, (loginErr: Error) => {
78-
if (loginErr) {
79-
return fail(loginErr);
78+
if (loginErr instanceof Error) {
79+
return reject(loginErr);
8080
}
81-
ok();
81+
resolve();
8282
});
8383
});
8484
return user;

src/interfaces/encrypt.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export interface IEncrypt {
2-
encrypt(value: string): string;
2+
encrypt: (value: string) => string;
33
}
44
export interface IEncryptOptions {
55
iv: string;

src/interfaces/provider.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
export interface IUserProvider<U = any> {
2-
findOne(data: any): Promise<U>;
2+
findOne: (data: any) => Promise<U>;
33
}
44

55
export interface IAuthProvider<U = any> {
6-
loadUser(data: any): U | Promise<U>;
7-
getName(): string;
6+
loadUser: (data: any) => U | Promise<U>;
7+
getName: () => string;
88
}

src/module.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { Global, Module, NestModule, MiddlewareConsumer, DynamicModule } from '@nestjs/common';
1+
import { DynamicModule, Global, MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
22
import { ConsoleModule } from 'nestjs-console';
3+
import * as passport from 'passport';
4+
5+
import { IAuthModuleAsyncOptions, IAuthModuleOptions } from './interfaces/options';
36
import { AuthService } from './service';
47
import { LocalStrategy } from './strategies/local';
58
import { UserSerializer } from './user.serializer';
6-
import passport from 'passport';
7-
import { IEncryptOptions } from './interfaces/encrypt';
8-
import { IAuthModuleOptions, IAuthModuleAsyncOptions } from './interfaces/options';
99

1010
@Global()
1111
@Module({})

src/providers/auth.provider.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,23 @@ export class AuthUserProvider<T extends IEmailPassword> implements IAuthProvider
1010
private readonly providerName: string,
1111
private readonly userProvider: IUserProvider<T>,
1212
private readonly encryptor: IEncrypt
13-
) {}
13+
) { }
1414

15-
async loadUser(data: { password: string; [key: string]: any }): Promise<T> {
15+
async loadUser(data: { password: string;[key: string]: any }): Promise<T> {
1616
const query = { ...data };
1717
delete query.password;
18-
const entity = await this.userProvider.findOne(query);
19-
if (!entity) {
20-
return;
21-
}
22-
if (entity && data.password) {
23-
const encrypted = this.encryptor.encrypt(data.password);
24-
if (encrypted !== entity.passwordEncrypted) {
18+
if (typeof data.password === 'string') {
19+
const entity = await this.userProvider.findOne(query);
20+
if (entity === undefined) {
2521
return;
22+
} else if (typeof data.password === 'string') {
23+
const encrypted = this.encryptor.encrypt(data.password);
24+
if (encrypted !== entity.passwordEncrypted) {
25+
return;
26+
}
2627
}
28+
return entity;
2729
}
28-
return entity;
2930
}
3031

3132
getName(): string {

0 commit comments

Comments
 (0)