Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(auth): catch error on invalid auth code #2654

Merged
merged 2 commits into from Sep 26, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 30 additions & 25 deletions src/panel/helpers/isUserLoggedIn.ts
Expand Up @@ -14,38 +14,43 @@ export const isUserLoggedIn = async function (token: string) {
window.location.replace(window.location.origin + '/login');
}
} else {
const axiosData = await axios.get(`https://api.twitch.tv/helix/users`, {
headers: {
'Authorization': 'Bearer ' + code,
},
});
const data = get(axiosData, 'data.data[0]', null);
if (data === null) {
try {
const axiosData = await axios.get(`https://api.twitch.tv/helix/users`, {
headers: {
'Authorization': 'Bearer ' + code,
},
});
const data = get(axiosData, 'data.data[0]', null);
if (data === null) {
throw Error('User must be logged');
}

// save userId to db
await new Promise((resolve) => {
const socket = io('/core/users', { query: 'token=' + token });
socket.emit('update', {
collection: '_users',
key: 'id',
items: [
{
id: data.id,
username: data.login,
},
],
}, (err, data) => {
resolve();
});
});
return data;
} catch(e) {
console.log('Redirecting, user code expired');
if (window.location.href.includes('popout')) {
window.location.replace(window.location.origin + '/login#error=popout+must+be+logged');
} else {
window.location.replace(window.location.origin + '/login');
}
return;
}

// save userId to db
await new Promise((resolve) => {
const socket = io('/core/users', { query: 'token=' + token });
socket.emit('update', {
collection: '_users',
key: 'id',
items: [
{
id: data.id,
username: data.login,
},
],
}, (err, data) => {
resolve();
});
});
return data;
}
};

Expand Down