Skip to content
This repository was archived by the owner on Feb 6, 2024. It is now read-only.

Commit 78aeeab

Browse files
feat: UserInfo and User to handle user names in the frontend
1 parent d5eb5b9 commit 78aeeab

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

studio/src/app/models/user.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
export interface User {
2-
id?: string;
1+
// The user information we use to authenticate the user with our backend
2+
export interface UserInfo {
33
anonymous: boolean;
44
firebase_uid: string;
5+
email: string;
6+
}
7+
8+
// The representation of the user saved in our database
9+
export interface User {
10+
id: string;
11+
anonymous: boolean;
12+
username?: string;
513
}

studio/src/app/pages/core/app-signin/app-signin.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {filter, take} from 'rxjs/operators';
88

99
import {del, get, set} from 'idb-keyval';
1010

11-
import {User} from '../../../models/user';
11+
import {User, UserInfo} from '../../../models/user';
1212
import {AuthUser} from '../../../models/auth-user';
1313
import {Deck} from '../../../models/deck';
1414

@@ -181,7 +181,8 @@ export class AppSignIn {
181181

182182
this.authService.watch().pipe(take(1)).subscribe(async (authUser: AuthUser) => {
183183
if (authUser) {
184-
await this.userService.query(user, authUser.token, 'PUT');
184+
const apiUser: UserInfo = await this.userService.createUserInfo(authUser);
185+
await this.userService.query(apiUser, authUser.token, 'PUT');
185186
}
186187

187188
await this.navigateRedirect();

studio/src/app/services/api/user/user.service.tsx

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {Observable, ReplaySubject} from 'rxjs';
33
import {get, del, set} from 'idb-keyval';
44

55
import {AuthUser} from '../../../models/auth-user';
6-
import {User} from '../../../models/user';
6+
import {User, UserInfo} from '../../../models/user';
77

88
import {EnvironmentConfigService} from '../../core/environment/environment-config.service';
99

@@ -36,10 +36,7 @@ export class UserService {
3636
} else {
3737
const savedApiUserId: string = await get('deckdeckgo_user_id');
3838
if (!savedApiUserId) {
39-
const apiUser: User = {
40-
anonymous: authUser.anonymous,
41-
firebase_uid: authUser.uid
42-
};
39+
const apiUser: UserInfo = await this.createUserInfo(authUser);
4340

4441
try {
4542
await this.query(apiUser, authUser.token, 'POST');
@@ -71,7 +68,7 @@ export class UserService {
7168
});
7269
}
7370

74-
query(apiUser: User, token: string, method: string): Promise<User> {
71+
query(apiUserInfo: UserInfo, token: string, method: string): Promise<User> {
7572
return new Promise<User>(async (resolve, reject) => {
7673
try {
7774
const apiUrl: string = EnvironmentConfigService.getInstance().get('apiUrl');
@@ -83,7 +80,7 @@ export class UserService {
8380
'Content-Type': 'application/json',
8481
'Authorization': `Bearer ${token}`
8582
},
86-
body: JSON.stringify(apiUser)
83+
body: JSON.stringify(apiUserInfo)
8784
});
8885

8986
if (!rawResponse || (!rawResponse.ok && rawResponse.status !== 409)) {
@@ -163,4 +160,21 @@ export class UserService {
163160
return this.apiUserSubject.asObservable();
164161
}
165162

163+
createUserInfo(authUser: AuthUser): Promise<UserInfo> {
164+
return new Promise<UserInfo>((resolve) => {
165+
if (!authUser) {
166+
resolve(null);
167+
return;
168+
}
169+
170+
const apiUserInfo: UserInfo = {
171+
anonymous: authUser.anonymous,
172+
firebase_uid: authUser.uid,
173+
email: authUser.anonymous ? null : authUser.email
174+
};
175+
176+
resolve(apiUserInfo);
177+
});
178+
}
179+
166180
}

0 commit comments

Comments
 (0)