App Explained
Repository Open Source
Sign In Front End
Sign In Handler Back End
Welcome and Refresh Handlers
Last updated
Was this helpful?
Last updated
Was this helpful?
Was this helpful?
function SignIn() {
const [userDetails, setUserDetails] = useContext(AppContext);
const [isUserWelcomed, setIsUserWelcomed] = useState(null);
const [wallet, setWallet] = useState(userDetails || null);
const authenticate = async () => {
const token = uuidv4();
openSignatureRequestPopup({
message: token,
network: new StacksTestnet(), // for mainnet, `new StacksMainnet()`
appDetails: {
name: "My Message Signing App",
icon: window.location.origin + "/my-app-logo.svg",
},
onFinish: async (data) => {
console.log("Signature of the message", data.signature);
console.log("Use public key:", data.publicKey);
console.log("To send", data);
// console.log("Addr", publicKeyToBtcAddress(data.publicKey), publicKeyToAddress(AddressVersion.TestnetMultiSig, data.publicKey));
console.log("Addr2", getAddressFromPublicKey(data.publicKey, TransactionVersion.Testnet))
try {
await axios.post('/signin', {token, publicKey: data.publicKey, signature: data.signature})
const updatedWallet = getAddressFromPublicKey(data.publicKey, TransactionVersion.Testnet);
setWallet(updatedWallet);
localStorage.setItem('userDetails', updatedWallet);
setUserDetails(updatedWallet);
setIsUserWelcomed(true);
} catch (e) {
console.error("Failed to login", e);
}
},
});
}const {verifyMessageSignatureRsv} = require("@stacks/encryption");
const signinHandler = (req, res) => {
const {token, signature, publicKey} = req.body
console.log(req.body);
if (!verifyMessageSignatureRsv({message: token, publicKey, signature})) {
// If the username isn't present, return an HTTP unauthorized code
res.status(401).end()
return
}
// set the expiry time as 120s after the current time
const now = new Date()
const expiresAt = new Date(+now + 120 * 1000)
const wallet = getAddressFromPublicKey(publicKey, TransactionVersion.Testnet);
// create a session containing information about the user and the expiry time
const session = new Session(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 time
res.cookie("session_token", token, {expires: expiresAt})
res.end()
}