Handle authenticatoin

pull/3813/head
Kamran Ahmed 2 years ago
parent 0389cd8f51
commit 332c71c16e
  1. 0
      src/components/AuthenticationFlow/Divider.astro
  2. 42
      src/components/Authenticator/authenticator.ts
  3. 2
      src/components/Login/LoginCopmponent.astro
  4. 10
      src/pages/forgot-password.astro
  5. 2
      src/pages/login.astro
  6. 2
      src/pages/signup.astro

@ -1,38 +1,48 @@
import Cookies from 'js-cookie'; import Cookies from 'js-cookie';
import { TOKEN_COOKIE_NAME } from '../../lib/constants'; import { TOKEN_COOKIE_NAME } from '../../lib/constants';
/** function showHideAuthElements(hideOrShow: 'hide' | 'show' = 'hide') {
* Prepares the UI for the user who is logged in
*/
function handleLoggedOut() {
document.querySelectorAll('[data-auth-required]').forEach((el) => { 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) => { 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() { function handleGuest() {
document.querySelectorAll('[data-auth-required]').forEach((el) => { showHideAuthElements('hide');
el.classList.remove('hidden'); 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() { function handleAuthRequired() {
const token = Cookies.get(TOKEN_COOKIE_NAME); const token = Cookies.get(TOKEN_COOKIE_NAME);
if (token) { if (token) {
handleLoggedIn(); handleAuthenticated();
} else { } else {
handleLoggedOut(); handleGuest();
} }
} }

@ -1,5 +1,5 @@
--- ---
import Divider from './Divider.astro'; import Divider from '../AuthenticationFlow/Divider.astro';
import EmailLoginForm from '../AuthenticationFlow/EmailLoginForm'; import EmailLoginForm from '../AuthenticationFlow/EmailLoginForm';
import GithubLogin from './GithubLogin.astro'; import GithubLogin from './GithubLogin.astro';
import GoogleLogin from './GoogleLogin.astro'; import GoogleLogin from './GoogleLogin.astro';

@ -16,13 +16,3 @@ import SettingLayout from '../layouts/SettingLayout.astro';
</div> </div>
</div> </div>
</SettingLayout> </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>

@ -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 { GitHubButton } from '../components/AuthenticationFlow/GitHubButton';
import { GoogleButton } from '../components/AuthenticationFlow/GoogleButton'; import { GoogleButton } from '../components/AuthenticationFlow/GoogleButton';
import EmailLoginForm from '../components/AuthenticationFlow/EmailLoginForm'; import EmailLoginForm from '../components/AuthenticationFlow/EmailLoginForm';

@ -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 GoogleLogin from '../components/Login/GoogleLogin.astro';
import EmailSignupForm from '../components/AuthenticationFlow/EmailSignupForm'; import EmailSignupForm from '../components/AuthenticationFlow/EmailSignupForm';
import SettingLayout from '../layouts/SettingLayout.astro'; import SettingLayout from '../layouts/SettingLayout.astro';

Loading…
Cancel
Save