@next-nest-auth/nestauth 1.2.6 → 1.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/http-exception.filter.d.ts +4 -0
- package/dist/src/http-exception.filter.js +65 -0
- package/dist/src/http-exception.filter.js.map +1 -0
- package/dist/src/index.d.ts +5 -0
- package/dist/src/index.js +22 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/nestauth-facebook.guard.d.ts +7 -0
- package/dist/src/nestauth-facebook.guard.js +29 -0
- package/dist/src/nestauth-facebook.guard.js.map +1 -0
- package/dist/src/nestauth-facebook.strategy.d.ts +9 -0
- package/dist/src/nestauth-facebook.strategy.js +45 -0
- package/dist/src/nestauth-facebook.strategy.js.map +1 -0
- package/dist/src/nestauth-google.guard.d.ts +7 -0
- package/dist/src/nestauth-google.guard.js +29 -0
- package/dist/src/nestauth-google.guard.js.map +1 -0
- package/dist/src/nestauth-google.strategy.d.ts +9 -0
- package/dist/src/nestauth-google.strategy.js +44 -0
- package/dist/src/nestauth-google.strategy.js.map +1 -0
- package/dist/src/nestauth-jwt.guard.d.ts +7 -0
- package/dist/src/nestauth-jwt.guard.js +40 -0
- package/dist/src/nestauth-jwt.guard.js.map +1 -0
- package/dist/src/nestauth-jwt.strategy.d.ts +10 -0
- package/dist/src/nestauth-jwt.strategy.js +51 -0
- package/dist/src/nestauth-jwt.strategy.js.map +1 -0
- package/dist/src/nestauth-local.guard.d.ts +20 -0
- package/dist/src/nestauth-local.guard.js +20 -0
- package/dist/src/nestauth-local.guard.js.map +1 -0
- package/dist/src/nestauth-local.strategy.d.ts +14 -0
- package/dist/src/nestauth-local.strategy.js +42 -0
- package/dist/src/nestauth-local.strategy.js.map +1 -0
- package/dist/src/nestauth.controller.d.ts +16 -0
- package/dist/src/nestauth.controller.js +126 -0
- package/dist/src/nestauth.controller.js.map +1 -0
- package/dist/src/nestauth.interface.d.ts +36 -0
- package/dist/src/nestauth.interface.js +3 -0
- package/dist/src/nestauth.interface.js.map +1 -0
- package/dist/src/nestauth.module.d.ts +5 -0
- package/dist/src/nestauth.module.js +96 -0
- package/dist/src/nestauth.module.js.map +1 -0
- package/dist/src/nestauth.service.d.ts +14 -0
- package/dist/src/nestauth.service.js +72 -0
- package/dist/src/nestauth.service.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +61 -61
- package/src/nestauth-jwt.guard.ts +25 -27
- package/src/nestauth.controller.ts +69 -73
- package/src/nestauth.module.ts +91 -96
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
2
|
+
Request,
|
|
3
|
+
Controller,
|
|
4
|
+
Get,
|
|
5
|
+
Post,
|
|
6
|
+
UseGuards,
|
|
7
|
+
Body,
|
|
8
|
+
UnauthorizedException,
|
|
9
|
+
HttpStatus,
|
|
10
|
+
All,
|
|
11
|
+
BadRequestException,
|
|
12
|
+
UseFilters,
|
|
13
|
+
Inject,
|
|
14
14
|
} from "@nestjs/common";
|
|
15
15
|
import { NestAuthService } from "./nestauth.service";
|
|
16
16
|
import { NestAuthGoogleGuard } from "./nestauth-google.guard";
|
|
@@ -18,76 +18,72 @@ import { NestAuthFacebookGuard } from "./nestauth-facebook.guard";
|
|
|
18
18
|
import { HttpExceptionFilter } from "./http-exception.filter";
|
|
19
19
|
|
|
20
20
|
export function createDynamicController(
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
prefix: string,
|
|
22
|
+
nestAuthServiceToken: string,
|
|
23
|
+
localGuard: any,
|
|
24
24
|
) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
25
|
+
@UseFilters(HttpExceptionFilter)
|
|
26
|
+
@Controller(prefix)
|
|
27
|
+
class NestAuthController {
|
|
28
|
+
constructor(
|
|
29
|
+
@Inject(nestAuthServiceToken)
|
|
30
|
+
readonly nestAuthService: NestAuthService,
|
|
31
|
+
) {}
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
33
|
+
@All()
|
|
34
|
+
async greetings(): Promise<string> {
|
|
35
|
+
return "Welcome to NestAuth. Please check our documentation for more information.";
|
|
36
|
+
}
|
|
37
37
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
38
|
+
@UseGuards(localGuard)
|
|
39
|
+
@Post("login")
|
|
40
|
+
async login(@Request() req: any): Promise<any> {
|
|
41
|
+
// console.log("nestAuthServiceToken", nestAuthServiceToken);
|
|
42
|
+
return this.nestAuthService.login(req.user);
|
|
43
|
+
}
|
|
44
44
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
return this.nestAuthService.refreshToken(params.refresh_token);
|
|
53
|
-
}
|
|
45
|
+
@Post("refresh-token")
|
|
46
|
+
refreshToken(@Body() params: { refresh_token: string }): Promise<any> {
|
|
47
|
+
if (!params.refresh_token) {
|
|
48
|
+
throw new BadRequestException("Invalid or expired refresh token");
|
|
49
|
+
}
|
|
50
|
+
return this.nestAuthService.refreshToken(params.refresh_token);
|
|
51
|
+
}
|
|
54
52
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
53
|
+
@UseGuards(NestAuthGoogleGuard)
|
|
54
|
+
@Get("google")
|
|
55
|
+
async googleAuth(@Request() req: any): Promise<any> {}
|
|
58
56
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
57
|
+
@Get("google-redirect")
|
|
58
|
+
@UseGuards(NestAuthGoogleGuard)
|
|
59
|
+
googleAuthRedirect(@Request() req) {
|
|
60
|
+
if (!req.user) {
|
|
61
|
+
throw new UnauthorizedException("Unable to login with Google");
|
|
62
|
+
}
|
|
63
|
+
return this.nestAuthService.google(req.user);
|
|
64
|
+
}
|
|
67
65
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
66
|
+
@Get("/facebook")
|
|
67
|
+
@UseGuards(NestAuthFacebookGuard)
|
|
68
|
+
async facebookLogin(): Promise<any> {
|
|
69
|
+
return HttpStatus.OK;
|
|
70
|
+
}
|
|
73
71
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
return this.nestAuthService.facebook(req.user);
|
|
83
|
-
}
|
|
72
|
+
@Get("/facebook-redirect")
|
|
73
|
+
@UseGuards(NestAuthFacebookGuard)
|
|
74
|
+
async facebookLoginRedirect(@Request() req): Promise<any> {
|
|
75
|
+
if (!req.user) {
|
|
76
|
+
throw new UnauthorizedException("Unable to login with Facebook");
|
|
77
|
+
}
|
|
78
|
+
return this.nestAuthService.facebook(req.user);
|
|
79
|
+
}
|
|
84
80
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
81
|
+
@UseGuards(localGuard)
|
|
82
|
+
@Get("logout")
|
|
83
|
+
async logout(@Request() req: any) {
|
|
84
|
+
return req.logout();
|
|
90
85
|
}
|
|
86
|
+
}
|
|
91
87
|
|
|
92
|
-
|
|
88
|
+
return NestAuthController;
|
|
93
89
|
}
|
package/src/nestauth.module.ts
CHANGED
|
@@ -14,115 +14,110 @@ import { createLocalStrategy } from "./nestauth-local.strategy";
|
|
|
14
14
|
import { createLocalGuard } from "./nestauth-local.guard";
|
|
15
15
|
|
|
16
16
|
@Module({
|
|
17
|
-
|
|
17
|
+
imports: [PassportModule, ConfigModule.forRoot({})],
|
|
18
18
|
})
|
|
19
19
|
export class NestAuthModule {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
static forRoot(options: NestAuthModuleOptions): DynamicModule {
|
|
21
|
+
const JwtSecretProvider: Provider = {
|
|
22
|
+
provide: "JWT_SECRET",
|
|
23
|
+
useValue: options.jwtSecret || "60s",
|
|
24
|
+
};
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
26
|
+
const JwtExpiresInProvider: Provider = {
|
|
27
|
+
provide: "JWT_EXPIRES_IN",
|
|
28
|
+
useValue: options.jwtExpiresIn,
|
|
29
|
+
};
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
31
|
+
const JwtRefreshTokenExpiresInProvider: Provider = {
|
|
32
|
+
provide: "JWT_REFRESH_TOKEN_EXPIRES_IN",
|
|
33
|
+
useValue: options.jwtRefreshTokenExpiresIn,
|
|
34
|
+
};
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
const controllerPath = options.routePrefix
|
|
37
|
+
? `${options.routePrefix.replace(/^\/|\/$/g, "")}/nestauth`
|
|
38
|
+
: "nestauth";
|
|
39
39
|
|
|
40
|
-
|
|
40
|
+
const pathKey = controllerPath.replaceAll("/", "_").toUpperCase();
|
|
41
41
|
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
const userServiceToken = `NEST_AUTH_USER_SERVICE_${pathKey}`;
|
|
43
|
+
const nestAuthServiceToken = `NEST_AUTH_SERVICE_${pathKey}`;
|
|
44
44
|
|
|
45
|
-
|
|
45
|
+
const strategyName = `${pathKey}-local`;
|
|
46
46
|
|
|
47
|
-
|
|
48
|
-
strategyName,
|
|
49
|
-
userServiceToken,
|
|
50
|
-
);
|
|
47
|
+
const LocalStrategy = createLocalStrategy(strategyName, userServiceToken);
|
|
51
48
|
|
|
52
|
-
|
|
49
|
+
const LocalGuard = createLocalGuard(strategyName);
|
|
53
50
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
51
|
+
// console.log("controllerPath", controllerPath);
|
|
52
|
+
// console.log("pathKey", pathKey);
|
|
53
|
+
// console.log("userServiceToken", userServiceToken);
|
|
54
|
+
// console.log("nestAuthServiceToken", nestAuthServiceToken);
|
|
58
55
|
|
|
59
|
-
|
|
56
|
+
// console.log("------------------------------------------");
|
|
60
57
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
58
|
+
const controller = createDynamicController(
|
|
59
|
+
controllerPath,
|
|
60
|
+
nestAuthServiceToken,
|
|
61
|
+
LocalGuard,
|
|
62
|
+
);
|
|
66
63
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
LocalStrategy,
|
|
117
|
-
LocalGuard,
|
|
64
|
+
return {
|
|
65
|
+
module: NestAuthModule,
|
|
66
|
+
imports: [
|
|
67
|
+
JwtModule.registerAsync({
|
|
68
|
+
imports: [],
|
|
69
|
+
inject: [],
|
|
70
|
+
useFactory: async () => ({
|
|
71
|
+
secret: options.jwtSecret,
|
|
72
|
+
signOptions: {
|
|
73
|
+
expiresIn: options.jwtExpiresIn as StringValue | number,
|
|
74
|
+
},
|
|
75
|
+
}),
|
|
76
|
+
}),
|
|
77
|
+
forwardRef(() => options.UserModule),
|
|
78
|
+
],
|
|
79
|
+
providers: [
|
|
80
|
+
{
|
|
81
|
+
provide: userServiceToken,
|
|
82
|
+
useExisting: options.UserService,
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
provide: nestAuthServiceToken,
|
|
86
|
+
useFactory: (
|
|
87
|
+
jwtService: JwtService,
|
|
88
|
+
userService: NestAuthInterface,
|
|
89
|
+
jwtExpiresIn: StringValue | number,
|
|
90
|
+
jwtRefreshTokenExpiresIn: StringValue | number,
|
|
91
|
+
) =>
|
|
92
|
+
new NestAuthService(
|
|
93
|
+
jwtService,
|
|
94
|
+
userService,
|
|
95
|
+
jwtExpiresIn,
|
|
96
|
+
jwtRefreshTokenExpiresIn,
|
|
97
|
+
),
|
|
98
|
+
inject: [
|
|
99
|
+
JwtService,
|
|
100
|
+
userServiceToken,
|
|
101
|
+
"JWT_EXPIRES_IN",
|
|
102
|
+
"JWT_REFRESH_TOKEN_EXPIRES_IN",
|
|
103
|
+
],
|
|
104
|
+
},
|
|
105
|
+
NestAuthJwtStrategy,
|
|
106
|
+
NestAuthGoogleStrategy,
|
|
107
|
+
NestAuthFacebookStrategy,
|
|
108
|
+
JwtSecretProvider,
|
|
109
|
+
JwtExpiresInProvider,
|
|
110
|
+
JwtRefreshTokenExpiresInProvider,
|
|
111
|
+
LocalStrategy,
|
|
112
|
+
LocalGuard,
|
|
118
113
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
114
|
+
// {
|
|
115
|
+
// provide: APP_FILTER,
|
|
116
|
+
// useClass: HttpExceptionFilter,
|
|
117
|
+
// },
|
|
118
|
+
],
|
|
119
|
+
exports: [nestAuthServiceToken],
|
|
120
|
+
controllers: [controller],
|
|
121
|
+
};
|
|
122
|
+
}
|
|
128
123
|
}
|