fix login flow

This commit is contained in:
Alberto Xamin 2023-01-03 16:15:11 +00:00
parent 8d90935e7d
commit 7cba6fc7ef
3 changed files with 14 additions and 10 deletions

View File

@ -50,7 +50,8 @@ class Player:
if r.status_code == 200:
res = r.json()
self.avatar = f'https://cdn.discordapp.com/avatars/{res["id"]}/{res["avatar"]}.png'
self.sio.emit('chat_message', room=self.game.name, data=f'_change_username|{self.name}|{res["username"]}')
if self.game:
self.sio.emit('chat_message', room=self.game.name, data=f'_change_username|{self.name}|{res["username"]}')
self.name = res['username']
self.discord_id = res['id']
if self.is_admin():

View File

@ -96,7 +96,7 @@ def set_username(sid, username):
username = username["name"]
print(f'{sid} changed username to {username}')
prev = ses.name
if len([p for p in ses.game.players if p.name == username]) > 0:
if ses.game and len([p for p in ses.game.players if p.name == username]) > 0:
ses.name = f"{username}_{random.randint(0,100)}"
else:
ses.name = username

View File

@ -115,7 +115,7 @@ export default {
discord_auth_succ(data) {
if (data.access_token) {
localStorage.setItem('discord_token', data.access_token)
this.$$router.push({path:'/'})
this.login()
}
},
},
@ -157,13 +157,7 @@ export default {
init() {
location.reload();
},
},
mounted() {
if (this.$route.query.code) {
this.$socket.emit('discord_auth', {code:this.$route.query.code, origin:window.location.origin})
}
else if (localStorage.getItem('discord_token')) {
//get username from discord
login() {
fetch('https://discordapp.com/api/users/@me', {
headers: {
'Authorization': 'Bearer ' + localStorage.getItem('discord_token')
@ -179,8 +173,17 @@ export default {
}).catch(err => {
console.error(err)
localStorage.removeItem('discord_token')
this.$router.replace({query: []})
})
}
},
mounted() {
if (localStorage.getItem('discord_token')) {
this.login()
} else if (this.$route.query.code) {
this.$socket.emit('discord_auth', {code:this.$route.query.code, origin:window.location.origin})
this.$router.replace({query: []})
}
this.randomTip = `tip_${1+Math.floor(Math.random() * 8)}`
if (localStorage.getItem('username'))
this.username = localStorage.getItem('username')