(fix): grabbing envs from $env was a mistake

This commit is contained in:
Shahrad Elahi
2023-11-08 01:36:32 +03:30
parent 51929b3568
commit 1f189a710e
6 changed files with 25 additions and 22 deletions

View File

@@ -4,7 +4,7 @@
"private": true,
"scripts": {
"dev": "vite dev",
"build": "WG_HOST=localhost vite build",
"build": "vite build",
"preview": "vite preview",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",

View File

@@ -1,8 +1,10 @@
import type { Handle } from '@sveltejs/kit';
import { verifyToken } from '$lib/auth';
import { HASHED_PASSWORD } from '$env/static/private';
import 'dotenv/config';
export const handle: Handle = async ({ event, resolve }) => {
const { HASHED_PASSWORD } = process.env;
if (!!HASHED_PASSWORD && !AUTH_EXCEPTION.includes(event.url.pathname)) {
const token = event.cookies.get('authorization');
const token_valid = await verifyToken(token ?? '');

View File

@@ -1,5 +1,10 @@
import jwt from 'jsonwebtoken';
import { AUTH_SECRET } from '$env/static/private';
import 'dotenv/config';
import Hex from 'crypto-js/enc-hex';
import { randomUUID } from 'node:crypto';
import SHA256 from 'crypto-js/sha256';
export const AUTH_SECRET = process.env.AUTH_SECRET || Hex.stringify(SHA256(randomUUID()));
export async function generateToken(): Promise<string> {
const now = Math.floor(Date.now() / 1000);

View File

@@ -1,22 +1,22 @@
import type { RequestHandler } from '@sveltejs/kit';
import { WG_HOST } from '$env/static/private';
import Shell from '$lib/shell';
import 'dotenv/config';
export const GET: RequestHandler = async () => {
let HOSTNAME = WG_HOST;
let { WG_HOST } = process.env
// if the host is not set, then we are using the server's public IP
if (!HOSTNAME) {
if (!WG_HOST) {
const resp = await Shell.exec('curl -s ifconfig.me', true);
HOSTNAME = resp.trim();
WG_HOST = resp.trim();
}
// check if WG_HOST is still not set
if (!HOSTNAME) {
if (!WG_HOST) {
console.error('WG_HOST is not set');
return new Response('NOT_SET', { status: 500, headers: { 'Content-Type': 'text/plain' } });
}
return new Response(HOSTNAME, { status: 200, headers: { 'Content-Type': 'text/plain' } });
return new Response(WG_HOST, { status: 200, headers: { 'Content-Type': 'text/plain' } });
};