Change static env to dynamic env
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { PUBLIC_CLIENT_ID, PUBLIC_REDIRECT_URI } from "$env/static/public";
|
||||
import { env } from "$env/dynamic/public";
|
||||
|
||||
export const generateRandomString = (length: number) => {
|
||||
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
||||
@@ -27,10 +27,10 @@ export const getToken = async (code: string, codeVerifier: string) => {
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
},
|
||||
body: new URLSearchParams({
|
||||
client_id: PUBLIC_CLIENT_ID,
|
||||
client_id: env.PUBLIC_CLIENT_ID,
|
||||
grant_type: 'authorization_code',
|
||||
code,
|
||||
redirect_uri: PUBLIC_REDIRECT_URI,
|
||||
redirect_uri: env.PUBLIC_REDIRECT_URI,
|
||||
code_verifier: codeVerifier
|
||||
})
|
||||
};
|
||||
|
||||
@@ -5,27 +5,25 @@ import { db } from "$lib/server/db";
|
||||
import { sessionsTable, states, usersTable } from "$lib/server/db/schema";
|
||||
import { generateRandomString, getToken } from "$lib/server/auth/spotify";
|
||||
import { getCurrentUserProfile } from "$lib/server/spotify/users";
|
||||
import { env } from "$env/dynamic/public"
|
||||
|
||||
export const load: PageServerLoad = async ({ url, cookies }) => {
|
||||
const code = url.searchParams.get('code');
|
||||
const state = url.searchParams.get('state')
|
||||
|
||||
if (!state || !code) {
|
||||
redirect(307, "/")
|
||||
redirect(307, "/error")
|
||||
}
|
||||
|
||||
const s = await db.select().from(states).where(eq(states.id, state)).limit(1);
|
||||
|
||||
const token = await getToken(code, s[0].codeVerifier)
|
||||
// console.log(token);
|
||||
|
||||
// TODO: Check if deletion was fulfilled
|
||||
await db.delete(states).where(eq(states.id, state));
|
||||
|
||||
const userResponse = await getCurrentUserProfile(token.access_token)
|
||||
|
||||
// console.log(userResponse)
|
||||
|
||||
const isUser: boolean = (await db.$count(usersTable, eq(usersTable.email, userResponse.email))) === 1
|
||||
|
||||
if (!isUser) {
|
||||
@@ -47,7 +45,7 @@ export const load: PageServerLoad = async ({ url, cookies }) => {
|
||||
|
||||
const sessionResponse = await db.insert(sessionsTable).values(session);
|
||||
|
||||
cookies.set("session_id", session.id, { path: "/", secure: false});
|
||||
cookies.set("session_id", session.id, { path: "/", secure: /^true$/i.test(env.PUBLIC_SECURE ?? "true") });
|
||||
|
||||
redirect(307, "/")
|
||||
};
|
||||
@@ -1,4 +1,4 @@
|
||||
import { PUBLIC_CLIENT_ID, PUBLIC_REDIRECT_URI } from "$env/static/public";
|
||||
import { env } from "$env/dynamic/public";
|
||||
import { redirect } from "@sveltejs/kit";
|
||||
import { generateRandomString, sha256, base64encode } from '$lib/server/auth/spotify';
|
||||
import type { PageServerLoad } from "../$types";
|
||||
@@ -24,11 +24,11 @@ export const load: PageServerLoad = async () => {
|
||||
|
||||
const params = {
|
||||
response_type: 'code',
|
||||
client_id: PUBLIC_CLIENT_ID,
|
||||
client_id: env.PUBLIC_CLIENT_ID,
|
||||
scope,
|
||||
code_challenge_method: 'S256',
|
||||
code_challenge: codeChallenge,
|
||||
redirect_uri: PUBLIC_REDIRECT_URI,
|
||||
redirect_uri: env.PUBLIC_REDIRECT_URI,
|
||||
state
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ const config = {
|
||||
// Consult https://svelte.dev/docs/kit/integrations
|
||||
// for more information about preprocessors
|
||||
preprocess: vitePreprocess(),
|
||||
kit: { adapter: adapter() }
|
||||
kit: { adapter: adapter({out: 'build'}) }
|
||||
};
|
||||
|
||||
export default config;
|
||||
|
||||
Reference in New Issue
Block a user