How can I set a static login detail credentials in angular ?

Create a service to handle authentication:

import { Injectable } from '@angular/core';


@Injectable({

providedIn: 'root'

})

export class AuthService {

private readonly username = 'yourUsername';

private readonly password = 'yourPassword';


constructor() { }


authenticate(user: string, pass: string): boolean {

return user === this.username && pass === this.password;

}

}




Create a component to handle the login functionality:


import { Component } from '@angular/core';

import { AuthService } from './auth.service';


@Component({

selector: 'app-login',

template: `

<div>

<input type="text" [(ngModel)]="username" placeholder="Username">

<input type="password" [(ngModel)]="password" placeholder="Password">

<button (click)="login()">Login</button>

</div>

`

})

export class LoginComponent {

username: string;

password: string;


constructor(private authService: AuthService) { }


login(): void {

if (this.authService.authenticate(this.username, this.password)) {

// Successful login

console.log('Login successful!');

// Perform additional actions such as navigation, storing tokens, etc.

} else {

// Failed login

console.log('Invalid credentials!');

}

}

}



Use the LoginComponent in your application( Appcomponent.html )

<app-login></app-login>

Sign In or Register to comment.