Skip to content

Commit 17c5700

Browse files
committed
include logout
1 parent f7a564b commit 17c5700

7 files changed

Lines changed: 41 additions & 7 deletions

File tree

‎apps/codingcatdev/src/lib/client/firebase.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { initializeApp, getApps } from 'firebase/app';
2-
import { getAuth, setPersistence, browserSessionPersistence, signInWithEmailAndPassword } from 'firebase/auth';
2+
import { getAuth, setPersistence, browserSessionPersistence, signInWithEmailAndPassword, signInWithPopup, type AuthProvider } from 'firebase/auth';
33

44
import {
55
PUBLIC_FB_API_KEY,
@@ -35,4 +35,8 @@ export const ccdSignInWithEmailAndPassword = async ({ email, password }: { email
3535
const userResponse = await signInWithEmailAndPassword(auth, email, password);
3636
const idToken = await userResponse.user.getIdToken();
3737
document.cookie = '__ccdlogin=' + idToken + ';max-age=3600';
38+
}
39+
40+
export const ccdSignInWithPopUp = async (provider: AuthProvider) => {
41+
signInWithPopup(auth, provider)
3842
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<script lang="ts">
22
import LayoutWrapper from '../../(layout-partials)/LayoutWrapper.svelte';
3+
import LogoutButton from '../../login/LogoutButton.svelte';
34
import type { PageData } from './$types';
45
56
export let data: PageData;
67
</script>
78

89
<LayoutWrapper>
910
<h1>{data.user?.email}</h1>
11+
<LogoutButton />
1012
</LayoutWrapper>

‎apps/codingcatdev/src/routes/+layout.svelte‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
import type { LayoutData } from './$types';
33
export let data: LayoutData;
44
5-
console.log(data);
6-
75
// Core
86
import { page } from '$app/stores';
97
import { afterNavigate } from '$app/navigation';
Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { ccdCreateSessionCookie } from '$lib/server/firebase';
2-
import type { Actions } from './$types';
1+
import { ccdCreateSessionCookie, ccdValidateSessionCookie } from '$lib/server/firebase';
2+
import type { Actions, PageServerLoad } from './$types';
33
import { fail, redirect } from '@sveltejs/kit'
44

55
export const actions = {
6-
default: async ({ cookies, url }) => {
6+
login: async ({ cookies, url }) => {
77
const ccdLoginIdToken = cookies.get('__ccdlogin');
88

99
if (!ccdLoginIdToken) {
@@ -15,10 +15,21 @@ export const actions = {
1515
return fail(400, { incorrect: true });
1616
}
1717
cookies.set(sessionCookie.name, sessionCookie.sessionCookie, sessionCookie.options);
18+
cookies.set('__ccdlogin', '', { expires: new Date(Date.now() - 3600) });
1819

1920
if (url.searchParams.has('redirectTo')) {
2021
throw redirect(303, url.searchParams.get('redirectTo') || '/');
2122
}
2223
throw redirect(303, '/');
24+
},
25+
logout: async ({ cookies }) => {
26+
console.debug('logging out')
27+
cookies.set('session', '', { expires: new Date(Date.now() - 3600) });
28+
throw redirect(303, '/');
2329
}
24-
} satisfies Actions;
30+
} satisfies Actions;
31+
32+
export const load = (async ({ parent }) => {
33+
const { user } = await parent();
34+
if (user) throw redirect(303, '/account');
35+
}) satisfies PageServerLoad;

‎apps/codingcatdev/src/routes/login/+page.svelte‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88
99
import { toastStore } from '@codingcatdev/blackcatui';
1010
11+
import GoogleAuth from './GoogleAuth.svelte';
12+
1113
export let form: ActionData;
1214
</script>
1315

1416
<LayoutWrapper>
1517
<form
1618
class="flex justify-center w-full"
1719
method="POST"
20+
action="?/login"
1821
use:enhance={async ({ action, cancel, controller, data, form }) => {
1922
// `form` is the `<form>` element
2023
// `data` is its `FormData` object
@@ -60,6 +63,7 @@
6063
<input class="input" name="password" type="password" placeholder="****" />
6164
</label>
6265
<button class="bcu-button variant-filled-primary" type="submit">Login</button>
66+
<GoogleAuth />
6367
</div>
6468
</form>
6569
</LayoutWrapper>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script lang="ts">
2+
import { Icon } from '@steeze-ui/svelte-icon';
3+
import { Google } from '@steeze-ui/simple-icons';
4+
5+
import { GoogleAuthProvider } from 'firebase/auth';
6+
const provider = new GoogleAuthProvider();
7+
</script>
8+
9+
<button class="bcu-button flex gap-2 variant-ringed-primary" type="button">
10+
<Icon src={Google} class="w-4" />
11+
<div class="text-lg">Google</div>
12+
</button>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<form method="POST" action="/login?/logout">
2+
<button class="bcu-button variant-filled-primary">Logout</button>
3+
</form>

0 commit comments

Comments
 (0)