Skip to content

Commit

Permalink
fix: fixed login issue
Browse files Browse the repository at this point in the history
  • Loading branch information
JanssenBrm committed Jan 27, 2024
1 parent 92b9930 commit f7fe443
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/app/login/login.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class LoginPage implements OnInit {
} else {
this.router.navigate(['verify-email']);
}
this.loading = false;
}
},
});
Expand Down
16 changes: 8 additions & 8 deletions src/app/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,28 @@ export class AuthService {
}
})
)
.subscribe(({ user, data }) => {
.subscribe(({ user }) => {
if (user) {
localStorage.setItem('user', JSON.stringify(user));
this.updateUser(user, data).then(() => {
console.log('User updated');
});
} else {
localStorage.removeItem('user');
}
this.user.next(user);
});
}

public async updateUser(user: User, data: UserData): Promise<UserData> {
public updateUser(user: User, data: UserData): Observable<UserData> {
const newData: UserData = {
id: user.uid,
name: user.displayName,
role: data?.role || UserRole.BASIC,
customerId: data?.customerId || '',
};
await this.fireStore.collection<UserData>('users').doc(user.uid).set(newData);
return newData;
return from(this.fireStore.collection<UserData>('users').doc(user.uid).set(newData)).pipe(
map(() => {
return newData;
})
);
}

public readUserData(user: User): Observable<UserData> {
Expand All @@ -75,7 +75,7 @@ export class AuthService {
.pipe(
take(1),
map((data: DocumentSnapshot<UserData>) => data.data()),
switchMap((data: UserData) => (!data ? from(this.updateUser(user, null)) : of(data))),
switchMap((data: UserData) => (!data ? this.updateUser(user, null) : of(data))),
switchMap((data: UserData) =>
data.customerId !== ''
? of(data)
Expand Down
8 changes: 4 additions & 4 deletions src/app/services/features.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@angular/core';
import { AuthService } from './auth.service';
import { PREMIUM_ROLES, UserData, UserRole } from '../models/userdata.model';
import { combineLatest, from, Observable } from 'rxjs';
import { combineLatest, Observable } from 'rxjs';
import { map, skipWhile, switchMap, take } from 'rxjs/operators';
import { User } from 'firebase';

Expand Down Expand Up @@ -30,12 +30,12 @@ export class FeaturesService {
return combineLatest(this.authService.getUser(), this.authService.getUserData()).pipe(
take(1),
switchMap(([user, userData]: [User, UserData]) =>
from(
this.authService.updateUser(user, {
this.authService
.updateUser(user, {
...userData,
role,
})
).pipe(switchMap(() => this.authService.readUserData(user)))
.pipe(switchMap(() => this.authService.readUserData(user)))
)
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/verify-email/verify-email.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ <h3>Thank You for Registering</h3>
</div>
</div>
<ion-buttons class="secondary">
<ion-button expand="full" routerLink="/login">
<ion-button expand="full" (click)="toLogin()">
<span>Back to login</span>
</ion-button>
</ion-buttons>
Expand Down
8 changes: 7 additions & 1 deletion src/app/verify-email/verify-email.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AuthService } from '../services/auth.service';
import { BehaviorSubject } from 'rxjs';
import { User } from 'firebase';
import { UtilService } from '../services/util.service';
import { Router } from '@angular/router';

@Component({
selector: 'app-verify-email',
Expand All @@ -12,9 +13,14 @@ import { UtilService } from '../services/util.service';
export class VerifyEmailPage implements OnInit {
public user: BehaviorSubject<User>;

constructor(public authService: AuthService, public utilService: UtilService) {}
constructor(public authService: AuthService, private router: Router, public utilService: UtilService) {}

ngOnInit() {
this.user = this.authService.getUser();
}

async toLogin() {
await this.authService.logout();
await this.router.navigate(['login']);
}
}

0 comments on commit f7fe443

Please sign in to comment.