This project was generated with Angular CLI version 18.2.12.
This project is a basic Angular application demonstrating user authentication, component-based routing, and API communication. It includes auth/login, home, and admin components and uses HttpClientModule for API interactions and localStorage for token management.
Before starting, ensure you have the following installed on your system:
- Node.js: Download and install Node.js.
- Angular CLI: Install globally via npm:
npm install -g @angular/cli
- Create a new Angular application:
ng new my-app
- Navigate to the project directory:
cd my-app - Start the development server:
The application runs at
ng serve
http://localhost:4200.
- Open
src/app/app.component.html. - Remove existing content and add the following:
This serves as the entry point for other components.
<main class="main"> <router-outlet></router-outlet> </main>
- Create the
auth/logincomponent:ng generate component auth/login
- This component will handle user login.
- Create the
homecomponent for the index page:ng generate component home
- This will serve as the landing page.
- Create the
admincomponent for admin functionality:ng generate component admin
- This component will handle admin-related actions.
-
Create the routing configuration file
src/app/app.routes.ts:import { Routes } from "@angular/router"; export const routes: Routes = [ { path: "", async loadComponent() { const m = await import("./home/home.component"); return m.HomeComponent; }, }, { path: "login", async loadComponent() { const m = await import("./auth/login/login.component"); return m.LoginComponent; }, }, { path: "admin", async loadComponent() { const m = await import("./admin/admin.component"); return m.AdminComponent; }, }, { path: "**", redirectTo: "" }, // Redirect undefined routes to home ];
-
Import the routes into
AppModule:import { provideRouter } from "@angular/router"; import { routes } from "./app.routes"; bootstrapApplication(AppComponent, { providers: [provideRouter(routes)], }).catch((err) => console.error(err));
Import HttpClientModule into your project for API requests:
import { HttpClientModule } from "@angular/common/http";
@NgModule({
imports: [HttpClientModule],
})
export class AppModule {}Example usage of HttpClient for API interaction:
import { HttpClient } from "@angular/common/http";
import { Injectable } from "@angular/core";
@Injectable({
providedIn: "root",
})
export class AuthService {
constructor(private http: HttpClient) {}
login(credentials: { email: string; password: string }) {
return this.http.post("http://localhost:5000/api/v1/auth/login", credentials);
}
}Store the accessToken and user data in localStorage after login:
localStorage.setItem("accessToken", token);
localStorage.setItem("user", JSON.stringify(user));Retrieve the accessToken when making authenticated API requests:
const token = localStorage.getItem("accessToken");Clear tokens during logout:
localStorage.removeItem("accessToken");
localStorage.removeItem("user");ng serveng buildng testmy-app/
├── src/
│ ├── app/
│ │ ├── auth/
│ │ │ ├── login/
│ │ │ │ ├── login.component.ts
│ │ │ │ ├── login.component.html
│ │ │ │ ├── login.component.css
│ │ │ │ ├── login.module.ts
│ │ │ ├── register/
│ │ │ │ ├── register.component.ts
│ │ │ │ ├── register.component.html
│ │ │ │ ├── register.component.css
│ │ │ │ ├── register.module.ts
│ │ ├── home/
│ │ │ ├── home.component.html
│ │ │ ├── home.component.ts
│ │ ├── admin/
│ │ │ ├── admin.component.html
│ │ │ ├── admin.component.ts
│ │ ├── app.routes.ts
│ ├── index.html
├── package.json
├── README.mdFeel free to contribute to this project by creating pull requests or opening issues.
To get more help on the Angular CLI use ng help or go check out the Angular CLI Overview and Command Reference page.