import React from 'react'; import {App} from './App'; import {baseURL} from './Meshify'; export class Auth extends React.Component{ constructor(props){ super(props); this.state = {login: false}; this.login = this.login.bind(this); this.logout = this.logout.bind(this); this.getLogin = this.getLogin.bind(this); this.handleAuthInput = this.handleAuthInput.bind(this); } handleAuthInput(e){ this.setState({[e.target.name]: e.target.value}); } componentDidMount() { this.getLogin(); } logout(){ fetch(baseURL + 'logout', {credentials: 'include'}).then(()=>{ this.setState({login: false, meshifyUsername: null, meshifyPassword: null}); }) } getLogin(){ console.log("getting login"); // this.setState({login: true, meshifyUsername: 'api@henry-pump.com'}); fetch(baseURL + 'login', {credentials: 'include'}).then((response)=>response.json()) .then((responseJson)=>{ // console.log(responseJson) if (typeof responseJson.username !== 'undefined'){ this.setState({login: true, meshifyUsername: responseJson.username}) } else { this.setState({login: false}) } }) .catch((error) => { console.error(error); }); } login(){ const authToken = "Basic " + btoa(this.state.meshifyUsername + ":" + this.state.meshifyPassword); this.setState({authToken: authToken}); fetch(baseURL + 'login', { method:'post', credentials: 'include', headers:{ 'Content-Type': 'application/json' }, body: JSON.stringify({username: this.state.meshifyUsername, authToken: authToken}) }).then((response)=>response.json()) .then((responseJson)=>{ // console.log(responseJson); if (responseJson.status === "success"){ this.setState({login: true, meshifyUsername: responseJson.username}) } else if (responseJson.status === 'failure'){ alert(responseJson.reason); } // this.getLogin(); }) } render() { if(this.state.login){ return (
) } else { return (
) } } }