import React, { Component } from "react"; import { Button , Grid, Typography, TextField, Select, FormHelperText, MenuItem, FormControl, Radio, FormControlLabel, RadioGroup, Menu} from "@material-ui/core" import { Link } from 'react-router-dom' import Image from 'material-ui-image' export default class UserGenPage extends Component { constructor(props) { super(props); this.state = { token: this.genBase62Token(32), }; this.getGenerateUser(); } // sort of cryptographically strong function to generate Base62 token client-side genBase62Token(length) { return window.btoa(Array.from( window.crypto.getRandomValues( new Uint8Array(length * 2))) .map((b) => String.fromCharCode(b)) .join("")).replace(/[+/]/g, "") .substring(0, length); } getGenerateUser() { fetch('/api/usergen' + '?token=' + this.state.token) .then((response) => response.json()) .then((data) => { this.setState({ nickname: data.nickname, bit_entropy: data.token_bits_entropy, avatar_url: 'static/assets/avatars/' + data.nickname + '.png', shannon_entropy: data.token_shannon_entropy, bad_request: data.bad_request, found: data.found, }); }); } // Fix next two handler functions so they work sequentially // at the moment they make the request generate a new user in parallel // to updating the token in the state. So the it works a bit weird. handleAnotherButtonPressed=(e)=>{ this.setState({ token: this.genBase62Token(32), }) this.getGenerateUser(); } handleChangeToken=(e)=>{ this.setState({ token: e.target.value, }) this.getGenerateUser(); } render() { return (
{this.state.nickname ? "⚡"+this.state.nickname+"⚡" : ""} { this.state.found ? We found your robosat, welcome back!
: }
); } }