Skip to content
This repository has been archived by the owner on Dec 19, 2024. It is now read-only.

Commit

Permalink
Handle authenticatoin
Browse files Browse the repository at this point in the history
  • Loading branch information
kamranahmedse committed Apr 9, 2023
1 parent 0389cd8 commit 332c71c
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 29 deletions.
42 changes: 26 additions & 16 deletions src/components/Authenticator/authenticator.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,48 @@
import Cookies from 'js-cookie';
import { TOKEN_COOKIE_NAME } from '../../lib/constants';

/**
* Prepares the UI for the user who is logged in
*/
function handleLoggedOut() {
function showHideAuthElements(hideOrShow: 'hide' | 'show' = 'hide') {
document.querySelectorAll('[data-auth-required]').forEach((el) => {
el.classList.add('hidden');
if (hideOrShow === 'hide') {
el.classList.add('hidden');
} else {
el.classList.remove('hidden');
}
});
}

function showHideGuestElements(hideOrShow: 'hide' | 'show' = 'hide') {
document.querySelectorAll('[data-guest-required]').forEach((el) => {
el.classList.remove('hidden');
if (hideOrShow === 'hide') {
el.classList.add('hidden');
} else {
el.classList.remove('hidden');
}
});
}

/**
* Prepares the UI for the user who is logged out
* Prepares the UI for the user who is logged in
*/
function handleLoggedIn() {
document.querySelectorAll('[data-auth-required]').forEach((el) => {
el.classList.remove('hidden');
});
function handleGuest() {
showHideAuthElements('hide');
showHideGuestElements('show');
}

document.querySelectorAll('[data-guest-required]').forEach((el) => {
el.classList.add('hidden');
});
/**
* Prepares the UI for the user who is logged out
*/
function handleAuthenticated() {
showHideAuthElements('show');
showHideGuestElements('hide');
}

function handleAuthRequired() {
const token = Cookies.get(TOKEN_COOKIE_NAME);
if (token) {
handleLoggedIn();
handleAuthenticated();
} else {
handleLoggedOut();
handleGuest();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/Login/LoginCopmponent.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
import Divider from './Divider.astro';
import Divider from '../AuthenticationFlow/Divider.astro';
import EmailLoginForm from '../AuthenticationFlow/EmailLoginForm';
import GithubLogin from './GithubLogin.astro';
import GoogleLogin from './GoogleLogin.astro';
Expand Down
10 changes: 0 additions & 10 deletions src/pages/forgot-password.astro
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,3 @@ import SettingLayout from '../layouts/SettingLayout.astro';
</div>
</div>
</SettingLayout>

<script>
import Cookies from 'js-cookie';
import { TOKEN_COOKIE_NAME } from '../lib/constants';
const token = Cookies.get(TOKEN_COOKIE_NAME);

if (token) {
window.location.href = '/';
}
</script>
2 changes: 1 addition & 1 deletion src/pages/login.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
import Divider from '../components/Login/Divider.astro';
import Divider from '../components/AuthenticationFlow/Divider.astro';
import { GitHubButton } from '../components/AuthenticationFlow/GitHubButton';
import { GoogleButton } from '../components/AuthenticationFlow/GoogleButton';
import EmailLoginForm from '../components/AuthenticationFlow/EmailLoginForm';
Expand Down
2 changes: 1 addition & 1 deletion src/pages/signup.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
import Divider from '../components/Login/Divider.astro';
import Divider from '../components/AuthenticationFlow/Divider.astro';
import GoogleLogin from '../components/Login/GoogleLogin.astro';
import EmailSignupForm from '../components/AuthenticationFlow/EmailSignupForm';
import SettingLayout from '../layouts/SettingLayout.astro';
Expand Down

0 comments on commit 332c71c

Please sign in to comment.