Skip to content

Commit

Permalink
chore: auth, get user family_name and given_name from google
Browse files Browse the repository at this point in the history
  • Loading branch information
jfmcquade committed Feb 12, 2025
1 parent 011c9de commit c8eb0ef
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/data-models/fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ enum PROTECTED_FIELDS {
APP_USER_ID = "app_user_id",
APP_VERSION = "app_version",
AUTH_USER_DISPLAY_NAME = "auth_user_display_name",
AUTH_USER_FAMILY_NAME = "auth_user_family_name",
AUTH_USER_GIVEN_NAME = "auth_user_given_name",
AUTH_USER_ID = "auth_user_id",
AUTH_USER_PROFILE_IMAGE_URL = "auth_user_profile_image_url",
CONTENT_VERSION = "content_version",
Expand All @@ -32,5 +34,7 @@ export type IProtectedFieldName = keyof typeof PROTECTED_FIELDS;
/** A list of protected fields that should not be synced to the server */
export const EXCLUDED_FIELDS: IProtectedFieldName[] = [
"AUTH_USER_DISPLAY_NAME",
"AUTH_USER_FAMILY_NAME",
"AUTH_USER_GIVEN_NAME",
"AUTH_USER_PROFILE_IMAGE_URL",
];
6 changes: 6 additions & 0 deletions src/app/shared/services/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ export class AuthService extends AsyncServiceBase {
if ("photoUrl" in auth_user) {
this.localStorageService.setProtected("AUTH_USER_PROFILE_IMAGE_URL", auth_user.photoUrl);
}
if ("family_name" in auth_user) {
this.localStorageService.setProtected("AUTH_USER_FAMILY_NAME", auth_user.family_name);
}
if ("given_name" in auth_user) {
this.localStorageService.setProtected("AUTH_USER_GIVEN_NAME", auth_user.given_name);
}
} else {
this.localStorageService.removeProtected("AUTH_USER_ID");
this.localStorageService.removeProtected("AUTH_USER_DISPLAY_NAME");
Expand Down
16 changes: 16 additions & 0 deletions src/app/shared/services/auth/providers/base.auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ export class AuthProviderBase<TAuthUser = IAuthUser> {
return this.authUser();
}

public async getGoogleUserInfo(accessToken: string) {
if (!accessToken) return;
try {
const response = await fetch("https://www.googleapis.com/oauth2/v3/userinfo", {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
const userInfo = await response.json();
const { given_name, family_name } = userInfo;
this.authUser.update((v) => ({ ...v, given_name, family_name }));
} catch (error) {
console.error("[Auth] Error fetching Google user info:", error);
}
}

public async signOut() {
this.authUser.set(undefined);
return this.authUser();
Expand Down
4 changes: 3 additions & 1 deletion src/app/shared/services/auth/providers/firebase.auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export class FirebaseAuthProvider extends AuthProviderBase<User> {
}

public async signInWithGoogle() {
await FirebaseAuthentication.signInWithGoogle();
const result = await FirebaseAuthentication.signInWithGoogle();
const accessToken = result?.credential?.accessToken;
await this.getGoogleUserInfo(accessToken);
return this.authUser();
}

Expand Down
8 changes: 7 additions & 1 deletion src/app/shared/services/auth/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@ import type { FirebaseAuthUser } from "./providers/firebase.auth";

export type IAuthProvider = IDeploymentConfig["auth"]["provider"];

export type IAuthUser = { uid: string } | FirebaseAuthUser;
export type IAuthUser =
| {
uid: string;
given_name?: string;
family_name?: string;
}
| FirebaseAuthUser;

0 comments on commit c8eb0ef

Please sign in to comment.