This repository has been archived by the owner on Sep 17, 2024. It is now read-only.
-
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.
refactor(auth_providers): split
providerData
into uniqueData
and …
…`additionalData`
- Loading branch information
1 parent
b41f94e
commit 30af057
Showing
9 changed files
with
139 additions
and
30 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
65 changes: 65 additions & 0 deletions
65
modules/auth_providers/scripts/get_or_create_user_from_provider.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,65 @@ | ||
import { ScriptContext } from "../module.gen.ts"; | ||
import { ProviderDataInput, ProviderInfo } from "../utils/types.ts"; | ||
|
||
export interface Request { | ||
info: ProviderInfo; | ||
uniqueData: ProviderDataInput; | ||
additionalData: ProviderDataInput; | ||
|
||
suggestedUsername?: string; | ||
} | ||
|
||
export interface Response { | ||
userToken: string; | ||
} | ||
|
||
export async function run( | ||
ctx: ScriptContext, | ||
req: Request, | ||
): Promise<Response> { | ||
const key = req.info.providerType + ":" + req.info.providerId + ":" + JSON.stringify(req.uniqueData); | ||
await ctx.modules.rateLimit.throttle({ | ||
key, | ||
period: 10, | ||
requests: 10, | ||
type: "user", | ||
}); | ||
|
||
// Get users the provider is associated with | ||
const providers = await ctx.db.providerEntries.findFirst({ | ||
where: { | ||
providerType: req.info.providerType, | ||
providerId: req.info.providerId, | ||
uniqueData: { equals: req.uniqueData }, | ||
}, | ||
select: { | ||
userId: true, | ||
}, | ||
}); | ||
|
||
// If the provider is associated with a user, generate a user token and | ||
// return it | ||
if (providers) { | ||
const { token: { token } } = await ctx.modules.users.createToken({ userId: providers.userId }); | ||
return { userToken: token }; | ||
} | ||
|
||
// If the provider is not associated with a user, create a new user | ||
const { user } = await ctx.modules.users.create({ username: req.suggestedUsername }); | ||
|
||
// Insert the provider data with the newly-created user | ||
await ctx.db.providerEntries.create({ | ||
data: { | ||
userId: user.id, | ||
providerType: req.info.providerType, | ||
providerId: req.info.providerId, | ||
uniqueData: req.uniqueData, | ||
additionalData: req.additionalData, | ||
}, | ||
}); | ||
|
||
// Generate a user token and return it | ||
const { token: { token } } = await ctx.modules.users.createToken({ userId: user.id }); | ||
|
||
return { userToken: token }; | ||
} |
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