0
사용자 이름 "[email protected]"과 비밀번호 "Admin"으로 로그인 한 후 사용자가 로그인하여 홈 페이지로 리디렉션되도록 웹 사이트를 구현하려고합니다. 그러나 사용자가 지정된 값 이외의 값을 지정하면 웹 사이트에서 오류가 발생합니다.Angular 2의 관리자 인증
각도 2에 코드를 작성하여 어떻게 할 수 있습니까?
import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { AuthService } from '../../Services/auth.service';
@Component({
templateUrl: './login.component.html',
})
export class LoginComponent {
message: string;
constructor(public authService: AuthService, public router: Router) {
this.setMessage();
}
setMessage() {
this.message = 'Logged ' + (this.authService.isLoggedIn ? 'in' : 'out');
}
login() {
this.message = 'Trying to log in ...';
this.authService.login().subscribe(() => {
this.setMessage();
if (this.authService.isLoggedIn) {
// Get the redirect URL from our auth service
// If no redirect has been set, use the default
let redirect = this.authService.redirectUrl ? this.authService.redirectUrl : '/home';
// Redirect the user
this.router.navigate([redirect]);
}
});
}
logout() {
this.authService.logout();
this.setMessage();
}
}
그리고 내 login.component.html 파일은 다음과 같습니다 : 당신은 루트 가드를 사용할 수 있습니다
<h1>Please login to proceed</h1>
<br />
<br />
<form >
<table border="0">
<tr>
<td>
<div class="form-group">
<label for="uname" style="display: inline-block">Username:*  </label>
</div>
</td>
<td>
<div class="form-group">
<input style="display: inline-block" id="uname" type="text" class="form-control"
placeholder="Username">
</div>
</td>
</tr>
<tr>
<td>
<div class="form-group">
<label for="pwd" style="display: inline-block">Password:*  </label>
</div>
</td>
<td>
<div class="form-group">
<input style="display: inline-block" type="password" class="form-control"
placeholder="Password">
</div>
</td>
</tr>
<tr>
<td>
<p style="color: red">* Required Fields</p>
</td>
</tr>
<tr>
<td>
<p>
<button class="btn btn-info" (click)="login()" *ngIf="!authService.isLoggedIn">Login</button>
</p>
</td>
</table>
</form>
을 app.routes.ts 나를 리디렉션하고 모든 것이 잘 작동하면 사용자가 입력하는 입력을 "[email protected]"과 암호 "admin"과 비교하면됩니다. login.component.html에 어떻게 기록 할 수 있습니까? – Moana
양식 유효성 검사를 추가 할 수 있습니다. 하지만 사용자 이름과 암호를 하드 코딩해야합니다. 하지만 추천하지 않습니다. 또한 db를 만들고 값을 저장하고 각도로 통합하여 해당 데이터에 액세스 할 수 있습니다. –
당신은 사용자가 DB에 저장된 값과 비교 한 명령을 어떻게 쓸 수 있습니까? – Moana