Add basic auth config to example app

This commit is contained in:
RaviAnand Mohabir 2021-05-20 19:59:42 +02:00
parent 768b7b9fef
commit 6da05b0542
2 changed files with 116 additions and 0 deletions

15
example-app/src/hooks.ts Normal file
View File

@ -0,0 +1,15 @@
import type { Handle } from "@sveltejs/kit";
import { appAuth } from "$lib/appAuth";
export const handle: Handle = async ({ request, render }) => {
// TODO https://github.com/sveltejs/kit/issues/1046
if (request.query.has("_method")) {
request.method = request.query.get("_method").toUpperCase();
}
const response = await render(request);
return response;
};
export const { getSession } = appAuth;

View File

@ -0,0 +1,101 @@
import { Auth } from "svelte-kit-auth";
import {
FacebookAuthProvider,
GoogleOAuthProvider,
RedditOAuthProvider,
TwitterAuthProvider,
} from "svelte-kit-auth/providers";
export const appAuth = new Auth({
providers: [
new GoogleOAuthProvider({
clientId: import.meta.env.VITE_GOOGLE_OAUTH_CLIENT_ID,
clientSecret: import.meta.env.VITE_GOOGLE_OAUTH_CLIENT_SECRET,
profile(profile) {
return { ...profile, provider: "google" };
},
}),
new FacebookAuthProvider({
clientId: import.meta.env.VITE_FACEBOOK_OAUTH_CLIENT_ID,
clientSecret: import.meta.env.VITE_FACEBOOK_OAUTH_CLIENT_SECRET,
profile(profile) {
return { ...profile, provider: "facebook" };
},
}),
new TwitterAuthProvider({
apiKey: import.meta.env.VITE_TWITTER_API_KEY,
apiSecret: import.meta.env.VITE_TWITTER_API_SECRET,
profile(profile) {
return { ...profile, provider: "twitter" };
},
}),
new RedditOAuthProvider({
apiKey: import.meta.env.VITE_REDDIT_API_KEY,
apiSecret: import.meta.env.VITE_REDDIT_API_SECRET,
profile({
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,
}) {
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",
};
},
}),
],
callbacks: {
jwt(token, profile) {
if (profile?.provider) {
const { provider, ...account } = profile;
token = {
...token,
user: {
...token.user,
[provider]: account,
},
};
}
return token;
},
},
});