Skip to content

Commit

Permalink
Fixes 401 auth race condition on reload
Browse files Browse the repository at this point in the history
  • Loading branch information
mrharpo committed Jan 9, 2024
1 parent 2c997a6 commit 3beb81c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/Games.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const Games = (state, actions) => {
return {
listener: null,
oninit: vnode => {
if (!state.loggedIn) {
if (!state.loggedIn()) {
m.route.set('/login')
}
state.invert(false)
Expand Down Expand Up @@ -192,7 +192,7 @@ export const Opponent = state =>

export const GamePageOnline = (state, actions) => ({
oninit: vnode => {
if (!state.loggedIn) {
if (!state.loggedIn()) {
console.log('not logged in. Redirecting to login')
m.route.set('/login')
}
Expand Down
66 changes: 34 additions & 32 deletions src/LaunchGame.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,44 +207,46 @@ export const LaunchGame = (state, actions) => ({
actions.lightBoard()
},
streamGame: () => {
if (!state.loggedIn) {
if (!state.loggedIn()) {
m.route.set('/login')
}
console.log('streaming game', m.route.param('id'))
console.log('not logged in. Redirecting to login page')
} else {
console.log('streaming game', m.route.param('id'), state.loggedIn())

state.auth.openStream('/api/board/game/stream/' + m.route.param('id'), {}, v => {
console.log('calling back', v)
if (v.type == 'gameFull') {
state.game = v
// TODO: change to Stream()
// m.redraw()
console.log('loading game', v.state.moves)
console.log(
'loaded?',
state.chess.loadPgn(v.state.moves, { sloppy: true })
)
if (v.black.id == state.user.profile.id && !state.invert()) {
// if playing black, and not already inverted, flip board
actions.flipBoard()
state.color = 'b'
} else {
state.color = 'w'
state.auth.openStream('/api/board/game/stream/' + m.route.param('id'), {}, v => {
console.log('calling back', v)
if (v.type == 'gameFull') {
state.game = v
// TODO: change to Stream()
// m.redraw()
console.log('loading game', v.state.moves)
console.log(
'loaded?',
state.chess.loadPgn(v.state.moves, { sloppy: true })
)
if (v.black.id == state.user.profile.id && !state.invert()) {
// if playing black, and not already inverted, flip board
actions.flipBoard()
state.color = 'b'
} else {
state.color = 'w'
}
} else if (v.type == 'gameState') {
console.log('move played', v.moves)
console.log(
'loaded?',
state.chess.loadPgn(v.moves, { sloppy: true })
)
}
} else if (v.type == 'gameState') {
console.log('move played', v.moves)
console.log(
'loaded?',
state.chess.loadPgn(v.moves, { sloppy: true })
)
}
let turn = state.chess.turn() == 'w' ? 'white' : 'black'
console.log('updated. turn is', turn)
let turn = state.chess.turn() == 'w' ? 'white' : 'black'
console.log('updated. turn is', turn)

setBoard(state.chess, state.ground)
setBoard(state.chess, state.ground)

actions.lightBoard(true)
actions.lightBoard(true)
}
)
}
)
},
newGame: () => {
state.chess = new Chess()
Expand Down
5 changes: 2 additions & 3 deletions src/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const UserActions = (state, actions) => ({
console.log('logging in')
state.auth.login()
console.log('logged in as: ', state.user.username)
state.user.loggedIn = true
state.loggedIn(true)
if (m.route.param('redirect')) {
m.route.set(m.route.param('redirect'))
} else if (m.route.get() == '/login') {
Expand All @@ -21,15 +21,14 @@ export const UserActions = (state, actions) => ({
console.log('logging out')
state.auth.logout()
state.user.username = null
state.user.loggedIn = false
state.loggedIn(false)
window.localStorage.setItem('CREDENTIALS_FLUSH', Date.now().toString())
window.localStorage.removeItem('CREDENTIALS_FLUSH')
// m.redraw()
},
initAuth: async () => {
if (localStorage.getItem('me')) {
state.user = JSON.parse(localStorage.getItem('me'))
state.user.loggedIn = true
console.log('loaded user from localstorage', state.user)
}
state.auth = Auth(state, actions)
Expand Down

0 comments on commit 3beb81c

Please sign in to comment.