nestjs-with-auth 1.0.0

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.
Files changed (50) hide show
  1. package/bin/cli.js +3 -0
  2. package/package.json +26 -0
  3. package/src/index.js +37 -0
  4. package/src/prompts.js +28 -0
  5. package/src/setup.js +70 -0
  6. package/templates/base/.env.example +21 -0
  7. package/templates/base/.prettierrc +4 -0
  8. package/templates/base/README.md +99 -0
  9. package/templates/base/eslint.config.mjs +35 -0
  10. package/templates/base/lib/prisma.ts +10 -0
  11. package/templates/base/nest-cli.json +8 -0
  12. package/templates/base/package-lock.json +12548 -0
  13. package/templates/base/package.json +92 -0
  14. package/templates/base/prisma/migrations/20260118151539_init/migration.sql +12 -0
  15. package/templates/base/prisma/migrations/migration_lock.toml +3 -0
  16. package/templates/base/prisma/schema.prisma +21 -0
  17. package/templates/base/prisma.config.ts +14 -0
  18. package/templates/base/src/app.controller.spec.ts +22 -0
  19. package/templates/base/src/app.controller.ts +12 -0
  20. package/templates/base/src/app.module.ts +15 -0
  21. package/templates/base/src/app.service.ts +8 -0
  22. package/templates/base/src/auth/auth.controller.spec.ts +18 -0
  23. package/templates/base/src/auth/auth.controller.ts +22 -0
  24. package/templates/base/src/auth/auth.module.ts +30 -0
  25. package/templates/base/src/auth/auth.service.spec.ts +18 -0
  26. package/templates/base/src/auth/auth.service.ts +31 -0
  27. package/templates/base/src/auth/constants/constants.ts +4 -0
  28. package/templates/base/src/auth/guards/jwt.guard.ts +22 -0
  29. package/templates/base/src/auth/guards/local.guard.ts +5 -0
  30. package/templates/base/src/auth/interfaces/jwt.interface.ts +7 -0
  31. package/templates/base/src/auth/interfaces/requestWithUser.interface.ts +8 -0
  32. package/templates/base/src/auth/strategies/jwt.strategy.ts +22 -0
  33. package/templates/base/src/auth/strategies/local.strategy.ts +22 -0
  34. package/templates/base/src/common/decorators/public.decorator.ts +4 -0
  35. package/templates/base/src/main.ts +24 -0
  36. package/templates/base/src/prisma/prisma.module.ts +8 -0
  37. package/templates/base/src/prisma/prisma.service.spec.ts +18 -0
  38. package/templates/base/src/prisma/prisma.service.ts +25 -0
  39. package/templates/base/src/user/dto/create-user.dto.ts +13 -0
  40. package/templates/base/src/user/dto/update-user.dto.ts +4 -0
  41. package/templates/base/src/user/entities/user.entity.ts +1 -0
  42. package/templates/base/src/user/user.controller.spec.ts +20 -0
  43. package/templates/base/src/user/user.controller.ts +36 -0
  44. package/templates/base/src/user/user.module.ts +12 -0
  45. package/templates/base/src/user/user.service.spec.ts +18 -0
  46. package/templates/base/src/user/user.service.ts +50 -0
  47. package/templates/base/test/app.e2e-spec.ts +25 -0
  48. package/templates/base/test/jest-e2e.json +9 -0
  49. package/templates/base/tsconfig.build.json +4 -0
  50. package/templates/base/tsconfig.json +21 -0
