summaryrefslogtreecommitdiff
path: root/animism-align/frontend/app/views/auth/auth.gate.js
blob: ba692565854daadaab557a0bec9db69188791d3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import React, { Component } from 'react'
import { connect } from 'react-redux'

import './auth.css'

import actions from 'app/actions'

import AuthLogin from './auth.login'

class AuthGate extends Component {
  constructor(props) {
    super(props)
    actions.auth.load_access_token()
  }

  componentDidUpdate(prevProps) {
    if (this.props.user_id !== prevProps.user_id) {
      this.load()
    }
  }

  load() {
    if (!this.props.user_id) return
    actions.user.show(this.props.user_id)
    .then(() => {
      actions.site.loadProject()
    }).catch(error => {
      if (error.status_code === 401) {
        actions.auth.logout()
      } else {
        console.error(error)
      }
    })
  }

  render() {
    if (this.props.user) {
      return this.props.children
    }
    return (
      <div className='auth'>
        {this.props.logged_in
          ? <div className="login">Logging in...</div>
          : <AuthLogin onAuthenticate={this.load} />
        }
      </div>
    )
  }
}

const mapStateToProps = state => ({
  ...state.auth,
})

export default connect(mapStateToProps)(AuthGate)