Skip to content

Commit d73005b

Browse files
committed
Refactored BungieLogin, enabled token refresh logic, modified App to conditionally render content if logged in
1 parent 720e38f commit d73005b

File tree

3 files changed

+54
-25
lines changed

3 files changed

+54
-25
lines changed

src/App.jsx

+23-14
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,30 @@ const RightPane = styled('div')({
5858
function App() {
5959
return (
6060
<Container>
61-
<BungieLogin />
62-
<HeaderContainer>
63-
<SingleDiamondButton />
64-
</HeaderContainer>
65-
<ContentContainer>
66-
<LeftPane>
67-
<NumberBoxes />
68-
</LeftPane>
69-
<RightPane>
70-
<h1 style={{ fontSize: '16px' }}>Armour Combinations</h1>
71-
<StatsTable />
72-
</RightPane>
73-
</ContentContainer>
61+
<BungieLogin>{({ isLoggedIn }) => (
62+
<div>
63+
{ isLoggedIn ? (
64+
<Container>
65+
<HeaderContainer>
66+
<SingleDiamondButton />
67+
</HeaderContainer>
68+
<ContentContainer>
69+
<LeftPane>
70+
<NumberBoxes />
71+
</LeftPane>
72+
<RightPane>
73+
<h1 style={{ fontSize: '16px' }}>Armour Combinations</h1>
74+
<StatsTable />
75+
</RightPane>
76+
</ContentContainer>
77+
</Container>
78+
) : (
79+
<div> Please log in to continue...</div>
80+
)}
81+
</div>
82+
)}
83+
</BungieLogin>
7484
</Container>
75-
7685
);
7786
}
7887

+26-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,35 @@
11
import BungieAuth from "../../../lib/BungieAuth"
2-
import React, { useEffect } from "react"
2+
import React, { Component } from "react"
33

4-
export default () => {
4+
class BungieLogin extends Component {
5+
constructor(props) {
6+
super(props)
57

6-
function onLogIn() {
7-
BungieAuth.authenticate()
8+
this.state = {
9+
isLoggedIn: BungieAuth.isAuthenticated()
10+
}
811
}
912

10-
useEffect ( () => {
13+
componentDidMount() {
1114
if (BungieAuth.setAuthCode()) {
1215
BungieAuth.generateToken()
1316
}
14-
}, [])
17+
}
18+
19+
onLogIn () {
20+
BungieAuth.authenticate()
21+
}
22+
23+
onLogOut () {
24+
// TODO: log out
25+
}
26+
27+
render() {
28+
return <div>
29+
{ this.props.children({ isLoggedIn: this.state.isLoggedIn }) }
30+
{ this.state.isLoggedIn ? ( <button onClick={this.onLogOut}>Log Out</button> ) : ( <button onClick={this.onLogIn}>Log In</button> ) }
31+
</div>
32+
}
33+
}
1534

16-
return BungieAuth.isAuthenticated() ? null : ( <button onClick={onLogIn}>Log In</button> )
17-
}
35+
export default BungieLogin

src/lib/BungieAuth.jsx

+5-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const BungieAuth = {
2525
})
2626

2727
if (refreshToken && Date.now() < refreshTokenExpiringAt && Date.now() > lastRefresh + timing) {
28-
return generateToken(true)
28+
return BungieAuth.generateToken(true)
2929
}
3030

3131
return true
@@ -52,15 +52,17 @@ const BungieAuth = {
5252
localStorage.setItem("refreshToken", response.data.refresh_token)
5353
localStorage.setItem("refreshTokenExpiringAt", Date.now() + response.data.refresh_expires_in * 1000 - 10 * 1000)
5454
localStorage.setItem("lastRefresh", Date.now())
55+
return true
5556
}
5657
else {
57-
return new Error('Could not get access token')
58+
console.log("Could not get access token")
59+
return false
5860
}
5961
})
6062
},
6163

6264
isAuthenticated: () => {
63-
return (localStorage.getItem("accessToken") !== null)
65+
return (localStorage.getItem("accessToken") !== null && BungieAuth.autoRegenerateTokens())
6466
},
6567

6668
setAuthCode: () => {

0 commit comments

Comments
 (0)