@course-plateform/common
v1.1.2
Published
Common utilities, guards, decorators and constants for Udemy Clone
Downloads
827
Maintainers
Readme
@udemy/common
A shared package containing common utilities, guards, decorators, constants, and interfaces for the Course Platform microservices architecture.
Installation
npm install @udemy/commonFeatures
Guards
- RoleGuard: Authentication and authorization guard that validates JWT tokens and checks user roles and permissions
- Supports both REST and GraphQL contexts
Decorators
- @Auth(): Protects routes with required permissions
- @Roles(): Specifies required roles for route access
- @CurrentUser(): Injects the current user from the request context
Constants
- Enums: User roles, permissions, course levels, and request statuses
- Role-Permissions Mapping: Defines which permissions each role has
- Pagination: Default limit and page values
- Messages: Common error and validation messages
Interfaces
- Common TypeScript interfaces for user, JWT, and context objects
Usage
Basic Authentication
import { Auth, CurrentUser } from '@udemy/common';
@Resolver()
export class UserResolver {
@Query(() => User)
@Auth()
getProfile(@CurrentUser() user: any) {
return user;
}
}Role-Based Access
import { Roles, Role } from '@udemy/common';
@Resolver()
export class AdminResolver {
@Mutation(() => User)
@Roles(Role.ADMIN)
createUser(@Args('input') input: CreateUserInput) {
// Only ADMIN role can access this
}
}Permission-Based Access
import { Auth, Permission } from '@udemy/common';
@Resolver()
export class CourseResolver {
@Mutation(() => Course)
@Auth([Permission.CREATE_COURSE])
createCourse(@Args('input') input: CreateCourseInput) {
// Requires CREATE_COURSE permission
}
}Available Roles
- ADMIN: Full system access with all permissions
- INSTRUCTOR: Can create and manage courses
- USER: Basic user permissions for learning and purchases
Available Permissions
User Management
- UPDATE_USER, DELETE_USER, EDIT_USER_ROLE, VIEW_USER, CREATE_INSTRUCTOR
Authentication
- RESET_PASSWORD, CHANGE_PASSWORD, FORGOT_PASSWORD, LOGOUT
Category Management
- CREATE_CATEGORY, UPDATE_CATEGORY, DELETE_CATEGORY
Course Management
- CREATE_COURSE, UPDATE_COURSE, DELETE_COURSE
Request Management
- CREATE_REQUEST, UPDATE_REQUEST, DELETE_REQUEST, VIEW_REQUEST
Certificate Management
- CREATE_CERTIFICATE, VIEW_CERTIFICATE, DELETE_CERTIFICATE
Shopping Features
- CART operations: CREATE_CART, UPDATE_CART, DELETE_CART, VIEW_CART
- WISHLIST operations: CREATE_WISHLIST, DELETE_WISHLIST, VIEW_WISHLIST
- REVIEW operations: CREATE_REVIEW, UPDATE_REVIEW, DELETE_REVIEW
Configuration
The RoleGuard requires these environment variables:
JWT_SECRET=your-jwt-secret-keyDependencies
- @nestjs/common
- @nestjs/core
- @nestjs/graphql
- @nestjs/jwt
- @nestjs/passport
- nestjs-i18n
- passport
- passport-jwt
- typeorm
- reflect-metadata
Development
Building the Package
npm run buildWatch Mode
npm run devIntegration with Services
In your service module:
import { RoleGuard } from '@udemy/common';
@Module({
providers: [
{
provide: APP_GUARD,
useClass: RoleGuard,
},
],
})
export class AppModule {}Required Dependencies in Service:
Make sure your service has these dependencies installed:
npm install @nestjs/jwt @nestjs/passport passport-jwt nestjs-i18nError Messages
Common error messages included in the package:
- "User not found in request"
- "Password should be from 6 to 16 digits"
- "An error occurred"
Pagination
Default pagination constants:
import { LIMIT, PAGE } from '@udemy/common';
// LIMIT = 12
// PAGE = 1License
This package is part of the Course Platform microservices architecture.
