This project is an Authentication System built using NestJS, a progressive Node.js framework, and Prisma, an ORM for database management. The system includes a robust authentication mechanism to ensure secure access to the application.
- User Registration and Login
- Session-based Authentication (using Redis)
- Database Management with Prisma
- Email Notifications for Security Events:
- Account Deactivation
- Password Reset
- Password Change
- Account Verification
- NestJS: A framework for building efficient, reliable, and scalable server-side applications.
- Prisma: An ORM for database management, providing a type-safe API for interacting with the database.
- PostgreSQL: The database used for storing user and project data.
- Redis: Used for session management, providing fast and secure handling of user sessions.
- TypeScript: The primary language for development, providing type safety and modern JavaScript features.
- Node.js (v14 or higher)
- npm (v6 or higher)
- PostgreSQL
- Authenticator App (e.g., Google Authenticator, Authy) for Two-Factor Authentication (TOTP)
-
Clone the repository:
git clone https://github.com/ZiyaAkhundov/Nest-js-Authentication-system.git cd backend
-
Install the dependencies:
npm install
-
Set up the environment variables: Create a
.env
file in the root directory and add the following:NODE_ENV='development' APPLICATION_PORT=4000 APPLICATION_URL='http://localhost:${APPLICATION_PORT}' GRAPHQL_PREFIX='/graphql' ALLOWED_ORIGIN='http://localhost:3000' COOKIES_SECRET=your_secret__key SESSION_SECRET=your_secret__key SESSION_NAME='session' SESSION_DOMAIN='localhost' SESSION_MAX_AGE='30d' SESSION_HTTP_ONLY=true SESSION_SECURE=false SESSION_FOLDER='sessions:' POSTGRES_USER=your_postgres_name POSTGRES_PASSWORD=your_postgres_password POSTGRES_HOST='localhost' POSTGRES_PORT='5433' POSTGRES_DATABASE=your_database_name POSTGRES_URI='postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DATABASE}?schema=public' REDIS_USER=your_redis_name REDIS_PASSWORD=your_redis_password REDIS_HOST='localhost' REDIS_PORT='6379' REDIS_URI='redis://${REDIS_USER}:${REDIS_PASSWORD}@${REDIS_HOST}:${REDIS_PORT}' MAIL_HOST=your_email_host MAIL_PORT=your_email_port MAIL_LOGIN=your_email_login MAIL_PASSWORD=your_email_password
-
Run docker
docker-compose up --build
-
Run the Prisma set up the database schema:
npx prisma db push
-
Start the development server:
npm run start:dev
Instead of using JWT tokens, this system relies on Redis for session management. User sessions are stored in Redis, ensuring fast and secure handling of authentication states.
When enabling Two-Factor Authentication (TOTP), the system will generate a secret key for the user. This key should be added to an Authenticator App (e.g., Google Authenticator, Authy). The app will generate time-based one-time passwords that the user must provide alongside their regular login credentials to authenticate.
This document provides an overview of the GraphQL API schema based on the provided image. It includes the queries, mutations, models, and input types used in the API.
Queries allow fetching data from the server. The following queries are available:
- findCurrentSession: Retrieves the current session of the logged-in user.
- findProfile: Returns the profile details of the authenticated user.
- findSessionsByUser: Returns a list of active sessions for a given user.
- generateTotpSecret: Generates a secret key for enabling TOTP (Time-based One-Time Password).
Mutations are used to modify data. Below are the available mutations:
- clearSessionCookie: Clears the current session cookie.
- createUser(data: CreateUserInput!): Registers a new user.
- deactivateAccount(data: DeactivateAccountInput!): Deactivates an account and sends a confirmation email.
- disableTotp: Disables Two-Factor Authentication (TOTP).
- enableTotp(data: EnableTotpInput!): Enables TOTP authentication.
- loginUser(data: LoginInput!): Logs in a user and returns authentication details.
- logoutUser: Logs out the current user.
- newPassword(data: NewPasswordInput!): Sets a new password and sends a confirmation email.
- passwordChange(data: PasswordChangeInput!): Changes an existing password and sends a notification email.
- removeSession(id: String!): Removes an active session.
- resetPassword(data: ResetPasswordInput!): Sends a password reset request and an email with reset instructions.
- verifyAccount(data: VerificationInput!): Verifies a user's account and sends a confirmation email.
The system sends automatic email notifications to enhance security. Below are the cases when emails are sent:
- Account Deactivation: When a user deactivates their account, an email is sent to confirm the action.
- Password Reset: If a user requests a password reset, they receive an email with a reset link.
- Password Change: When a user successfully changes their password, they receive a confirmation email.
- Account Verification: After registration, users receive an email to verify their account.
The following models represent data structures used in queries and mutations:
Represents a user session.
type SessionModel {
createdAt: String!
id: ID!
metadata: SessionMetadataModel!
userId: String!
}
Represents a user profile.
type UserModel {
avatar: String
bio: String
createdAt: DateTime!
deactivateAt: DateTime
displayName: String!
email: String!
id: ID!
isDeactivated: Boolean!
isEmailVerified: Boolean!
isTotpEnabled: Boolean!
isVerified: Boolean!
updatedAt: DateTime!
username: String!
}
Represents authentication details returned after login.
type AuthModel {
message: String
user: UserModel
}
Represents a user's device information.
type DeviceModel {
browser: String!
os: String!
type: String!
}
Stores location details associated with a session.
type LocationModel {
city: String!
country: String!
latitude: Float!
longitude: Float!
}
Handles Two-Factor Authentication secrets.
type TotpModel {
qrcodeUrl: String!
secret: String!
}
Handles password change responses.
type PasswordChangeModel {
message: String
status: Boolean
}
Input types define structured data sent in mutations.
input CreateUserInput {
email: String!
password: String!
username: String!
}
input LoginInput {
login: String!
password: String!
pin: String
}
input DeactivateAccountInput {
email: String!
password: String!
pin: String
}
input EnableTotpInput {
pin: String!
secret: String!
}
input NewPasswordInput {
password: String!
passwordRepeat: String!
token: String!
}
input PasswordChangeInput {
currentPassword: String!
password: String!
passwordRepeat: String!
pin: String
}
input ResetPasswordInput {
email: String!
}
input VerificationInput {
token: String!
}
This document provides an overview of the GraphQL schema used in the project, detailing queries, mutations, models, and input types. The schema enables user authentication, session management, account verification, and security features like Two-Factor Authentication (TOTP) with email notifications for critical security actions.
This project is licensed under the MIT License. See the LICENSE file for details.