3arcus
3arcus
Explore posts from servers
TTCTheo's Typesafe Cult
Created by 3arcus on 6/29/2023 in #questions
Protect routes? For the app itself, no user auth, pages router. Help, desperate.
Hi, I'm a noob and I've been winging it with Chat GPT My app is ready, Im using next js 13, now I came to the challenge of protecting the routes, specially because I have routes to create / delete / update. This has become a problem. My app has interactions, that call the routes, I might have messed up the architecture, for instance: This is a button
onClick={() => {
//updateTicket("free", id).then(() => setQuantity(quantity - 1));
if (!lockedRaffle) updateTicket("free", id);
}}
disabled={quantity <= 0 || lockedRaffle}
>
onClick={() => {
//updateTicket("free", id).then(() => setQuantity(quantity - 1));
if (!lockedRaffle) updateTicket("free", id);
}}
disabled={quantity <= 0 || lockedRaffle}
>
Then, depending on the action, it will call a route like this ```try { setLockedRaffle(true); //await new Promise((r) => setTimeout(r, 1000)); // wait for 1 second const response = await fetch("/api/updateTicket", { // replace '/api/update-route' with your route method: "PUT", headers: { "Content-Type": "application/json", Authorization: Bearer ${process.env.TOKEN}`, }, body: JSON.stringify({ address: owner, raffle: raffleID, updateType: "FROM_NULL", }), });```` There is no user authentication, it's the App that decides what and where to update, obvious problem is that I don't want someone else calling these endpoints, I've now realised that Bearer is undefined, probably because this is being rendered client side? So how do I solve this? I can't find a solution, every tutorial that I've came across relies on authentication from the user, this is not what I want, the user should never be authorised to call these endpoints, it's the app that decided what to call depending on whats happening, such as buttons being pushed, Albeit probably subpart and dumb, my "architecture" works as I intended, the only issue is, how do I protect the routes, I'm able to protect them by checking the "origin" but this can be spoofed, I need some sort of token where the route checks for the token and the token is passed on the headers of the interaction that fetches the data, the problem is, I assume, that if the component is client side, then the client would be able to access this token, this is obviously not what I want. So how do I make a server side component on next js 13, that has buttons on the client side?! I hope this makes sense, I'm desperate with this. I know how stupid this sounds, my knowledge has gaps, please understand, I'm working on this. Thank you!
131 replies
RRailway
Created by 3arcus on 5/29/2023 in #✋|help
Cant load my metadata from xpress server due to cors policy
Access to XMLHttpRequest at 'https://metaserver-production.up.railway.app/nfts/0' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. This is a problem, how do I overcome this? Ty
12 replies
RRailway
Created by 3arcus on 5/28/2023 in #✋|help
Simple node express server not serving routes, no errors
very simple server.js file, this is my code, the app runs locally but not on railway, no routes are served
const express = require("express");
const app = express();
const fs = require("fs");
const path = require("path");

app.get("/:key", (req, res) => {
const key = req.params.key;
const filePath = path.join(__dirname, "db.json");
fs.readFile(filePath, (err, data) => {
if (err) {
console.error("Error reading file:", err);
return res.status(500).json({ error: "Internal server error" });
}

let jsonData;
try {
jsonData = JSON.parse(data);
} catch (err) {
console.error("Error parsing JSON:", err);
return res.status(500).json({ error: "Internal server error" });
}

if (!jsonData[key]) {
console.error("Key not found in JSON data:", key);
return res.status(404).json({ error: `Key "${key}" not found in data` });
}

res.json(jsonData[key]);
});
});

app.get("/:key/:id", (req, res) => {
const key = req.params.key;
const id = req.params.id;
const filePath = path.join(__dirname, "db.json");
fs.readFile(filePath, (err, data) => {
if (err) {
console.error(`Error reading file from disk: ${err}`);
return res.status(500).send(`Error reading file from disk: ${err}`);
}
let jsonData;
try {
jsonData = JSON.parse(data);
} catch (parseErr) {
console.error(`Error parsing JSON file: ${parseErr}`);
return res.status(500).send(`Error parsing JSON file: ${parseErr}`);
}

if (!jsonData[key]) {
console.error(`Key "${key}" not found in the JSON data.`);
return res.status(404).send(`Key "${key}" not found in the JSON data.`);
}

const item = jsonData[key].find((item) => item.id === parseInt(id));

if (!item) {
console.error(`Item with id "${id}" not found in the key "${key}".`);
return res
.status(404)
.send(`Item with id "${id}" not found in the key "${key}".`);
}

res.json(item);
});
});

app.listen(3000, () => {
console.log("Server started on port 3000");
});
const express = require("express");
const app = express();
const fs = require("fs");
const path = require("path");

app.get("/:key", (req, res) => {
const key = req.params.key;
const filePath = path.join(__dirname, "db.json");
fs.readFile(filePath, (err, data) => {
if (err) {
console.error("Error reading file:", err);
return res.status(500).json({ error: "Internal server error" });
}

let jsonData;
try {
jsonData = JSON.parse(data);
} catch (err) {
console.error("Error parsing JSON:", err);
return res.status(500).json({ error: "Internal server error" });
}

if (!jsonData[key]) {
console.error("Key not found in JSON data:", key);
return res.status(404).json({ error: `Key "${key}" not found in data` });
}

res.json(jsonData[key]);
});
});

app.get("/:key/:id", (req, res) => {
const key = req.params.key;
const id = req.params.id;
const filePath = path.join(__dirname, "db.json");
fs.readFile(filePath, (err, data) => {
if (err) {
console.error(`Error reading file from disk: ${err}`);
return res.status(500).send(`Error reading file from disk: ${err}`);
}
let jsonData;
try {
jsonData = JSON.parse(data);
} catch (parseErr) {
console.error(`Error parsing JSON file: ${parseErr}`);
return res.status(500).send(`Error parsing JSON file: ${parseErr}`);
}

if (!jsonData[key]) {
console.error(`Key "${key}" not found in the JSON data.`);
return res.status(404).send(`Key "${key}" not found in the JSON data.`);
}

const item = jsonData[key].find((item) => item.id === parseInt(id));

if (!item) {
console.error(`Item with id "${id}" not found in the key "${key}".`);
return res
.status(404)
.send(`Item with id "${id}" not found in the key "${key}".`);
}

res.json(item);
});
});

app.listen(3000, () => {
console.log("Server started on port 3000");
});
7 replies
RRailway
Created by 3arcus on 5/26/2023 in #✋|help
Deploying apps under the same domain
Hello, I have a railway app already configured with a domain, and I want to deploy another app to the same domain, is it possible to have it as a folder of the domain or does it need to be a sub domain? How would I set this up? Thank you
6 replies
RRailway
Created by 3arcus on 5/18/2023 in #✋|help
Trying to deploy Next js 13 app and its failing?
Hey guys, whats the process to deploy a simple Next js app? Version 13. Right now its failing. Is there specific steps? I'm just trying to deploy it from the github repo.
43 replies