🎨 Add RedditOAuthProvider.profileHandler for general use and stripping of payload

This commit is contained in:
RaviAnand Mohabir 2021-05-21 13:12:10 +02:00
parent 7d1f92d593
commit 7c454575ef
2 changed files with 52 additions and 92 deletions

View File

@ -32,53 +32,9 @@ export const appAuth = new SvelteKitAuth({
new RedditOAuthProvider({ new RedditOAuthProvider({
apiKey: import.meta.env.VITE_REDDIT_API_KEY, apiKey: import.meta.env.VITE_REDDIT_API_KEY,
apiSecret: import.meta.env.VITE_REDDIT_API_SECRET, apiSecret: import.meta.env.VITE_REDDIT_API_SECRET,
profile({ profile(profile) {
is_employee, profile = RedditOAuthProvider.profileHandler(profile);
has_external_account, return { ...profile, provider: "reddit" };
snoovatar_img,
verified,
id,
over_18,
is_gold,
is_mod,
awarder_karma,
has_verified_email,
is_suspended,
icon_img,
pref_nightmode,
awardee_karma,
password_set,
link_karma,
total_karma,
name,
created,
created_utc,
comment_karma,
}) {
return {
is_employee,
has_external_account,
snoovatar_img,
verified,
id,
over_18,
is_gold,
is_mod,
awarder_karma,
has_verified_email,
is_suspended,
icon_img,
pref_nightmode,
awardee_karma,
password_set,
link_karma,
total_karma,
name,
created,
created_utc,
comment_karma,
provider: "reddit",
};
}, },
}), }),
], ],

View File

@ -8,11 +8,7 @@ interface RedditOAuthProviderConfig extends OAuth2ProviderConfig {
duration?: "temporary" | "permanent"; duration?: "temporary" | "permanent";
} }
const defaultConfig: Partial<RedditOAuthProviderConfig> = { const redditProfileHandler = ({
id: "reddit",
scope: "identity",
duration: "temporary",
profile: ({
is_employee, is_employee,
has_external_account, has_external_account,
snoovatar_img, snoovatar_img,
@ -56,7 +52,13 @@ const defaultConfig: Partial<RedditOAuthProviderConfig> = {
created, created,
created_utc, created_utc,
comment_karma, comment_karma,
}), });
const defaultConfig: Partial<RedditOAuthProviderConfig> = {
id: "reddit",
scope: "identity",
duration: "temporary",
profile: redditProfileHandler,
}; };
export class RedditOAuthProvider extends OAuth2Provider<RedditOAuthProviderConfig> { export class RedditOAuthProvider extends OAuth2Provider<RedditOAuthProviderConfig> {
@ -67,6 +69,8 @@ export class RedditOAuthProvider extends OAuth2Provider<RedditOAuthProviderConfi
}); });
} }
static profileHandler = redditProfileHandler;
async getSigninUrl({ host }: ServerRequest, state: string) { async getSigninUrl({ host }: ServerRequest, state: string) {
const endpoint = "https://www.reddit.com/api/v1/authorize"; const endpoint = "https://www.reddit.com/api/v1/authorize";