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