AuthButton.jsx 1.12 KB
Newer Older
David Haynes's avatar
David Haynes committed
1 2
import React from "react";
import { Button } from "reactstrap";
Zach Osman's avatar
Cleanup  
Zach Osman committed
3

David Haynes's avatar
David Haynes committed
4 5 6 7 8
class AuthButton extends React.Component {
  constructor(props) {
    super(props);
    this.state = { error: null, is_auth: false };
  }
Zach Osman's avatar
Cleanup  
Zach Osman committed
9

David Haynes's avatar
David Haynes committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
  componentDidMount() {
    fetch("/auth/status")
      .then(res => res.json())
      .then(
        result => {
          this.setState({
            is_auth: result.is_authenticated
          });
        },
        error => {
          this.setState({
            error
          });
        }
      );
  }
Zach Osman's avatar
Cleanup  
Zach Osman committed
26

David Haynes's avatar
David Haynes committed
27 28 29
  render() {
    const { is_auth, error } = this.state;
    if (error) {
30 31 32 33 34
      return (
        <Button outline color="accent">
          Error: {error.message}
        </Button>
      );
David Haynes's avatar
David Haynes committed
35 36 37 38
    } else {
      return (
        <div>
          {is_auth ? (
David Haynes's avatar
David Haynes committed
39
            <Button outline color="accent" href="/auth/logout">
40
              Logout <i className="fas fa-sign-out-alt" />
David Haynes's avatar
David Haynes committed
41 42
            </Button>
          ) : (
David Haynes's avatar
David Haynes committed
43
            <Button outline color="accent" href="/auth/login">
44
              Login <i className="fas fa-sign-in-alt" />
David Haynes's avatar
David Haynes committed
45 46 47 48
            </Button>
          )}
        </div>
      );
Zach Osman's avatar
Cleanup  
Zach Osman committed
49
    }
David Haynes's avatar
David Haynes committed
50
  }
Zach Osman's avatar
Cleanup  
Zach Osman committed
51
}
David Haynes's avatar
David Haynes committed
52 53

export default AuthButton;