@@ -26,7 +26,10 @@ import { REMEMBER_ME_COOKIE_NAME } from "@cocalc/backend/auth/cookie-names";
26
26
import base_path from "@cocalc/backend/base-path" ;
27
27
import getLogger from "@cocalc/backend/logger" ;
28
28
import { set_email_address_verified } from "@cocalc/database/postgres/account-queries" ;
29
- import type { PostgreSQL } from "@cocalc/database/postgres/types" ;
29
+ import type {
30
+ PostgreSQL ,
31
+ UpdateAccountInfoAndPassportOpts ,
32
+ } from "@cocalc/database/postgres/types" ;
30
33
import {
31
34
PassportLoginLocals ,
32
35
PassportLoginOpts ,
@@ -40,6 +43,7 @@ import { createRememberMeCookie } from "@cocalc/server/auth/remember-me";
40
43
import { sanitizeID } from "@cocalc/server/auth/sso/sanitize-id" ;
41
44
import { sanitizeProfile } from "@cocalc/server/auth/sso/sanitize-profile" ;
42
45
import { callback2 as cb2 } from "@cocalc/util/async-utils" ;
46
+ import { is_valid_email_address } from "@cocalc/util/misc" ;
43
47
import { HELP_EMAIL } from "@cocalc/util/theme" ;
44
48
import { emailBelongsToDomain , getEmailDomain } from "./check-required-sso" ;
45
49
import { SSO_API_KEY_COOKIE_NAME } from "./consts" ;
@@ -487,22 +491,37 @@ export class PassportLogin {
487
491
if ( locals . new_account_created || locals . account_id == null ) return ;
488
492
const L = logger . extend ( "maybe_update_account_profile" ) . debug ;
489
493
490
- // if (opts.emails != null) {
491
- // locals.email_address = opts.emails[0];
492
- // }
493
-
494
- L ( `account exists and we update name of user based on SSO` ) ;
495
- await this . database . update_account_and_passport ( {
494
+ const upd : UpdateAccountInfoAndPassportOpts = {
496
495
account_id : locals . account_id ,
497
496
first_name : opts . first_name ,
498
497
last_name : opts . last_name ,
499
498
strategy : opts . strategyName ,
500
499
id : opts . id ,
501
500
profile : opts . profile ,
502
- // but not the email address, at least for now
503
- // email_address: locals.email_address,
504
501
passport_profile : opts . profile ,
505
- } ) ;
502
+ } ;
503
+
504
+ if ( Array . isArray ( opts . emails ) && opts . emails . length >= 1 ) {
505
+ locals . email_address = opts . emails [ 0 ] ;
506
+ }
507
+
508
+ // We update the email address, if it does not belong to another account.
509
+ // Most likely, this just returns the very same account (hence an account exists).
510
+ if ( is_valid_email_address ( locals . email_address ) ) {
511
+ const existing_account_id = await cb2 ( this . database . account_exists , {
512
+ email_address : locals . email_address ,
513
+ } ) ;
514
+ if ( ! existing_account_id ) {
515
+ // There is no account with the new email address, hence we can update the email address as well
516
+ upd . email_address = locals . email_address ;
517
+ L (
518
+ `No existing account with email address ${ locals . email_address } provided by the SSO strategy. Hence we change the email address of account ${ locals . account_id } as well.` ,
519
+ ) ;
520
+ }
521
+ }
522
+
523
+ L ( `account exists and we update name of user based on SSO` ) ;
524
+ await this . database . update_account_and_passport ( upd ) ;
506
525
}
507
526
508
527
// There is a special case, where an api_key was requested.
0 commit comments