An Angular-based web application for real-time chat. Users can register, login, create chat rooms, and exchange messages with other users.
- User Authentication: Register and login with JWT tokens
- Chat Rooms: Create and browse chat rooms
- Real-Time Messaging: Send and receive messages in chat rooms
- User-Friendly UI: Clean, responsive interface
- Token Management: Secure JWT token storage and handling
- Protected Routes: Authentication guard for chat pages
- Angular 17 - Modern web framework
- TypeScript - Type-safe JavaScript
- RxJS - Reactive programming
- Angular Forms - Form handling (FormsModule)
- HttpClient - HTTP communication
- CSS3 - Responsive styling
- Node.js 18+ and npm
- Angular CLI 17+
-
Navigate to the project directory:
cd chat-frontend -
Install dependencies:
npm install
-
Start the development server:
npm start
Or use:
ng serve
The application will be available at http://localhost:4200
npm run buildOutput will be in the dist/chat-frontend directory.
chat-frontend/
├── src/
│ ├── index.html
│ ├── main.ts
│ ├── styles.css
│ └── app/
│ ├── app.component.ts
│ ├── app.component.css
│ ├── app.routes.ts
│ ├── components/
│ │ ├── login/
│ │ │ ├── login.component.ts
│ │ │ ├── login.component.html
│ │ │ └── login.component.css
│ │ └── chat/
│ │ ├── chat.component.ts
│ │ ├── chat.component.html
│ │ └── chat.component.css
│ ├── services/
│ │ ├── auth.service.ts
│ │ └── chat.service.ts
│ ├── models/
│ │ ├── user.model.ts
│ │ └── chat.model.ts
│ └── guards/
│ └── auth.guard.ts
├── package.json
├── angular.json
├── tsconfig.json
├── tsconfig.app.json
└── README.md
The frontend communicates with two backend services:
- Base URL:
http://localhost:8000 - Endpoints:
POST /auth/register- Register new userPOST /auth/login- Login and get JWT tokenGET /auth/verify- Verify token validityGET /auth/me- Get current user info
- Base URL:
http://localhost:8080/api - Endpoints:
GET /rooms- Get all chat roomsPOST /rooms- Create new room (requires JWT)GET /messages?roomId=X- Get messages in a roomPOST /messages- Send a message (requires JWT)
- Click "Register" on the login page
- Enter username, email, and password
- Click "Register" button
- You'll be redirected to login
- Enter your username and password
- Click "Login" button
- You'll be redirected to the chat page
- Click "+ New Room" in the sidebar
- Enter room name and optional description
- Click "Create" button
- Select a room from the sidebar
- Type your message in the text area
- Press Enter or click "Send" button
- Your message will appear in the chat
Edit the service URLs in your components or services:
Auth Service URL (in auth.service.ts):
private authUrl = 'http://localhost:8000/auth';Chat Backend URL (in chat.service.ts):
private backendUrl = 'http://localhost:8080/api';-
Registration: User provides username, email, password
- Frontend sends to Auth Service
- Auth Service hashes password and stores user
-
Login: User provides username and password
- Frontend sends to Auth Service
- Auth Service validates and returns JWT token
- Frontend stores token in localStorage
-
Protected Requests: All chat operations
- Frontend includes JWT in
Authorization: Bearer <token>header - Backend validates token signature
- Request proceeds if token is valid
- Frontend includes JWT in
-
Token Storage: JWT stored in browser localStorage
- Persists across page refreshes
- Cleared on logout
The AuthService provides methods for token management:
// Save token after login
authService.saveToken(token);
// Get stored token
const token = authService.getToken();
// Check if user is authenticated
if (authService.isAuthenticated()) { ... }
// Get headers with token for API calls
const headers = authService.getAuthHeaders();
// Logout (clears token)
authService.logout();The application handles common errors:
- Invalid Credentials: "Invalid credentials" message on login
- User Exists: "User already exists" message during registration
- Network Errors: "Failed to load rooms/messages" alerts
- Token Expired: Auto-redirect to login if token is invalid
- JWT Authentication: Secure token-based authentication
- Password Hashing: Passwords hashed with bcrypt on auth service
- Protected Routes: AuthGuard prevents access to chat without login
- CORS: Configured for localhost development
- Secure Headers: Authorization headers sent with all protected requests
- Use HTTPS for all communications
- Update CORS allowed origins
- Use environment-specific configurations
- Implement token refresh mechanism
- Add rate limiting
- Secure JWT secret on backend
Port 4200 already in use?
ng serve --port 4201CORS errors?
- Ensure auth service is running on
http://localhost:8000 - Ensure chat backend is running on
http://localhost:8080 - Check CORS configuration in both backend services
Blank page after login?
- Check browser console for errors
- Verify token is being saved in localStorage
- Check network tab for API call failures
Messages not loading?
- Verify chat backend is running
- Check that room ID is valid
- Ensure JWT token is valid (not expired)
Can't create rooms?
- Verify you're logged in (token in localStorage)
- Check that auth service is running
- Verify chat backend is running
Example flow using the UI:
- Open
http://localhost:4200in browser - Register a new user (e.g., username:
testuser, email:[email protected]) - Login with those credentials
- Create a new room (e.g., "General")
- Send a message
- Open another browser/incognito window and repeat steps 2-3
- You should see the same rooms and messages from both users
- Messages are loaded once when a room is selected
- Auto-scroll to latest message implemented
- Responsive design works on mobile devices
- Lazy loading of routes with Angular Router
- Chrome/Edge 90+
- Firefox 88+
- Safari 14+
MIT