Automatically infer user language when signing in via Google (#6679)

This commit is contained in:
Tom Moor
2024-03-16 07:59:42 -06:00
committed by GitHub
parent f68c52e255
commit 6775f25425
3 changed files with 14 additions and 0 deletions

View File

@@ -4,6 +4,7 @@ import Router from "koa-router";
import capitalize from "lodash/capitalize"; import capitalize from "lodash/capitalize";
import { Profile } from "passport"; import { Profile } from "passport";
import { Strategy as GoogleStrategy } from "passport-google-oauth2"; import { Strategy as GoogleStrategy } from "passport-google-oauth2";
import { languages } from "@shared/i18n";
import { slugifyDomain } from "@shared/utils/domains"; import { slugifyDomain } from "@shared/utils/domains";
import accountProvisioner from "@server/commands/accountProvisioner"; import accountProvisioner from "@server/commands/accountProvisioner";
import { import {
@@ -33,6 +34,7 @@ type GoogleProfile = Profile & {
picture: string; picture: string;
_json: { _json: {
hd?: string; hd?: string;
locale?: string;
}; };
}; };
@@ -92,6 +94,10 @@ if (env.GOOGLE_CLIENT_ID && env.GOOGLE_CLIENT_SECRET) {
// Request a larger size profile picture than the default by tweaking // Request a larger size profile picture than the default by tweaking
// the query parameter. // the query parameter.
const avatarUrl = profile.picture.replace("=s96-c", "=s128-c"); const avatarUrl = profile.picture.replace("=s96-c", "=s128-c");
const locale = profile._json.locale;
const language = locale
? languages.find((l) => l.startsWith(locale))
: undefined;
// if a team can be inferred, we assume the user is only interested in signing into // if a team can be inferred, we assume the user is only interested in signing into
// that team in particular; otherwise, we will do a best effort at finding their account // that team in particular; otherwise, we will do a best effort at finding their account
@@ -107,6 +113,7 @@ if (env.GOOGLE_CLIENT_ID && env.GOOGLE_CLIENT_SECRET) {
user: { user: {
email: profile.email, email: profile.email,
name: profile.displayName, name: profile.displayName,
language,
avatarUrl, avatarUrl,
}, },
authenticationProvider: { authenticationProvider: {

View File

@@ -20,6 +20,8 @@ type Props = {
email: string; email: string;
/** The public url of an image representing the user */ /** The public url of an image representing the user */
avatarUrl?: string | null; avatarUrl?: string | null;
/** The language of the user, if known */
language?: string;
}; };
/** Details of the team the user is logging into */ /** Details of the team the user is logging into */
team: { team: {
@@ -129,6 +131,7 @@ async function accountProvisioner({
result = await userProvisioner({ result = await userProvisioner({
name: userParams.name, name: userParams.name,
email: userParams.email, email: userParams.email,
language: userParams.language,
isAdmin: isNewTeam || undefined, isAdmin: isNewTeam || undefined,
avatarUrl: userParams.avatarUrl, avatarUrl: userParams.avatarUrl,
teamId: team.id, teamId: team.id,

View File

@@ -20,6 +20,8 @@ type Props = {
name: string; name: string;
/** The email address of the user */ /** The email address of the user */
email: string; email: string;
/** The language of the user, if known */
language?: string;
/** Provision the new user as an administrator */ /** Provision the new user as an administrator */
isAdmin?: boolean; isAdmin?: boolean;
/** The public url of an image representing the user */ /** The public url of an image representing the user */
@@ -51,6 +53,7 @@ export default async function userProvisioner({
name, name,
email, email,
isAdmin, isAdmin,
language,
avatarUrl, avatarUrl,
teamId, teamId,
authentication, authentication,
@@ -233,6 +236,7 @@ export default async function userProvisioner({
{ {
name, name,
email, email,
language,
isAdmin: typeof isAdmin === "boolean" && isAdmin, isAdmin: typeof isAdmin === "boolean" && isAdmin,
isViewer: isAdmin === true ? false : defaultUserRole === "viewer", isViewer: isAdmin === true ? false : defaultUserRole === "viewer",
teamId, teamId,