I am using Angular and Moralis to make an NFT marketplace.
So far, I am using this bit of code in my signin component to login the user using their metamask extension:
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);
}
}
and as you can see, I want to set isAuthorized
to true in my auth service so that Iโm able to use it all over application but itโs not working and isAuthorized
stays false all throughout the signin process.
hereโs my auth service code:
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;
}
}