【发布时间】:2020-11-16 03:35:00
【问题描述】:
正如我在 stackoverflow 中搜索的那样,一个类似的问题也浮出水面,这是我的
登录 OTP 组件:
onSubmitValidOTP() {
this.authenticationService.ValidOTP(this.fo.OtpControl.value, username, role)
.pipe(first())
.subscribe(
data => {
console.log(data);
},
error => {
console.log(error);
});
}
身份验证服务:
ValidOTP(OTP, UserName, type) {
return this.http.post<any>(`Staff/ValidateOTP`, { OTP, UserName, type })
.pipe(map(bool => {
return bool;
}));
}
员工总监:
[AllowAnonymous]
[HttpPost("ValidateOTP")]
internal IActionResult ValidOTP(string OTP, string UserName, string type)
{
bool Result = false;
if (type == "SuperAdmin")
{
Users _Users = _Context.Users.FirstOrDefault(j => j.Username == UserName);
if (_Users != null)
{
}
}
else
{
Staff _Staff = _Context.Staffs.FirstOrDefault(j => j.Username == UserName);
if (_Staff != null)
{
}
}
return Ok(new { Result = Result });
}
更新:
下面是我的 login.ts
import { AlertService, AuthenticationService } from '../_services';
import { first } from 'rxjs/operators';
@Component({
templateUrl: './login.html'
})
export class LoginComponent implements OnInit {
returnUrl: string;
constructor(private Router: Router,
private route: ActivatedRoute,
private router: Router,
private authenticationService: AuthenticationService,
) {
this.pageSettings.pageEmpty = true;
if (this.authenticationService.currentUserValue) {
this.router.navigate(['/']);
}
}
get f() { return this.loginForm.controls; }
onSubmit() {
this.authenticationService.login(this.f.LoginUserNameControl.value, this.f.PasswordUserNameControl.value)
.pipe(first())
.subscribe(
data => {
if (data.validIp) {
this.router.navigate([this.returnUrl]);
}
else {
this.FormSubmitBool = false;
this.VerifyOTP = true;
}
},
error => {
console.log(error);
this.alertService.error(error);
this.loading = false;
});
}
ValidOTP(OTP, UserName, type) {
return this.http.post<any>(`Staff/ValidateOTP`, { OTP, UserName, type })
.pipe(map(bool => {
return bool;
}));
它是我拥有的 login.ts 文件,我也对其进行了修剪,以便您在某种程度上更好地理解它。
更新 2:
下面是我的 authentication.service.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { BehaviorSubject, Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { User } from '../_models';
@Injectable({ providedIn: 'root' })
export class AuthenticationService {
private currentUserSubject: BehaviorSubject<User>;
public currentUser: Observable<User>;
constructor(private http: HttpClient) {
this.currentUserSubject = new BehaviorSubject<User>(JSON.parse(localStorage.getItem('currentUser')));
this.currentUser = this.currentUserSubject.asObservable();
}
public get currentUserValue(): User {
return this.currentUserSubject.value;
}
login(username, password) {
{ username, password })
`Users/authenticate`, { username, password })
return this.http.post<any>(`Staff/authenticate`, { username, password })
.pipe(map(user => {
localStorage.setItem('currentUser', JSON.stringify(user));
this.currentUserSubject.next(user);
return user;
}));
}
ValidOTP(OTP, UserName, type) {
return this.http.post<any>(`Staff/ValidateOTP`, { OTP, UserName, type })
.pipe(map(bool => {
return bool;
}));
}
logout() {
// remove user from local storage and set current user to null
localStorage.removeItem('currentUser');
this.currentUserSubject.next(null);
}
}
更新 3:
这是再次显示 console.log 错误的更新。
onSubmitValidOTP() {
let IsUpdated = this.authenticationService.ValidOTP(this.fo.OtpControl.value, JSON.parse(localStorage.getItem('currentUser')).username, JSON.parse(localStorage.getItem('currentUser')).role)
if (IsUpdated) {
console.log(IsUpdated);
IsUpdated
.pipe(first())
.subscribe(
data => {
console.log(data);
},
error => {
console.log(error);
});
}
else {
this.InvalidOtp = false;
}
}
【问题讨论】:
-
发布 login.ts 代码
-
@Sajeetharan 我已经更新了参考代码并对其进行了一些修改。
-
你能说详细点吗?您的服务获得 404。我认为您正试图在对象内部获取消息属性,例如
yourDataObject.message。 -
@karthicvel 我已经更新了 authentication.service.ts 以便帮助您了解我的情况。
-
您发布的代码没有尝试访问名为
message的属性的语句
标签: angular typescript asp.net-core rxjs angular8