-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Multi tenant authentication (#355)
- Loading branch information
1 parent
cc6f970
commit 9b3f6d7
Showing
43 changed files
with
948 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { UserService } from "@dzangolab/fastify-user"; | ||
|
||
import getMultiTenantConfig from "./getMultiTenantConfig"; | ||
|
||
import type { Tenant } from "../types/tenant"; | ||
import type { ApiConfig } from "@dzangolab/fastify-config"; | ||
import type { Database } from "@dzangolab/fastify-slonik"; | ||
import type { | ||
User, | ||
UserCreateInput, | ||
UserUpdateInput, | ||
} from "@dzangolab/fastify-user"; | ||
import type { QueryResultRow } from "slonik"; | ||
|
||
const getUserService = ( | ||
config: ApiConfig, | ||
slonik: Database, | ||
tenant?: Tenant | ||
) => { | ||
const multiTenantConfig = getMultiTenantConfig(config); | ||
|
||
const dbSchema = tenant ? tenant[multiTenantConfig.table.columns.slug] : ""; | ||
|
||
return new UserService< | ||
User & QueryResultRow, | ||
UserCreateInput, | ||
UserUpdateInput | ||
>(config, slonik, dbSchema); | ||
}; | ||
|
||
export default getUserService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { | ||
emailPasswordSignIn, | ||
emailPasswordSignUp, | ||
emailPasswordSignUpPOST, | ||
thirdPartySignInUp, | ||
thirdPartySignInUpPOST, | ||
sendEmail, | ||
emailPasswordSignInPOST, | ||
generatePasswordResetTokenPOST, | ||
getUserById, | ||
} from "./third-party-email-password"; | ||
|
||
import type { ThirdPartyEmailPasswordRecipe } from "@dzangolab/fastify-user"; | ||
|
||
const thirdPartyEmailPasswordConfig: ThirdPartyEmailPasswordRecipe = { | ||
override: { | ||
apis: { | ||
emailPasswordSignInPOST, | ||
emailPasswordSignUpPOST, | ||
generatePasswordResetTokenPOST, | ||
thirdPartySignInUpPOST, | ||
}, | ||
functions: { | ||
emailPasswordSignIn, | ||
emailPasswordSignUp, | ||
getUserById, | ||
thirdPartySignInUp, | ||
}, | ||
}, | ||
sendEmail, | ||
}; | ||
|
||
export default thirdPartyEmailPasswordConfig; |
71 changes: 71 additions & 0 deletions
71
...es/multi-tenant/src/supertokens/recipes/third-party-email-password/emailPasswordSignIn.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { formatDate } from "@dzangolab/fastify-user"; | ||
|
||
import getUserService from "../../../lib/getUserService"; | ||
import Email from "../../utils/email"; | ||
|
||
import type { AuthUser } from "@dzangolab/fastify-user"; | ||
import type { FastifyInstance } from "fastify"; | ||
import type { RecipeInterface } from "supertokens-node/recipe/thirdpartyemailpassword"; | ||
|
||
const emailPasswordSignIn = ( | ||
originalImplementation: RecipeInterface, | ||
fastify: FastifyInstance | ||
): RecipeInterface["emailPasswordSignIn"] => { | ||
const { config, log, slonik } = fastify; | ||
|
||
return async (input) => { | ||
input.email = Email.addTenantPrefix( | ||
config, | ||
input.email, | ||
input.userContext.tenant | ||
); | ||
|
||
const originalResponse = await originalImplementation.emailPasswordSignIn( | ||
input | ||
); | ||
|
||
if (originalResponse.status !== "OK") { | ||
return originalResponse; | ||
} | ||
|
||
const userService = getUserService( | ||
config, | ||
slonik, | ||
input.userContext.tenant | ||
); | ||
|
||
const user = await userService.findById(originalResponse.user.id); | ||
|
||
if (!user) { | ||
log.error(`User record not found for userId ${originalResponse.user.id}`); | ||
|
||
return { status: "WRONG_CREDENTIALS_ERROR" }; | ||
} | ||
|
||
user.lastLoginAt = Date.now(); | ||
|
||
await userService | ||
.update(user.id, { | ||
lastLoginAt: formatDate(new Date(user.lastLoginAt)), | ||
}) | ||
/*eslint-disable-next-line @typescript-eslint/no-explicit-any */ | ||
.catch((error: any) => { | ||
log.error( | ||
`Unable to update lastLoginAt for userId ${originalResponse.user.id}` | ||
); | ||
log.error(error); | ||
}); | ||
|
||
const authUser: AuthUser = { | ||
...originalResponse.user, | ||
...user, | ||
}; | ||
|
||
return { | ||
status: "OK", | ||
user: authUser, | ||
}; | ||
}; | ||
}; | ||
|
||
export default emailPasswordSignIn; |
Oops, something went wrong.