0

I'm making a simple web server with Node.js and Express. I'm hosting many static files, including a Unity WebGL game.

For the game to work properly, many assets need to be loaded. Is there some kind of easy way I could store a cookie with a password (on a login page), and if the password cookie is present and has the right value, all requests are accepted?

Still very new to web server stuff, all I've done so far is Discord bots.

So basically all I'm trying to do is have a simple password page (and maybe have some kind of a delay when it checks if the password is correct) and then let you load things in one folder.

Hope that's easy to do. Sorry if my question is dumb.

Alex_Sour
  • 43
  • 7
  • This might help: [Documentation for Auth](https://stackoverflow.com/a/14301657/16010779) – zackaria_asks Apr 18 '22 at 03:50
  • I'm not using passport.js and it seems to use user systems, I'm just looking to have a password, no username – Alex_Sour Apr 18 '22 at 04:06
  • just to gather more info - you are creating a unity game which will call a nodejs/express API for authentication - this nodejs/express API will respond back with static files after successful authentication, and load files into the unity game correct? – alilland Apr 18 '22 at 04:57
  • If so, I suggest learning about JWT tokens, have a express route that validates the password and returns a JWT token, require the JWT token in every request to retrieve static files as a header in the request – alilland Apr 18 '22 at 05:04

1 Answers1

2

Setting a cookie

to set a cookie you can use something like expresses cookie-parser middle-ware check it here

Giving access

you can send some kind of uuid or access token from the client side using something like fetch api or an ordinary XMLHttpRequest refer here and use some logic to give access to the file

code

var express = require('express')
var cookieParser = require('cookie-parser')

var app = express()
app.use(cookieParser())

app.get('/getfile', (req, res) => {
    const code = req.cookies.mysecertcode;
    if(code = secert) {
         res.send("correct token")
    } else {
         res.send("incorrect token")
    }

})

app.listen(8080)

this code is just a simple code I made you can use your own

Hope this can help you thank you

net-js
  • 119
  • 1
  • 8