Skip to content

Commit

Permalink
feat: Add session check functionality to App class and update session…
Browse files Browse the repository at this point in the history
… status redirects

- Added a `session` object to the `App` class with a `check` method to fetch the current session status.
  - The `check` method sends a GET request to `/sessions/current` and returns the session data or `null` if not authenticated.
- Updated `home.ejs`, `login.ejs`, `password-reset.ejs`, `register.ejs`, and `two-factor-authentication.ejs` to handle session status redirects:
  - Redirects to `/two-factor-authentication` if authenticated but 2FA is not verified.
  - Redirects to `/login` if not authenticated.
  • Loading branch information
TKanX committed Jun 22, 2024
1 parent 3b67cfb commit 9b77cc2
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 2 deletions.
28 changes: 28 additions & 0 deletions public/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,36 @@ class App {
*/
constructor(serverUrl) {
this.serverUrl = serverUrl;
this.session.serverUrl = serverUrl;
}

/**
* Session object.
* @type {object} session
*/
session = {
/**
* Check the current session.
* @returns {Promise<object>} The session data.
*/
async check() {
const response = await fetch(`${this.serverUrl}/sessions/current`, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});

const data = await response.json();

if (data.status === "success") {
return data.data;
} else {
return null;
}
},
};

/**
* Display a notification message in an element.
* @param {string} type - The type of notification.
Expand Down
12 changes: 12 additions & 0 deletions views/home.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,19 @@

<head>
<%- include("./components/head") %>
<script src="/js/app.js"></script>
<script src="/js/auth.js"></script>
<title>Tridecco</title>
<script>
(async () => {
const sessionStatus = await app.session.check();
if (sessionStatus.authenticated === true && sessionStatus.twoFactorAuthenticated === false) {
window.location.href = '/two-factor-authentication';
} else if (sessionStatus.authenticated === false) {
window.location.href = '/login';
}
})();
</script>
</head>

<body class="h-full">
Expand Down
10 changes: 10 additions & 0 deletions views/login.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
<script src="/js/app.js"></script>
<script src="/js/auth.js"></script>
<title>Login</title>
<script>
(async () => {
const sessionStatus = await app.session.check();
if (sessionStatus.authenticated === true && sessionStatus.twoFactorAuthenticated === true) {
window.location.href = '/';
} else if (sessionStatus.authenticated === true) {
window.location.href = '/two-factor-authentication';
}
})();
</script>
</head>

<body class="h-full">
Expand Down
10 changes: 10 additions & 0 deletions views/password-reset.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
<script src="/js/app.js"></script>
<script src="/js/auth.js"></script>
<title>Password Reset</title>
<script>
(async () => {
const sessionStatus = await app.session.check();
if (sessionStatus.authenticated === true && sessionStatus.twoFactorAuthenticated === true) {
window.location.href = '/';
} else if (sessionStatus.authenticated === true) {
window.location.href = '/two-factor-authentication';
}
})();
</script>
</head>

<body class="h-full">
Expand Down
10 changes: 10 additions & 0 deletions views/register.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
<script src="/js/app.js"></script>
<script src="/js/auth.js"></script>
<title>Register</title>
<script>
(async () => {
const sessionStatus = await app.session.check();
if (sessionStatus.authenticated === true && sessionStatus.twoFactorAuthenticated === true) {
window.location.href = '/';
} else if (sessionStatus.authenticated === true) {
window.location.href = '/two-factor-authentication';
}
})();
</script>
</head>

<body class="h-full">
Expand Down
14 changes: 12 additions & 2 deletions views/two-factor-authentication.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@
<%- include("./components/head") %>
<script src="/js/app.js"></script>
<script src="/js/auth.js"></script>
<title>Register</title>
<title>Two Factor Authentication</title>
<script>
(async () => {
const sessionStatus = await app.session.check();
if (sessionStatus.authenticated === true && sessionStatus.twoFactorAuthenticated === true) {
window.location.href = '/';
} else if (sessionStatus.authenticated === false) {
window.location.href = '/login';
}
})();
</script>
</head>

<body class="h-full">
Expand Down Expand Up @@ -53,7 +63,7 @@
</div>
<script>
const auth = new Auth();
const email = localStorage.getItem("userEmail");
const getCodeButton = document.querySelector('#get-code-button');
Expand Down

0 comments on commit 9b77cc2

Please sign in to comment.