Skip to content

Commit

Permalink
Merge pull request #124 from BeyondPong/dev
Browse files Browse the repository at this point in the history
Dev -> Main
  • Loading branch information
mixsung authored Jul 9, 2024
2 parents 8f8d14f + 39b7192 commit d002638
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
28 changes: 28 additions & 0 deletions frontend/static/js/api/getAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,34 @@ import { env } from '../utility/env';
const API = env.API_URL;
const getToken = (key) => encodeURIComponent(localStorage.getItem(key));

export const getLoginURI = async () => {
try {
const response = await fetch(`${API}/login/oauth/`, {
method: 'GET',
});
const data = await response.json();
return data;
} catch (error) {
console.log('Failed to get login URI: ', error);
}
};

export const getMultipleLogin = async () => {
try {
const response = await fetch(`${API}/login/multiple/`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${getToken('token')}`,
},
});
const data = await response.json();
return data;
} catch (error) {
console.log('Failed to get multiple login: ', error);
}
};

export const getRegistration = async () => {
try {
const response = await fetch(`${API}/login/registration/`, {
Expand Down
19 changes: 17 additions & 2 deletions frontend/static/js/router/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import pathToRegex from '../utility/pathToRegex.js';
import getParams from '../utility/getParams.js';
import navigateTo from '../utility/navigateTo.js';
import updateBackground from '../utility/updateBackground.js';
import WebSocketManager from '../state/WebSocketManager.js';
import { getLoginURI } from '../api/getAPI.js';
import { postLoginCode } from '../api/postAPI.js';
import { removeBlurBackground } from '../utility/blurBackGround.js';
import { checkLogin } from '../utility/checkLogin.js';
import { check2FAStatus } from '../utility/check2FA.js';
import WebSocketManager from '../state/WebSocketManager.js';
import { checkMultipleLogin } from '../utility/checkMultipleLogin.js';
import { env } from '../utility/env.js';

export class Router {
Expand Down Expand Up @@ -106,6 +108,12 @@ export class Router {
window.location.href = '/notlogin';
return;
}
if (checkMultipleLogin() === true) {
localStorage.clear();
alert('You are already logged in another device');
window.location.href = '/';
return;
}
updateBackground('normal');
removeBlurBackground();
await this.render(match);
Expand All @@ -132,6 +140,12 @@ export class Router {

async handleMainRoute(match) {
if (checkLogin() === true) {
if (checkMultipleLogin() === true) {
localStorage.clear();
alert('You are already logged in another device');
window.location.href = '/';
return;
}
if (check2FAStatus() === false) {
window.location.href = '/2fa';
return;
Expand All @@ -152,7 +166,8 @@ export class Router {
if (localStorage.getItem('token') !== null) {
window.location.href = '/';
} else if (!localStorage.getItem('token')) {
window.location.href = env.LOGIN_REDIRECT_URL;
const loginURL = await getLoginURI();
window.location.href = loginURL.redirect_url;
}
}

Expand Down
6 changes: 6 additions & 0 deletions frontend/static/js/utility/checkMultipleLogin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { getMultipleLogin } from '../api/getAPI';

export const checkMultipleLogin = async () => {
const data = await getMultipleLogin();
return data.is_multiple;
};

0 comments on commit d002638

Please sign in to comment.