Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/dreancaste/TriviaPP/llms.txt

Use this file to discover all available pages before exploring further.

AuthService is the single point of contact for all identity operations in TriviaPP. It wraps the AWS Amplify v6 aws-amplify/auth primitives — signUp, confirmSignUp, signIn, signOut, getCurrentUser, and fetchAuthSession — behind a clean Angular service interface. It also holds the authenticated user’s email address in memory so that other services can read it synchronously via the userEmail getter without needing to hit Cognito on every call. For Amplify and Cognito project configuration, see the AWS Amplify Auth configuration guide.

Constructor / Dependencies

AuthService is provided in the root injector (providedIn: 'root') and has no constructor dependencies — it uses the top-level Amplify auth functions directly.
@Injectable({ providedIn: 'root' })
export class AuthService {
  private currentEmail: string = '';
  constructor() {}
}

Methods

register()

Registers a new user in the Cognito User Pool. Delegates to Amplify’s signUp(), passing the email as the username.
async register(email: string, password: string): Promise<SignUpOutput>
email
string
required
The user’s email address. Used as the Cognito username.
password
string
required
The account password. Must meet the Cognito password policy configured for the User Pool.
After a successful call, Cognito sends a confirmation code to the provided email. Pass that code to confirmarRegistro() to complete sign-up.

confirmarRegistro()

Confirms a pending registration using the six-digit code that Cognito emailed to the user.
async confirmarRegistro(email: string, codigo: string): Promise<ConfirmSignUpOutput>
email
string
required
The email address supplied during register().
codigo
string
required
The confirmation code received by email.
If the code has expired, Cognito can resend it via resendSignUpCode(). That utility is not wrapped by AuthService — call it directly from aws-amplify/auth if needed.

login()

Signs the user in with email and password. On success, caches the email in currentEmail so that userEmail can return it without an additional async call.
async login(email: string, password: string): Promise<SignInOutput>
email
string
required
The registered email address.
password
string
required
The account password.
The returned SignInOutput includes an isSignedIn boolean and a nextStep descriptor. If isSignedIn is false, the user may need to complete an MFA or new-password challenge described in nextStep.

logout()

Signs the user out globally and clears the in-memory currentEmail.
async logout(): Promise<void>
Calling logout() invalidates the Amplify session tokens locally. No parameters are required.

getCurrentUser()

Retrieves the currently authenticated Cognito user. Updates currentEmail from signInDetails.loginId on success; clears it and returns null if no session is active.
async getCurrentUser(): Promise<any | null>
This is the recommended way to restore session state when the app resumes (e.g., in AppComponent.ngOnInit). Returns null rather than throwing when the user is not signed in.

obtenerTokenJWT()

Fetches the current Amplify auth session and returns the raw string representation of the Cognito ID token, which can be forwarded as a Bearer token to backend services.
async obtenerTokenJWT(): Promise<string | null>
Returns null if the session cannot be fetched (unauthenticated, expired, network error).
The ID token contains the user’s Cognito claims (email, sub, groups, etc.). Prefer this over the access token when your API Gateway authorizer is configured for Cognito User Pools.

userEmail (getter)

Returns the in-memory cached email of the currently signed-in user. Does not make a network call.
get userEmail(): string
Returns an empty string '' when no user is signed in.

Usage Example

The snippet below shows the full registration → confirmation → login flow, followed by retrieving the JWT for an authenticated API call.
import { AuthService } from './services/auth.service';

@Component({ ... })
export class AuthPage {
  constructor(private auth: AuthService) {}

  // Step 1 — create the Cognito account
  async onRegister(email: string, password: string) {
    await this.auth.register(email, password);
    console.log('Check your inbox for a confirmation code.');
  }

  // Step 2 — confirm with the emailed code
  async onConfirm(email: string, code: string) {
    await this.auth.confirmarRegistro(email, code);
    console.log('Account confirmed — you can now log in.');
  }

  // Step 3 — sign in
  async onLogin(email: string, password: string) {
    const result = await this.auth.login(email, password);
    if (result.isSignedIn) {
      console.log('Logged in as:', this.auth.userEmail);
    }
  }

  // Step 4 — fetch a JWT for downstream API calls
  async callSecureApi() {
    const token = await this.auth.obtenerTokenJWT();
    if (!token) {
      console.warn('Not authenticated');
      return;
    }
    // Use token as Authorization: Bearer <token>
  }

  // Restore session on app init
  async ngOnInit() {
    const user = await this.auth.getCurrentUser();
    if (user) {
      console.log('Session restored for:', this.auth.userEmail);
    }
  }

  async onLogout() {
    await this.auth.logout();
  }
}

Build docs developers (and LLMs) love