Skip to main content

AuthRequest

Interface for authentication requests sent to the login and registration endpoints.

Interface Definition

export interface AuthRequest {
  username: string;
  password: string;
}

Properties

username
string
required
The username for authentication. Used for both login and registration.
password
string
required
The user’s password. Should be transmitted securely over HTTPS.

AuthResponse

Interface for authentication responses received after successful login.

Interface Definition

export interface AuthResponse {
  token: string;
  tipoToken: string;
  username: string;
  role: string;
}

Properties

token
string
required
The JWT authentication token used for subsequent API requests. Store this securely in localStorage.
tipoToken
string
required
The type of token, typically “Bearer” for JWT tokens.
username
string
required
The authenticated user’s username.
role
string
required
The user’s role in the system. Common values: ROLE_ADMIN, ROLE_USER.

Usage Examples

Login Request

import { AuthService } from './auth/auth.service';
import { AuthRequest, AuthResponse } from './core/models/auth.interface';

const loginData: AuthRequest = {
  username: '[email protected]',
  password: 'password123'
};

this.authService.login(loginData).subscribe({
  next: (response: AuthResponse) => {
    console.log('Token:', response.token);
    console.log('Role:', response.role);
    this.authService.saveSession(response);
  },
  error: (err) => {
    console.error('Login failed:', err);
  }
});

Registration Request

const registerData: AuthRequest = {
  username: '[email protected]',
  password: 'securepassword'
};

this.authService.register(registerData).subscribe({
  next: (message) => {
    console.log('Registration successful:', message);
  },
  error: (err) => {
    console.error('Registration failed:', err);
  }
});

Saving Session Data

saveSession(response: AuthResponse): void {
  // Save token using TokenStorageService
  this.tokenStorage.saveToken(response.token);
  
  // Save additional user data
  localStorage.setItem('username', response.username);
  localStorage.setItem('role', response.role);
}

Checking User Role

isAdmin(): boolean {
  return localStorage.getItem('role') === 'ROLE_ADMIN';
}

isUser(): boolean {
  return localStorage.getItem('role') === 'ROLE_USER';
}

Notes

Always transmit authentication credentials over HTTPS to ensure security.
Store tokens securely and never expose them in console logs in production environments.

Build docs developers (and LLMs) love