diff --git a/.auri/$1j735d60.md b/.auri/$1j735d60.md new file mode 100644 index 000000000..a5b023f42 --- /dev/null +++ b/.auri/$1j735d60.md @@ -0,0 +1,6 @@ +--- +package: "@lucia-auth/oauth" # package name +type: "major" # "major", "minor", "patch" +--- + +Rename `providerUser` property to `User` (`githubUser` etc) \ No newline at end of file diff --git a/.auri/$40a7umh0.md b/.auri/$40a7umh0.md new file mode 100644 index 000000000..ec1eab8f5 --- /dev/null +++ b/.auri/$40a7umh0.md @@ -0,0 +1,6 @@ +--- +package: "@lucia-auth/oauth" # package name +type: "major" # "major", "minor", "patch" +--- + +Rename `useAuth()` to `providerUserAuth()` \ No newline at end of file diff --git a/.auri/$yf495wr1.md b/.auri/$yf495wr1.md new file mode 100644 index 000000000..d6ad0a941 --- /dev/null +++ b/.auri/$yf495wr1.md @@ -0,0 +1,6 @@ +--- +package: "@lucia-auth/oauth" # package name +type: "major" # "major", "minor", "patch" +--- + +Rename `tokens` property to `Tokens` (`githubTokens` etc) \ No newline at end of file diff --git a/packages/oauth/src/core.ts b/packages/oauth/src/core.ts index 02b28dd1a..ad3713597 100644 --- a/packages/oauth/src/core.ts +++ b/packages/oauth/src/core.ts @@ -9,20 +9,14 @@ export type OAuthConfig = { scope?: string[]; }; -export type OAuthProvider = { +export type OAuthProvider = { validateCallback: ( code: string, ...args: any[] ) => Promise<{ - existingUser: LuciaUser | null; - createUser: ( - attributes: CreateUserAttributesParameter - ) => Promise>; + existingUser: Record | null; + createUser: (attributes: Record) => Promise>; createKey: (userId: string) => Promise; - providerUser: Record; - tokens: { - accessToken: string; - }; }>; getAuthorizationUrl: ( redirectUri?: string @@ -48,7 +42,7 @@ export const scope = (base: string[], config: string[] = []) => { return [...base, ...(config ?? [])].join(" "); }; -export const useAuth = async <_Auth extends Auth>( +export const providerUserAuth = async <_Auth extends Auth>( auth: _Auth, providerId: string, providerUserId: string diff --git a/packages/oauth/src/index.ts b/packages/oauth/src/index.ts index 0fa32db2d..d26d69236 100644 --- a/packages/oauth/src/index.ts +++ b/packages/oauth/src/index.ts @@ -1,3 +1,3 @@ -export { generateState, OAuthRequestError, useAuth } from "./core.js"; +export { generateState, OAuthRequestError, providerUserAuth } from "./core.js"; export type { OAuthProvider } from "./core.js"; diff --git a/packages/oauth/src/providers/auth0.ts b/packages/oauth/src/providers/auth0.ts index f5cef4fff..0be17b291 100644 --- a/packages/oauth/src/providers/auth0.ts +++ b/packages/oauth/src/providers/auth0.ts @@ -1,5 +1,5 @@ import { createUrl, handleRequest, authorizationHeaders } from "../request.js"; -import { scope, generateState, useAuth } from "../core.js"; +import { scope, generateState, providerUserAuth } from "../core.js"; import type { Auth } from "lucia"; import type { OAuthConfig, OAuthProvider } from "../core.js"; @@ -16,7 +16,7 @@ type Config = OAuthConfig & { }; export const auth0 = <_Auth extends Auth>(auth: _Auth, config: Config) => { - const getTokens = async (code: string) => { + const getAuth0Tokens = async (code: string) => { const request = new Request(new URL("/oauth/token", config.appDomain), { method: "POST", headers: { @@ -45,7 +45,7 @@ export const auth0 = <_Auth extends Auth>(auth: _Auth, config: Config) => { }; }; - const getProviderUser = async (accessToken: string) => { + const getAuth0User = async (accessToken: string) => { const request = new Request(new URL("/userinfo", config.appDomain), { headers: authorizationHeaders("bearer", accessToken) }); @@ -83,21 +83,21 @@ export const auth0 = <_Auth extends Auth>(auth: _Auth, config: Config) => { return [url, state] as const; }, validateCallback: async (code: string) => { - const tokens = await getTokens(code); - const providerUser = await getProviderUser(tokens.accessToken); - const providerUserId = providerUser.id; - const providerAuthHelpersHelpers = await useAuth( + const auth0Tokens = await getAuth0Tokens(code); + const auth0User = await getAuth0User(auth0Tokens.accessToken); + const providerUserId = auth0User.id; + const auth0UserAuth = await providerUserAuth( auth, PROVIDER_ID, providerUserId ); return { - ...providerAuthHelpersHelpers, - providerUser, - tokens + ...auth0UserAuth, + auth0User, + auth0Tokens }; } - } as const satisfies OAuthProvider<_Auth>; + } as const satisfies OAuthProvider; }; type Auth0Profile = { diff --git a/packages/oauth/src/providers/discord.ts b/packages/oauth/src/providers/discord.ts index 5a090d75e..7e75905bc 100644 --- a/packages/oauth/src/providers/discord.ts +++ b/packages/oauth/src/providers/discord.ts @@ -1,5 +1,5 @@ import { createUrl, handleRequest, authorizationHeaders } from "../request.js"; -import { useAuth, generateState, scope } from "../core.js"; +import { providerUserAuth, generateState, scope } from "../core.js"; import type { Auth } from "lucia"; import type { OAuthConfig, OAuthProvider } from "../core.js"; @@ -11,7 +11,7 @@ type Config = OAuthConfig & { const PROVIDER_ID = "discord"; export const discord = <_Auth extends Auth>(auth: _Auth, config: Config) => { - const getTokens = async (code: string) => { + const getDiscordTokens = async (code: string) => { const request = new Request("https://discord.com/api/oauth2/token", { method: "POST", headers: { @@ -38,7 +38,7 @@ export const discord = <_Auth extends Auth>(auth: _Auth, config: Config) => { }; }; - const getProviderUser = async (accessToken: string) => { + const getDiscordUser = async (accessToken: string) => { // do not use oauth/users/@me because it ignores intents, use oauth/users/@me instead const request = new Request("https://discord.com/api/users/@me", { headers: authorizationHeaders("bearer", accessToken) @@ -60,21 +60,21 @@ export const discord = <_Auth extends Auth>(auth: _Auth, config: Config) => { return [url, state]; }, validateCallback: async (code: string) => { - const tokens = await getTokens(code); - const providerUser = await getProviderUser(tokens.accessToken); - const providerUserId = providerUser.id; - const providerAuthHelpers = await useAuth( + const discordTokens = await getDiscordTokens(code); + const discordUser = await getDiscordUser(discordTokens.accessToken); + const providerUserId = discordUser.id; + const discordUserAuth = await providerUserAuth( auth, PROVIDER_ID, providerUserId ); return { - ...providerAuthHelpers, - providerUser, - tokens + ...discordUserAuth, + discordUser, + discordTokens }; } - } as const satisfies OAuthProvider<_Auth>; + } as const satisfies OAuthProvider; }; export type DiscordUser = { diff --git a/packages/oauth/src/providers/facebook.ts b/packages/oauth/src/providers/facebook.ts index de1b0c78d..c0021b182 100644 --- a/packages/oauth/src/providers/facebook.ts +++ b/packages/oauth/src/providers/facebook.ts @@ -1,5 +1,5 @@ import { createUrl, handleRequest, authorizationHeaders } from "../request.js"; -import { scope, generateState, useAuth } from "../core.js"; +import { scope, generateState, providerUserAuth } from "../core.js"; import type { Auth } from "lucia"; import type { OAuthConfig, OAuthProvider } from "../core.js"; @@ -11,7 +11,7 @@ type Config = OAuthConfig & { const PROVIDER_ID = "facebook"; export const facebook = <_Auth extends Auth>(auth: _Auth, config: Config) => { - const getTokens = async (code: string) => { + const getFacebookTokens = async (code: string) => { const requestUrl = createUrl( "https://graph.facebook.com/v16.0/oauth/access_token", { @@ -35,7 +35,7 @@ export const facebook = <_Auth extends Auth>(auth: _Auth, config: Config) => { }; }; - const getProviderUser = async (accessToken: string) => { + const getFacebookUser = async (accessToken: string) => { const requestUrl = createUrl("https://graph.facebook.com/me", { access_token: accessToken, fields: ["id", "name", "picture"].join(",") @@ -59,21 +59,21 @@ export const facebook = <_Auth extends Auth>(auth: _Auth, config: Config) => { return [url, state] as const; }, validateCallback: async (code: string) => { - const tokens = await getTokens(code); - const providerUser = await getProviderUser(tokens.accessToken); - const providerUserId = providerUser.id; - const providerAuthHelpers = await useAuth( + const tokens = await getFacebookTokens(code); + const facebookUser = await getFacebookUser(tokens.accessToken); + const providerUserId = facebookUser.id; + const facebookUserAuth = await providerUserAuth( auth, PROVIDER_ID, providerUserId ); return { - ...providerAuthHelpers, - providerUser, + ...facebookUserAuth, + facebookUser, tokens }; } - } as const satisfies OAuthProvider<_Auth>; + } as const satisfies OAuthProvider; }; export type FacebookUser = { diff --git a/packages/oauth/src/providers/github.ts b/packages/oauth/src/providers/github.ts index a2a43e48f..b0ecb99b3 100644 --- a/packages/oauth/src/providers/github.ts +++ b/packages/oauth/src/providers/github.ts @@ -1,5 +1,5 @@ import { createUrl, handleRequest, authorizationHeaders } from "../request.js"; -import { scope, generateState, useAuth } from "../core.js"; +import { scope, generateState, providerUserAuth } from "../core.js"; import type { Auth } from "lucia"; import type { OAuthConfig, OAuthProvider } from "../core.js"; @@ -23,7 +23,7 @@ type Config = OAuthConfig & { }; export const github = <_Auth extends Auth>(auth: _Auth, config: Config) => { - const getTokens = async (code: string): Promise => { + const getGithubTokens = async (code: string): Promise => { const requestUrl = createUrl( "https://github.com/login/oauth/access_token", { @@ -63,7 +63,7 @@ export const github = <_Auth extends Auth>(auth: _Auth, config: Config) => { }; }; - const getProviderUser = async (accessToken: string) => { + const getGithubUser = async (accessToken: string) => { const request = new Request("https://api.github.com/user", { headers: authorizationHeaders("bearer", accessToken) }); @@ -85,21 +85,21 @@ export const github = <_Auth extends Auth>(auth: _Auth, config: Config) => { return [url, state] as const; }, validateCallback: async (code: string) => { - const tokens = await getTokens(code); - const providerUser = await getProviderUser(tokens.accessToken); - const providerUserId = providerUser.id.toString(); - const providerAuthHelpers = await useAuth( + const githubTokens = await getGithubTokens(code); + const githubUser = await getGithubUser(githubTokens.accessToken); + const providerUserId = githubUser.id.toString(); + const githubUserAuth = await providerUserAuth( auth, PROVIDER_ID, providerUserId ); return { - ...providerAuthHelpers, - providerUser, - tokens + ...githubUserAuth, + githubUser, + githubTokens }; } - } as const satisfies OAuthProvider<_Auth>; + } as const satisfies OAuthProvider; }; type PublicGithubUser = { diff --git a/packages/oauth/src/providers/google.ts b/packages/oauth/src/providers/google.ts index b1203db6d..7a52bd7ef 100644 --- a/packages/oauth/src/providers/google.ts +++ b/packages/oauth/src/providers/google.ts @@ -1,5 +1,5 @@ import { createUrl, handleRequest, authorizationHeaders } from "../request.js"; -import { scope, generateState, useAuth } from "../core.js"; +import { scope, generateState, providerUserAuth } from "../core.js"; import type { Auth } from "lucia"; import type { OAuthConfig, OAuthProvider } from "../core.js"; @@ -11,7 +11,7 @@ type Config = OAuthConfig & { const PROVIDER_ID = "google"; export const google = <_Auth extends Auth>(auth: _Auth, config: Config) => { - const getTokens = async (code: string) => { + const getGoogleTokens = async (code: string) => { const requestUrl = createUrl("https://oauth2.googleapis.com/token", { client_id: config.clientId, client_secret: config.clientSecret, @@ -35,7 +35,7 @@ export const google = <_Auth extends Auth>(auth: _Auth, config: Config) => { }; }; - const getProviderUser = async (accessToken: string) => { + const getGoogleUser = async (accessToken: string) => { const request = new Request( "https://www.googleapis.com/oauth2/v3/userinfo", { @@ -62,21 +62,21 @@ export const google = <_Auth extends Auth>(auth: _Auth, config: Config) => { return [url, state] as const; }, validateCallback: async (code: string) => { - const tokens = await getTokens(code); - const providerUser = await getProviderUser(tokens.accessToken); - const providerUserId = providerUser.sub; - const providerAuthHelpers = await useAuth( + const googleTokens = await getGoogleTokens(code); + const googleUser = await getGoogleUser(googleTokens.accessToken); + const providerUserId = googleUser.sub; + const googleUserAuth = await providerUserAuth( auth, PROVIDER_ID, providerUserId ); return { - ...providerAuthHelpers, - providerUser, - tokens + ...googleUserAuth, + googleUser, + googleTokens }; } - } as const satisfies OAuthProvider<_Auth>; + } as const satisfies OAuthProvider; }; export type GoogleUser = { diff --git a/packages/oauth/src/providers/linkedin.ts b/packages/oauth/src/providers/linkedin.ts index 39ee2817a..802af3795 100644 --- a/packages/oauth/src/providers/linkedin.ts +++ b/packages/oauth/src/providers/linkedin.ts @@ -1,5 +1,5 @@ import { createUrl, handleRequest, authorizationHeaders } from "../request.js"; -import { scope, generateState, useAuth } from "../core.js"; +import { scope, generateState, providerUserAuth } from "../core.js"; import type { Auth } from "lucia"; import type { OAuthConfig, OAuthProvider } from "../core.js"; @@ -11,7 +11,7 @@ type Config = OAuthConfig & { }; export const linkedin = <_Auth extends Auth>(auth: _Auth, config: Config) => { - const getTokens = async (code: string) => { + const getLinkedinTokens = async (code: string) => { const requestUrl = createUrl( "https://www.linkedin.com/oauth/v2/accessToken", { @@ -45,17 +45,17 @@ export const linkedin = <_Auth extends Auth>(auth: _Auth, config: Config) => { }; }; - const getProviderUser = async (accessToken: string) => { - const linkedinProfile = await getProfile(accessToken); - const displayImageElement = linkedinProfile.profilePicture[ + const getLinkedinUser = async (accessToken: string) => { + const linkedinUserProfile = await getProfile(accessToken); + const displayImageElement = linkedinUserProfile.profilePicture[ "displayImage~" ]?.elements ?.slice(-1) ?.pop(); const linkedinUser: LinkedinUser = { - id: linkedinProfile.id, - firstName: linkedinProfile.localizedFirstName, - lastName: linkedinProfile.localizedLastName, + id: linkedinUserProfile.id, + firstName: linkedinUserProfile.localizedFirstName, + lastName: linkedinUserProfile.localizedLastName, profilePicture: displayImageElement?.identifiers?.pop()?.identifier }; @@ -90,21 +90,21 @@ export const linkedin = <_Auth extends Auth>(auth: _Auth, config: Config) => { return [url, state] as const; }, validateCallback: async (code: string) => { - const tokens = await getTokens(code); - const providerUser = await getProviderUser(tokens.accessToken); - const providerUserId = providerUser.id; - const providerAuthHelpers = await useAuth( + const linkedinTokens = await getLinkedinTokens(code); + const linkedinUser = await getLinkedinUser(linkedinTokens.accessToken); + const providerUserId = linkedinUser.id; + const linkedinUserAuth = await providerUserAuth( auth, PROVIDER_ID, providerUserId ); return { - ...providerAuthHelpers, - providerUser, - tokens + ...linkedinUserAuth, + linkedinUser, + linkedinTokens }; } - } as const satisfies OAuthProvider<_Auth>; + } as const satisfies OAuthProvider; }; type LinkedinProfileResponse = { diff --git a/packages/oauth/src/providers/patreon.ts b/packages/oauth/src/providers/patreon.ts index b7bc148ab..38bc19162 100644 --- a/packages/oauth/src/providers/patreon.ts +++ b/packages/oauth/src/providers/patreon.ts @@ -1,5 +1,5 @@ import { createUrl, handleRequest, authorizationHeaders } from "../request.js"; -import { scope, generateState, useAuth } from "../core.js"; +import { scope, generateState, providerUserAuth } from "../core.js"; import type { Auth } from "lucia"; import type { OAuthConfig, OAuthProvider } from "../core.js"; @@ -12,7 +12,7 @@ type Config = OAuthConfig & { const PROVIDER_ID = "patreon"; export const patreon = <_Auth extends Auth>(auth: _Auth, config: Config) => { - const getTokens = async (code: string) => { + const getPatreonTokens = async (code: string) => { const requestUrl = createUrl("https://www.patreon.com/api/oauth2/token", { client_id: config.clientId, client_secret: config.clientSecret, @@ -37,7 +37,7 @@ export const patreon = <_Auth extends Auth>(auth: _Auth, config: Config) => { }; }; - const getProviderUser = async (accessToken: string) => { + const getPatreonUser = async (accessToken: string) => { const requestUrl = createUrl( "https://www.patreon.com/api/oauth2/v2/identity", { @@ -68,21 +68,21 @@ export const patreon = <_Auth extends Auth>(auth: _Auth, config: Config) => { return [url, state] as const; }, validateCallback: async (code: string) => { - const tokens = await getTokens(code); - const providerUser = await getProviderUser(tokens.accessToken); - const providerUserId = providerUser.id; - const providerAuthHelpers = await useAuth( + const patreonTokens = await getPatreonTokens(code); + const patreonUser = await getPatreonUser(patreonTokens.accessToken); + const providerUserId = patreonUser.id; + const patreonUserAuth = await providerUserAuth( auth, PROVIDER_ID, providerUserId ); return { - ...providerAuthHelpers, - providerUser, - tokens + ...patreonUserAuth, + patreonUser, + patreonTokens }; } - } as const satisfies OAuthProvider<_Auth>; + } as const satisfies OAuthProvider; }; export type PatreonUser = { diff --git a/packages/oauth/src/providers/reddit.ts b/packages/oauth/src/providers/reddit.ts index ebd2ac8a4..0f8c26db0 100644 --- a/packages/oauth/src/providers/reddit.ts +++ b/packages/oauth/src/providers/reddit.ts @@ -1,5 +1,5 @@ import { createUrl, handleRequest, authorizationHeaders } from "../request.js"; -import { scope, generateState, useAuth } from "../core.js"; +import { scope, generateState, providerUserAuth } from "../core.js"; import type { Auth } from "lucia"; import type { OAuthConfig, OAuthProvider } from "../core.js"; @@ -11,7 +11,7 @@ type Config = OAuthConfig & { const PROVIDER_ID = "reddit"; export const reddit = <_Auth extends Auth>(auth: _Auth, config: Config) => { - const getTokens = async (code: string) => { + const getRedditTokens = async (code: string) => { const requestUrl = createUrl("https://www.reddit.com/api/v1/access_token", { grant_type: "authorization_code", redirect_uri: config.redirectUri, @@ -33,7 +33,7 @@ export const reddit = <_Auth extends Auth>(auth: _Auth, config: Config) => { }; }; - const getProviderUser = async (accessToken: string) => { + const getRedditUser = async (accessToken: string) => { const request = new Request("https://oauth.reddit.com/api/v1/me", { headers: authorizationHeaders("bearer", accessToken) }); @@ -56,21 +56,21 @@ export const reddit = <_Auth extends Auth>(auth: _Auth, config: Config) => { return [url, state] as const; }, validateCallback: async (code: string) => { - const tokens = await getTokens(code); - const providerUser = await getProviderUser(tokens.accessToken); - const providerUserId = providerUser.id; - const providerAuthHelpers = await useAuth( + const redditTokens = await getRedditTokens(code); + const redditUser = await getRedditUser(redditTokens.accessToken); + const providerUserId = redditUser.id; + const redditUserAuth = await providerUserAuth( auth, PROVIDER_ID, providerUserId ); return { - ...providerAuthHelpers, - providerUser, - tokens + ...redditUserAuth, + redditUser, + redditTokens }; } - } as const satisfies OAuthProvider<_Auth>; + } as const satisfies OAuthProvider; }; const encodeBase64 = (s: string) => { diff --git a/packages/oauth/src/providers/twitch.ts b/packages/oauth/src/providers/twitch.ts index ab17b7675..170adef30 100644 --- a/packages/oauth/src/providers/twitch.ts +++ b/packages/oauth/src/providers/twitch.ts @@ -1,5 +1,5 @@ import { createUrl, handleRequest, authorizationHeaders } from "../request.js"; -import { scope, generateState, useAuth } from "../core.js"; +import { scope, generateState, providerUserAuth } from "../core.js"; import type { Auth } from "lucia"; import type { OAuthConfig, OAuthProvider } from "../core.js"; @@ -12,7 +12,7 @@ type Config = OAuthConfig & { const PROVIDER_ID = "twitch"; export const twitch = <_Auth extends Auth>(auth: _Auth, config: Config) => { - const getTokens = async (code: string) => { + const getTwitchTokens = async (code: string) => { const requestUrl = createUrl("https://id.twitch.tv/oauth2/token", { client_id: config.clientId, client_secret: config.clientSecret, @@ -36,7 +36,7 @@ export const twitch = <_Auth extends Auth>(auth: _Auth, config: Config) => { }; }; - const getProviderUser = async (accessToken: string) => { + const getTwitchUser = async (accessToken: string) => { // https://dev.twitch.tv/docs/api/reference/#get-users const request = new Request("https://api.twitch.tv/helix/users", { headers: { @@ -65,21 +65,21 @@ export const twitch = <_Auth extends Auth>(auth: _Auth, config: Config) => { return [url, state] as const; }, validateCallback: async (code: string) => { - const tokens = await getTokens(code); - const providerUser = await getProviderUser(tokens.accessToken); - const providerUserId = providerUser.id; - const providerAuthHelpers = await useAuth( + const twitchTokens = await getTwitchTokens(code); + const twitchUser = await getTwitchUser(twitchTokens.accessToken); + const providerUserId = twitchUser.id; + const twitchUserAuth = await providerUserAuth( auth, PROVIDER_ID, providerUserId ); return { - ...providerAuthHelpers, - providerUser, - tokens + ...twitchUserAuth, + twitchUser, + twitchTokens }; } - } as const satisfies OAuthProvider<_Auth>; + } as const satisfies OAuthProvider; }; export type TwitchUser = {