-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ec15625
commit e5658b9
Showing
32 changed files
with
243 additions
and
17 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
2 changes: 2 additions & 0 deletions
2
waspc/data/Generator/templates/sdk/wasp/client/auth/twitter.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,2 @@ | ||
// PUBLIC API | ||
export { signInUrl as twitterSignInUrl } from '../../auth/helpers/Twitter' |
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
28 changes: 28 additions & 0 deletions
28
waspc/data/Generator/templates/sdk/wasp/server/auth/oauth/providers/twitter.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,28 @@ | ||
{{={= =}=}} | ||
import { Twitter } from "arctic"; | ||
|
||
import { defineProvider } from "../provider.js"; | ||
import { ensureEnvVarsForProvider } from "../env.js"; | ||
import { getRedirectUriForCallback } from "../redirect.js"; | ||
|
||
const id = "{= providerId =}"; | ||
const displayName = "{= displayName =}"; | ||
|
||
const env = ensureEnvVarsForProvider( | ||
["TWITTER_CLIENT_ID", "TWITTER_CLIENT_SECRET"], | ||
displayName | ||
); | ||
|
||
const oAuthClient = new Twitter( | ||
env.TWITTER_CLIENT_ID, | ||
env.TWITTER_CLIENT_SECRET, | ||
getRedirectUriForCallback(id).toString(), | ||
); | ||
|
||
// PUBLIC API | ||
export const twitter = defineProvider({ | ||
id, | ||
displayName, | ||
env, | ||
oAuthClient, | ||
}); |
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
68 changes: 68 additions & 0 deletions
68
waspc/data/Generator/templates/server/src/auth/providers/config/twitter.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,68 @@ | ||
{{={= =}=}} | ||
|
||
import type { ProviderConfig } from "wasp/auth/providers/types"; | ||
import { twitter } from "wasp/server/auth"; | ||
import { mergeDefaultAndUserConfig } from "../oauth/config.js"; | ||
import { createOAuthProviderRouter } from "../oauth/handler.js"; | ||
|
||
{=# userSignupFields.isDefined =} | ||
{=& userSignupFields.importStatement =} | ||
const _waspUserSignupFields = {= userSignupFields.importIdentifier =} | ||
{=/ userSignupFields.isDefined =} | ||
{=^ userSignupFields.isDefined =} | ||
const _waspUserSignupFields = undefined | ||
{=/ userSignupFields.isDefined =} | ||
{=# configFn.isDefined =} | ||
{=& configFn.importStatement =} | ||
const _waspUserDefinedConfigFn = {= configFn.importIdentifier =} | ||
{=/ configFn.isDefined =} | ||
{=^ configFn.isDefined =} | ||
const _waspUserDefinedConfigFn = undefined | ||
{=/ configFn.isDefined =} | ||
|
||
const _waspConfig: ProviderConfig = { | ||
id: twitter.id, | ||
displayName: twitter.displayName, | ||
createRouter(provider) { | ||
const config = mergeDefaultAndUserConfig({ | ||
scopes: {=& requiredScopes =}, | ||
}, _waspUserDefinedConfigFn); | ||
|
||
async function getTwitterProfile(accessToken: string): Promise<{ | ||
providerProfile: unknown; | ||
providerUserId: string; | ||
}> { | ||
const response = await fetch("https://api.twitter.com/2/users/me", { | ||
headers: { | ||
Authorization: `Bearer ${accessToken}`, | ||
}, | ||
}); | ||
|
||
const jsonResponse = await response.json(); | ||
|
||
const providerProfile = jsonResponse.data as { | ||
id?: string; | ||
name?: string; | ||
username?: string; | ||
}; | ||
|
||
|
||
if (!providerProfile.id) { | ||
throw new Error("Invalid profile"); | ||
} | ||
|
||
return { providerProfile, providerUserId: providerProfile.id }; | ||
} | ||
|
||
return createOAuthProviderRouter({ | ||
provider, | ||
oAuthType: 'OAuth2WithPKCE', | ||
userSignupFields: _waspUserSignupFields, | ||
getAuthorizationUrl: ({ state, codeVerifier }) => twitter.oAuthClient.createAuthorizationURL(state, codeVerifier, config), | ||
getProviderTokens: ({ code, codeVerifier }) => twitter.oAuthClient.validateAuthorizationCode(code, codeVerifier), | ||
getProviderInfo: ({ accessToken }) => getTwitterProfile(accessToken), | ||
}); | ||
}, | ||
} | ||
|
||
export default _waspConfig; |
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,10 @@ | ||
import { defineUserSignupFields } from 'wasp/server/auth' | ||
|
||
export function config() { | ||
console.log('Inside user-supplied Twitter config') | ||
return { | ||
scopes: ['users.read', 'tweet.read'], | ||
} | ||
} | ||
|
||
export const userSignupFields = defineUserSignupFields({}) |
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
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
Oops, something went wrong.