openSignatureRequestPopup creates the necessary data after the pop-up is confirmed. The data contains the publicKey and signature, both for the previous token created.
Then it saves the Stacks address locally and sends to the backend the token, publicKey and signature to the route signin.
Sign In Handler Back End
Gets the token, signature and publicKey from the client, then verifies the message signature using verifyMessageSignatureRsv from the @stacks/encryption library.
If it fails, sends a 401 error status back, else create the cookie session which will be available for 120 seconds.
const {verifyMessageSignatureRsv} =require("@stacks/encryption");constsigninHandler= (req, res) => {const {token,signature,publicKey} =req.bodyconsole.log(req.body);if (!verifyMessageSignatureRsv({message: token, publicKey, signature})) {// If the username isn't present, return an HTTP unauthorized coderes.status(401).end()return }// set the expiry time as 120s after the current timeconstnow=newDate()constexpiresAt=newDate(+now +120*1000)constwallet=getAddressFromPublicKey(publicKey,TransactionVersion.Testnet);// create a session containing information about the user and the expiry timeconstsession=newSession(wallet, expiresAt)// add the session information to the sessions map sessions[token] = session;// In the response set a cookie on the client with the name "session_cookie"// and the value as the UUID we generated. We also set the expiry timeres.cookie("session_token", token, {expires: expiresAt})res.end()}
Welcome and Refresh Handlers
After the user gets a cookie session attributed, if he reloads the page or comes back later to the page, the /welcome request will be made. The server checks that there are cookies, the session_token cookie exists, the userSession exists and it didn't expire and sends as confirmation, if all of them are alright, a message Welcome address. If any of the conditions was not met, the server returns the 401 error status.
This can be placed on any action done on the client side. If the client app has an action to collect resources, it will send a message to the server checking if the session is valid and if it is, will collect the resources. If not, will ask the user to confirm his identity again.