package/bin/cli.js ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+
3
+ require('../src/index.js');
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "nestjs-with-auth",
3
+ "version": "1.0.0",
4
+ "description": "nestjs boilerplate with passport integrated",
5
+ "license": "ISC",
6
+ "author": "sangnd2x",
7
+ "type": "commonjs",
8
+ "main": "index.js",
9
+ "bin": {
10
+ "nest-with-auth": "./bin/cli.js"
11
+ },
12
+ "files": [
13
+ "bin",
14
+ "src",
15
+ "templates"
16
+ ],
17
+ "scripts": {
18
+ "test": "echo \"Error: no test specified\" && exit 1"
19
+ },
20
+ "dependencies": {
21
+ "commander": "^11.0.0",
22
+ "prompts": "^2.4.2",
23
+ "fs-extra": "^11.1.1",
24
+ "chalk": "^4.1.2"
25
+ }
26
+ }
package/src/index.js ADDED
@@ -0,0 +1,37 @@
1
+ const {program} = require('commander');
2
+ const chalk = require('chalk');
3
+ const {setupProject} = require('./setup');
4
+ const {execSync} = require('child_process');
5
+
6
+ program
7
+ .argument('<project-name>', 'Name of the project')
8
+ .action(async (projectName) => {
9
+ console.log(chalk.blue(`Creating ${projectName}...\n`));
10
+
11
+ // Get user choices
12
+ // const choices = await getUserChoices();
13
+ //
14
+ // if (!choices.database || !choices.orm) {
15
+ // console.log(chalk.red('Setup cancelled'));
16
+ // process.exit(1);
17
+ // }
18
+
19
+ // Setup project
20
+ const projectPath = await setupProject(projectName);
21
+
22
+ // Install dependencies
23
+ console.log(chalk.blue('\nInstalling dependencies...'));
24
+ execSync('npm install', {cwd: projectPath, stdio: 'inherit'});
25
+
26
+ // Run Prisma init if needed
27
+ console.log(chalk.blue('\nGenerating Prisma client...'));
28
+ execSync('npx prisma generate', {cwd: projectPath, stdio: 'inherit'});
29
+
30
+ console.log(chalk.green(`\n✅ Project ${projectName} created successfully!`));
31
+ console.log(chalk.blue(`\nNext steps:`));
32
+ console.log(` cd ${projectName}`);
33
+ console.log(` cp .env.example .env # Configure your database`);
34
+ console.log(` npm run start:dev`);
35
+ });
36
+
37
+ program.parse();
package/src/prompts.js ADDED
@@ -0,0 +1,28 @@
1
+ const prompts = require('prompts');
2
+
3
+ async function getUserChoices() {
4
+ const questions = [
5
+ {
6
+ type: 'select',
7
+ name: 'database',
8
+ message: 'Which database do you want to use?',
9
+ choices: [
10
+ { title: 'PostgreSQL', value: 'postgresql' },
11
+ { title: 'MySQL', value: 'mysql' }
12
+ ]
13
+ },
14
+ {
15
+ type: 'select',
16
+ name: 'orm',
17
+ message: 'Which ORM do you want to use?',
18
+ choices: [
19
+ { title: 'Prisma', value: 'prisma' },
20
+ { title: 'TypeORM', value: 'typeorm' }
21
+ ]
22
+ }
23
+ ];
24
+
25
+ return await prompts(questions);
26
+ }
27
+
28
+ module.exports = { getUserChoices };
package/src/setup.js ADDED
@@ -0,0 +1,70 @@
1
+ const fs = require('fs-extra');
2
+ const path = require('path');
3
+
4
+ async function setupProject(projectName) {
5
+ const targetPath = path.join(process.cwd(), projectName);
6
+ const templatePath = path.join(__dirname, '../templates/base');
7
+
8
+ // Copy base template
9
+ console.log('Creating project...');
10
+ await fs.copy(templatePath, targetPath);
11
+
12
+ // Update package.json with ORM dependencies
13
+ const packageJsonPath = path.join(targetPath, 'package.json');
14
+ const packageJson = await fs.readJSON(packageJsonPath);
15
+
16
+ packageJson.name = projectName;
17
+
18
+ // Add ORM-specific dependencies
19
+ packageJson.dependencies['@prisma/client'] = '^5.0.0';
20
+ packageJson.devDependencies['prisma'] = '^5.0.0';
21
+
22
+ await fs.writeJSON(packageJsonPath, packageJson, {spaces: 2});
23
+
24
+ // Generate ORM config files
25
+ await generateOrmConfig(targetPath);
26
+
27
+ return targetPath;
28
+ }
29
+
30
+ async function generateOrmConfig(targetPath) {
31
+ // Create prisma schema
32
+ const prismaDir = path.join(targetPath, 'prisma');
33
+ await fs.ensureDir(prismaDir);
34
+
35
+ const schemaContent = `
36
+ datasource db {
37
+ provider = "postgresql"
38
+ url = env("DATABASE_URL")
39
+ }
40
+
41
+ generator client {
42
+ provider = "prisma-client-js"
43
+ }
44
+
45
+ model User {
46
+ id Int @id @default(autoincrement())
47
+ email String @unique
48
+ password String
49
+ }
50
+ `;
51
+
52
+ const prismaConfigContent = `
53
+ import 'dotenv/config';
54
+ import { defineConfig, env } from 'prisma/config';
55
+
56
+ export default defineConfig({
57
+ schema: 'prisma/schema.prisma',
58
+ migrations: {
59
+ path: 'prisma/migrations',
60
+ },
61
+ engine: 'classic',
62
+ datasource: {
63
+ url: env('DATABASE_URL'),
64
+ },
65
+ });`
66
+ await fs.writeFile(path.join(prismaDir, 'schema.prisma'), schemaContent.trim());
67
+ await fs.writeFile(path.join(targetPath, 'prisma.config.ts'), prismaConfigContent.trim());
68
+ }
69
+
70
+ module.exports = {setupProject};
@@ -0,0 +1,21 @@
1
+ # Node Environment
2
+ NODE_ENV=development
3
+
4
+ # Server Configuration
5
+ PORT=3000
6
+
7
+ # Database Configuration
8
+ DATABASE_HOST=
9
+ DATABASE_PORT=5432
10
+ DATABASE_DB=
11
+ DATABASE_USER=
12
+ DATABASE_PASSWORD=password
13
+ DATABASE_URL=postgresql://<DATABSE_USER>:<DATABASE_PASSWORD>@<DATABASE_HOST>:DATABASE_PORT/DATABASE_DB
14
+
15
+ # JWT Configuration
16
+ JWT_SECRET=89956c8e91769728afcc757375c04f980b42ac3c8498e6adb6467da6eddf9e11
17
+ JWT_EXPIRES_IN=7d
18
+
19
+ # API Configuration
20
+ API_PREFIX=api/v1
21
+ CORS_ORIGINS=http://localhost:3200,http://localhost:3000,http://localhost:3100
@@ -0,0 +1,4 @@
1
+ {
2
+ "singleQuote": true,
3
+ "trailingComma": "all"
4
+ }
@@ -0,0 +1,99 @@
1
+ <p align="center">
2
+ <a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo-small.svg" width="120" alt="Nest Logo" /></a>
3
+ </p>
4
+
5
+ [circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456
6
+ [circleci-url]: https://circleci.com/gh/nestjs/nest
7
+
8
+ <p align="center">A progressive <a href="http://nodejs.org" target="_blank">Node.js</a> framework for building efficient and scalable server-side applications.</p>
9
+ <p align="center">
10
+ <a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/v/@nestjs/core.svg" alt="NPM Version" /></a>
11
+ <a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/l/@nestjs/core.svg" alt="Package License" /></a>
12
+ <a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/dm/@nestjs/common.svg" alt="NPM Downloads" /></a>
13
+ <a href="https://circleci.com/gh/nestjs/nest" target="_blank"><img src="https://img.shields.io/circleci/build/github/nestjs/nest/master" alt="CircleCI" /></a>
14
+ <a href="https://coveralls.io/github/nestjs/nest?branch=master" target="_blank"><img src="https://coveralls.io/repos/github/nestjs/nest/badge.svg?branch=master#9" alt="Coverage" /></a>
15
+ <a href="https://discord.gg/G7Qnnhy" target="_blank"><img src="https://img.shields.io/badge/discord-online-brightgreen.svg" alt="Discord"/></a>
16
+ <a href="https://opencollective.com/nest#backer" target="_blank"><img src="https://opencollective.com/nest/backers/badge.svg" alt="Backers on Open Collective" /></a>
17
+ <a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://opencollective.com/nest/sponsors/badge.svg" alt="Sponsors on Open Collective" /></a>
18
+ <a href="https://paypal.me/kamilmysliwiec" target="_blank"><img src="https://img.shields.io/badge/Donate-PayPal-ff3f59.svg" alt="Donate us"/></a>
19
+ <a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://img.shields.io/badge/Support%20us-Open%20Collective-41B883.svg" alt="Support us"></a>
20
+ <a href="https://twitter.com/nestframework" target="_blank"><img src="https://img.shields.io/twitter/follow/nestframework.svg?style=social&label=Follow" alt="Follow us on Twitter"></a>
21
+ </p>
22
+ <!--[![Backers on Open Collective](https://opencollective.com/nest/backers/badge.svg)](https://opencollective.com/nest#backer)
23
+ [![Sponsors on Open Collective](https://opencollective.com/nest/sponsors/badge.svg)](https://opencollective.com/nest#sponsor)-->
24
+
25
+ ## Description
26
+
27
+ [Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.
28
+
29
+ ## Project setup
30
+
31
+ ```bash
32
+ $ npm install
33
+ ```
34
+
35
+ ## Compile and run the project
36
+
37
+ ```bash
38
+ # development
39
+ $ npm run start
40
+
41
+ # watch mode
42
+ $ npm run start:dev
43
+
44
+ # production mode
45
+ $ npm run start:prod
46
+ ```
47
+
48
+ ## Run tests
49
+
50
+ ```bash
51
+ # unit tests
52
+ $ npm run test
53
+
54
+ # e2e tests
55
+ $ npm run test:e2e
56
+
57
+ # test coverage
58
+ $ npm run test:cov
59
+ ```
60
+
61
+ ## Deployment
62
+
63
+ When you're ready to deploy your NestJS application to production, there are some key steps you can take to ensure it runs as efficiently as possible. Check out the [deployment documentation](https://docs.nestjs.com/deployment) for more information.
64
+
65
+ If you are looking for a cloud-based platform to deploy your NestJS application, check out [Mau](https://mau.nestjs.com), our official platform for deploying NestJS applications on AWS. Mau makes deployment straightforward and fast, requiring just a few simple steps:
66
+
67
+ ```bash
68
+ $ npm install -g mau
69
+ $ mau deploy
70
+ ```
71
+
72
+ With Mau, you can deploy your application in just a few clicks, allowing you to focus on building features rather than managing infrastructure.
73
+
74
+ ## Resources
75
+
76
+ Check out a few resources that may come in handy when working with NestJS:
77
+
78
+ - Visit the [NestJS Documentation](https://docs.nestjs.com) to learn more about the framework.
79
+ - For questions and support, please visit our [Discord channel](https://discord.gg/G7Qnnhy).
80
+ - To dive deeper and get more hands-on experience, check out our official video [courses](https://courses.nestjs.com/).
81
+ - Deploy your application to AWS with the help of [NestJS Mau](https://mau.nestjs.com) in just a few clicks.
82
+ - Visualize your application graph and interact with the NestJS application in real-time using [NestJS Devtools](https://devtools.nestjs.com).
83
+ - Need help with your project (part-time to full-time)? Check out our official [enterprise support](https://enterprise.nestjs.com).
84
+ - To stay in the loop and get updates, follow us on [X](https://x.com/nestframework) and [LinkedIn](https://linkedin.com/company/nestjs).
85
+ - Looking for a job, or have a job to offer? Check out our official [Jobs board](https://jobs.nestjs.com).
86
+
87
+ ## Support
88
+
89
+ Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support).
90
+
91
+ ## Stay in touch
92
+
93
+ - Author - [Kamil Myśliwiec](https://twitter.com/kammysliwiec)
94
+ - Website - [https://nestjs.com](https://nestjs.com/)
95
+ - Twitter - [@nestframework](https://twitter.com/nestframework)
96
+
97
+ ## License
98
+
99
+ Nest is [MIT licensed](https://github.com/nestjs/nest/blob/master/LICENSE).
@@ -0,0 +1,35 @@
1
+ // @ts-check
2
+ import eslint from '@eslint/js';
3
+ import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended';
4
+ import globals from 'globals';
5
+ import tseslint from 'typescript-eslint';
6
+
7
+ export default tseslint.config(
8
+ {
9
+ ignores: ['eslint.config.mjs'],
10
+ },
11
+ eslint.configs.recommended,
12
+ ...tseslint.configs.recommendedTypeChecked,
13
+ eslintPluginPrettierRecommended,
14
+ {
15
+ languageOptions: {
16
+ globals: {
17
+ ...globals.node,
18
+ ...globals.jest,
19
+ },
20
+ ecmaVersion: 5,
21
+ sourceType: 'module',
22
+ parserOptions: {
23
+ projectService: true,
24
+ tsconfigRootDir: import.meta.dirname,
25
+ },
26
+ },
27
+ },
28
+ {
29
+ rules: {
30
+ '@typescript-eslint/no-explicit-any': 'off',
31
+ '@typescript-eslint/no-floating-promises': 'warn',
32
+ '@typescript-eslint/no-unsafe-argument': 'warn'
33
+ },
34
+ },
35
+ );
@@ -0,0 +1,10 @@
1
+ import "dotenv/config";
2
+ import { PrismaPg } from '@prisma/adapter-pg'
3
+ import { PrismaClient } from '../generated/prisma/client'
4
+
5
+ const connectionString = `${process.env.DATABASE_URL}`
6
+
7
+ const adapter = new PrismaPg({ connectionString })
8
+ const prisma = new PrismaClient({ adapter })
9
+
10
+ export { prisma }
@@ -0,0 +1,8 @@
1
+ {
2
+ "$schema": "https://json.schemastore.org/nest-cli",
3
+ "collection": "@nestjs/schematics",
4
+ "sourceRoot": "src",
5
+ "compilerOptions": {
6
+ "deleteOutDir": true
7
+ }
8
+ }