supertokens-core icon indicating copy to clipboard operation
supertokens-core copied to clipboard

UserId Mapping Feature

Open jscyo opened this issue 2 years ago • 2 comments

Related Issues

https://github.com/supertokens/supertokens-node/issues/198

Description

Adds userId mapping feature where a user does not have to override all the functions and can simply override the mapper and get done with this.

Recipe ID

useridmapping

DB schema

PostgreSQL

CREATE TABLE IF NOT EXISTS userid_mapping (
    supertokens_user_id CHAR(36) NOT NULL UNIQUE,
    external_user_id VARCHAR(128) NOT NULL UNIQUE,
    external_user_id_info TEXT,
    CONSTRAINT userid_mapping_pkey PRIMARY KEY(supertokens_user_id, external_user_id),
    CONSTRAINT userid_mapping_user_id_fkey FOREIGN KEY (supertokens_user_id) REFERENCES all_auth_recipe_users (user_id) ON DELETE CASCADE
);

MySQL

CREATE TABLE IF NOT EXISTS userid_mapping (
    supertokens_user_id CHAR(36) NOT NULL UNIQUE,
    external_user_id VARCHAR(128) NOT NULL UNIQUE,
    external_user_id_info TEXT,
    PRIMARY KEY (supertokens_user_id, external_user_id),
    FOREIGN KEY (supertokens_user_id) REFERENCES all_auth_recipe_users (user_id) ON DELETE CASCADE
);

API spec

POST /recipe/userid/map

codeflow: https://app.code2flow.com/iWVMxbYowTTk

Body

{
   superTokensUserId: string,
   externalUserId: string,
   externalUserIdInfo?: string
}

Response

{
  status: "OK" | "UNKNOWN_SUPERTOKENS_USER_ID_ERROR"
} |  {
 status: "USER_ID_MAPPING_ALREADY_EXISTS_ERROR",
 doesSuperTokensUserIdExist: boolean,
 doesExternalUserIdExist: boolean
}

POST /recipe/userid/map/remove

codeflow: https://app.code2flow.com/PgmY2PcDEXnS

Body

{
 userId: string,
 userIdType?: "SUPERTOKENS" | "EXTERNAL" | "ANY"
}

Response

{
 status: "OK",
 didMappingExist: boolean
}

GET /recipe/userid/map

codeflow: https://app.code2flow.com/rtJH1uEeHnHZ

Query params

{
 userId: string,
 userIdType?: "SUPERTOKENS" | "EXTERNAL" | "ANY"
}

Response

{
 status: "OK",
 superTokensUserId: string,
 externalUserId: string,
 externalUserIdInfo: string | undefined
} | {
 status: "UNKNOWN_MAPPING_ERROR"
}

PUT /recipe/userid/external-user-id-info

codeFlow: https://app.code2flow.com/5x0OMcSOduof

Body

{
 userId: string,
 userIdType?: "SUPERTOKENS" | "EXTERNAL" | "ANY",
 externalUserIdInfo: string | null
}

Response

{
  status: "OK"  | "UNKNOWN_MAPPING_ERROR"
} 

Remaining TODO's:

  • [x] check whether it's better for the userid_mapping table to have both supertokens_user_id and external_user_idas unique or external_user_id as unique and supertokens_user_id as the primary key
  • [x] make superTokensUserId a foreign key on allAuthUsers userid
  • [x] make sanitization more specific specify empty strings not allowed etc...
  • [x] make supertokensUserId and externalUserId composite keys and check how specific the duplicate key exceptions are
  • [x] create new API to update externalUserId info, should take userId, userIdtype and externalUserIdInfo
  • [x] where we are doing any(apis take userIdType), for the query in that condition we want to check if its a supertokens userID, update row with supertokens userId check. Otherwise update with external userId
  • [x] For the GET API we will use the SELECT query with the OR condition and then filter out only the row with the supertokensUserId = userId result. For the UPDATE and DELETE queries we will have a function that checks if it is a supertokens userId.

jscyo avatar Jul 07 '22 05:07 jscyo

Review:

  • [x] POST /recipe/userid/map
  • [x] POST /recipe/userid/map/remove
  • [x] GET /recipe/userid/map
  • [x] PUT /recipe/userid/external-user-id-info

jscyo avatar Jul 07 '22 06:07 jscyo

Reopening since we need to make changes to all the APIs to resolve based on the map as well - in the core, and not in the backend SDK side.

rishabhpoddar avatar Jul 30 '22 10:07 rishabhpoddar