AuthButton.jsx 986 Bytes
Newer Older
David Haynes's avatar
David Haynes committed
1
2
3
4
5
6
import React from "react";
import { Button } from "reactstrap";

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

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

  render() {
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
    const { is_auth, error } = this.state;
    if (error) {
      return <div>Error: {error.message}</div>;
    } else {
      return (
        <div>
          {is_auth ? (
            <Button color="info" href="/auth/logout">
              Logout
            </Button>
          ) : (
            <Button color="info" href="/auth/login">
              Login
            </Button>
          )}
        </div>
      );
    }
David Haynes's avatar
David Haynes committed
46
47
48
49
  }
}

export default AuthButton;