我正在使用 Angular 和Moralis创建 NFT 市场。
到目前为止,我在我的登录组件中使用这段代码来使用他们的元掩码扩展登录用户:
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { AuthService } from '@core/services';
import { environment } from '@environments/environment';
import { Moralis } from 'moralis';
export type User = Moralis.User<Moralis.Attributes>;
@Component({
selector: 'signin-page',
templateUrl: './signin-page.component.html',
styleUrls: ['./signin-page.component.scss'],
})
export class SigninPage implements OnInit {
user?: User;
constructor(private router: Router, private authService: AuthService) {}
ngOnInit(): void {
Moralis.start({
appId: environment.moralis.appId,
serverUrl: environment.moralis.serverUrl,
})
.then(() => console.info('Moralis has been initialised.'))
.finally(() => {
this.setLoggedInUser(Moralis.User.current());
this.authService.isAuthorized = true;
});
}
login(provider: 'metamask' | 'walletconnect' = 'metamask') {
(provider === 'metamask'
? Moralis.Web3.authenticate()
: Moralis.Web3.authenticate({ provider })
)
.then((loggedInUser) => {
this.setLoggedInUser(loggedInUser);
this.router.navigate(['dashboard']);
})
.catch((e) => console.error(`Moralis '${provider}' login error:`, e));
}
private setLoggedInUser(loggedInUser?: User) {
this.user = loggedInUser;
this.authService.changeUser(this.user);
this.authService.isAuthorized = true;
console.info('Loggedin user:', loggedInUser);
}
}
正如您所看到的,我想isAuthorized
在我的身份验证服务中设置为 true,以便我能够在整个应用程序中使用它,但它无法正常工作并且isAuthorized
在整个登录过程中始终保持为 false。
这是我的身份验证服务代码:
import { Injectable } from '@angular/core';
import { Moralis } from 'moralis';
export type User = Moralis.User<Moralis.Attributes>;
@Injectable({
providedIn: 'root',
})
export class AuthService {
user?: User;
private _isUserAuthorized: boolean = false;
constructor() {}
ngOnInit() {}
changeUser(user?: User) {
this.user = user;
this.isAuthorized = true;
}
get isAuthorized(): boolean {
return this._isUserAuthorized;
}
set isAuthorized(value: boolean) {
this._isUserAuthorized = value;
}
}