AuthButton.jsx 1.04 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
30
31
32
33
34
35
  render() {
    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">
36
              Logout <i className="fas fa-sign-out-alt" />
David Haynes's avatar
David Haynes committed
37
38
39
            </Button>
          ) : (
            <Button color="info" href="/auth/login">
40
              Login <i className="fas fa-sign-in-alt" />
David Haynes's avatar
David Haynes committed
41
42
43
44
            </Button>
          )}
        </div>
      );
Zach Osman's avatar
Cleanup    
Zach Osman committed
45
    }
David Haynes's avatar
David Haynes committed
46
  }
Zach Osman's avatar
Cleanup    
Zach Osman committed
47
}
David Haynes's avatar
David Haynes committed
48
49

export default AuthButton;