The Auth Service Library for Angular is a streamlined and reusable authentication solution for Angular applications. Developed for internal use at our workplace, this library simplifies the process of integrating authentication into our Angular projects. By importing this library, developers can easily implement authentication features, ensuring consistency and reducing development time across multiple applications.
- Seamless Integration: Easily integrate authentication into Angular applications by importing this library.
- Standardized Authentication: Provides a consistent authentication flow across all projects.
- Configurable: Allows customization for different authentication needs and use cases.
To install the library, use the following npm command:
npm install @emilgramdk/ngx-auth-service
-
Setup / Configuration: Configure the library with your authentication settings.
import { ApplicationConfig } from "@angular/core"; import { AuthServiceConfig, provideAuthService, } from "@emilgramdk/ngx-auth-service"; import { provideHttpClient } from "@angular/common/http"; const authServiceConfig: AuthServiceConfig = { authURL: "https://auth.example.com", // URL to authenticate users baseURL: "https://example.com/app", // Base URL for the application storageKey: "authToken", // Token cookie name application: "default", // Application name sent to auth app apiSettings: { apiURL: "https://api.example.com", transformKeys: false, // remove first _ from keys in api response retryCount: 0, }, }; export const appConfig: ApplicationConfig = { providers: [ provideHttpClient(), // This is needed to send api request. provideAuthService(authServiceConfig), ], };
-
Using the Service in a Component: Inject the AuthService into your components.
import { AuthService } from "@emilgramdk/ngx-auth-service"; @Component({ selector: "app-root", templateUrl: "./app.component.html", }) export class AppComponent { constructor(public authService: AuthService) {} showUserInfo() { this.authService.showPopup("user"); } }
-
Using the Service in API Service: Inject the AuthService into your service.
import { RequestService } from "@emilgramdk/ngx-auth-service"; @Injectable({ providedIn: "root", }) export class APIService { private apiURL = config.apiURL; constructor(private requestService: RequestService) {} public async getAllUsers() { const route = "users"; return this.requestService .makeRequest<any>("GET", route) .then((response) => response.value); } }
This project is licensed under the MIT License.