login with supabase working
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
<script>
|
||||
export let card;
|
||||
|
||||
function useCard() {
|
||||
console.log(card.title);
|
||||
}
|
||||
</script>
|
||||
|
||||
<article class="card">
|
||||
<div class="card-image-wrap">
|
||||
<img src={card.imageUrl} alt={card.title} class="card-image" loading="lazy" />
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<h3 class="card-title">{card.title}</h3>
|
||||
<p class="card-description">{card.description}</p>
|
||||
<button type="button" class="btn-use" onclick={useCard}>Use</button>
|
||||
</div>
|
||||
</article>
|
||||
|
||||
<style>
|
||||
.card {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
background: var(--card-bg, #1a1a1a);
|
||||
border-radius: 10px;
|
||||
overflow: hidden;
|
||||
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
|
||||
transition: box-shadow 0.2s, transform 0.2s;
|
||||
}
|
||||
|
||||
.card:hover {
|
||||
box-shadow: 0 6px 24px rgba(0, 0, 0, 0.4);
|
||||
}
|
||||
|
||||
.card-image-wrap {
|
||||
aspect-ratio: 400 / 240;
|
||||
overflow: hidden;
|
||||
background: var(--bg-muted, #252525);
|
||||
}
|
||||
|
||||
.card-image {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: cover;
|
||||
display: block;
|
||||
}
|
||||
|
||||
.card-body {
|
||||
padding: 1rem 1.25rem;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.5rem;
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.card-title {
|
||||
margin: 0;
|
||||
font-size: 1.1rem;
|
||||
font-weight: 600;
|
||||
color: var(--text-primary, #f0f0f0);
|
||||
}
|
||||
|
||||
.card-description {
|
||||
margin: 0;
|
||||
font-size: 0.9rem;
|
||||
line-height: 1.4;
|
||||
color: var(--text-muted, #a0a0a0);
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.btn-use {
|
||||
align-self: flex-start;
|
||||
margin-top: 0.25rem;
|
||||
padding: 0.5rem 1rem;
|
||||
font-size: 0.9rem;
|
||||
font-weight: 500;
|
||||
border: 1px solid var(--border, #333);
|
||||
border-radius: 6px;
|
||||
background: var(--accent, #3b82f6);
|
||||
color: #fff;
|
||||
cursor: pointer;
|
||||
transition: background 0.2s, filter 0.2s;
|
||||
}
|
||||
|
||||
.btn-use:hover {
|
||||
filter: brightness(1.1);
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,10 @@
|
||||
<script>
|
||||
let count = $state(0)
|
||||
const increment = () => {
|
||||
count += 1
|
||||
}
|
||||
</script>
|
||||
|
||||
<button onclick={increment}>
|
||||
count is {count}
|
||||
</button>
|
||||
@@ -0,0 +1,37 @@
|
||||
<footer class="footer">
|
||||
<div class="footer-inner">
|
||||
<span class="footer-brand">Omotomo</span>
|
||||
<p class="footer-copy">© {new Date().getFullYear()} Omotomo. All rights reserved.</p>
|
||||
</div>
|
||||
</footer>
|
||||
|
||||
<style>
|
||||
.footer {
|
||||
margin-top: auto;
|
||||
width: 100%;
|
||||
padding: 1.25rem 1.5rem;
|
||||
background: var(--nav-bg, #161616);
|
||||
border-top: 1px solid var(--border, #333);
|
||||
box-shadow: 0 -2px 12px rgba(0, 0, 0, 0.2);
|
||||
}
|
||||
|
||||
.footer-inner {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 0.5rem 1rem;
|
||||
max-width: 100%;
|
||||
}
|
||||
|
||||
.footer-brand {
|
||||
font-weight: 600;
|
||||
color: var(--text-primary, #f0f0f0);
|
||||
}
|
||||
|
||||
.footer-copy {
|
||||
margin: 0;
|
||||
font-size: 0.875rem;
|
||||
color: var(--text-muted, #a0a0a0);
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,341 @@
|
||||
<script>
|
||||
import { auth } from './stores/auth.js';
|
||||
|
||||
let showPopup = false;
|
||||
let mode = 'login'; // 'login' | 'register'
|
||||
let email = '';
|
||||
let password = '';
|
||||
let submitting = false;
|
||||
let registerSuccess = false;
|
||||
|
||||
function openPopup() {
|
||||
auth.clearError();
|
||||
showPopup = true;
|
||||
mode = 'login';
|
||||
email = '';
|
||||
password = '';
|
||||
registerSuccess = false;
|
||||
}
|
||||
|
||||
function closePopup() {
|
||||
showPopup = false;
|
||||
email = '';
|
||||
password = '';
|
||||
registerSuccess = false;
|
||||
}
|
||||
|
||||
function handleBackdropClick(e) {
|
||||
if (e.target === e.currentTarget) closePopup();
|
||||
}
|
||||
|
||||
async function handleSubmit() {
|
||||
if (!email.trim() || !password) return;
|
||||
submitting = true;
|
||||
registerSuccess = false;
|
||||
if (mode === 'login') {
|
||||
const ok = await auth.login(email.trim(), password);
|
||||
if (ok) closePopup();
|
||||
} else {
|
||||
const result = await auth.register(email.trim(), password);
|
||||
if (result?.success) {
|
||||
if (result.needsConfirmation) {
|
||||
registerSuccess = true;
|
||||
} else {
|
||||
closePopup();
|
||||
}
|
||||
}
|
||||
}
|
||||
submitting = false;
|
||||
}
|
||||
|
||||
function handleLogout() {
|
||||
auth.logout();
|
||||
}
|
||||
|
||||
$: displayName = $auth.user?.user_metadata?.name ?? $auth.user?.email ?? null;
|
||||
</script>
|
||||
|
||||
<nav class="navbar">
|
||||
<span class="app-name">Omotomo</span>
|
||||
<div class="nav-actions">
|
||||
{#if $auth.loading}
|
||||
<span class="username">…</span>
|
||||
{:else if $auth.user}
|
||||
<span class="username">{displayName}</span>
|
||||
<button type="button" class="btn btn-logout" onclick={handleLogout}>Logout</button>
|
||||
{:else}
|
||||
<button type="button" class="btn btn-login" onclick={openPopup}>Login</button>
|
||||
{/if}
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
{#if showPopup}
|
||||
<div class="popup-backdrop" onclick={handleBackdropClick} role="presentation">
|
||||
<div class="popup" role="dialog" aria-modal="true" aria-labelledby="auth-popup-title">
|
||||
<div class="popup-header">
|
||||
<h2 id="auth-popup-title" class="popup-title">Sign in</h2>
|
||||
<button type="button" class="popup-close" onclick={closePopup} aria-label="Close">×</button>
|
||||
</div>
|
||||
|
||||
<div class="popup-tabs">
|
||||
<button
|
||||
type="button"
|
||||
class="tab"
|
||||
class:active={mode === 'login'}
|
||||
onclick={() => { mode = 'login'; auth.clearError(); registerSuccess = false; }}
|
||||
>
|
||||
Log in
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="tab"
|
||||
class:active={mode === 'register'}
|
||||
onclick={() => { mode = 'register'; auth.clearError(); registerSuccess = false; }}
|
||||
>
|
||||
Register
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{#if registerSuccess && !$auth.user}
|
||||
<p class="auth-message auth-success">
|
||||
Check your email to confirm your account.
|
||||
</p>
|
||||
{:else}
|
||||
<form class="popup-form" onsubmit={(e) => { e.preventDefault(); handleSubmit(); }}>
|
||||
<input
|
||||
type="email"
|
||||
bind:value={email}
|
||||
placeholder="Email"
|
||||
class="input"
|
||||
disabled={submitting}
|
||||
autocomplete={mode === 'login' ? 'email' : 'email'}
|
||||
/>
|
||||
<input
|
||||
type="password"
|
||||
bind:value={password}
|
||||
placeholder="Password"
|
||||
class="input"
|
||||
disabled={submitting}
|
||||
autocomplete={mode === 'login' ? 'current-password' : 'new-password'}
|
||||
/>
|
||||
{#if $auth.error}
|
||||
<p class="auth-error">{$auth.error}</p>
|
||||
{/if}
|
||||
<button
|
||||
type="submit"
|
||||
class="btn btn-primary btn-block"
|
||||
disabled={submitting || !email.trim() || !password}
|
||||
>
|
||||
{#if submitting}
|
||||
{mode === 'login' ? 'Logging in…' : 'Creating account…'}
|
||||
{:else}
|
||||
{mode === 'login' ? 'Log in' : 'Create account'}
|
||||
{/if}
|
||||
</button>
|
||||
</form>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<style>
|
||||
.navbar {
|
||||
position: sticky;
|
||||
top: 0;
|
||||
z-index: 100;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
width: 100%;
|
||||
padding: 0.75rem 1.5rem;
|
||||
background: var(--nav-bg, #161616);
|
||||
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.4);
|
||||
}
|
||||
|
||||
.app-name {
|
||||
font-size: 1.25rem;
|
||||
font-weight: 600;
|
||||
color: var(--text-primary, #f0f0f0);
|
||||
}
|
||||
|
||||
.nav-actions {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.75rem;
|
||||
}
|
||||
|
||||
.username {
|
||||
font-size: 0.9rem;
|
||||
color: var(--text-muted, #a0a0a0);
|
||||
}
|
||||
|
||||
.btn {
|
||||
padding: 0.5rem 1rem;
|
||||
font-size: 0.9rem;
|
||||
font-weight: 500;
|
||||
border: 1px solid var(--border, #333);
|
||||
border-radius: 6px;
|
||||
background: var(--card-bg, #1e1e1e);
|
||||
color: var(--text-primary, #f0f0f0);
|
||||
cursor: pointer;
|
||||
transition: background 0.2s, border-color 0.2s;
|
||||
}
|
||||
|
||||
.btn:hover:not(:disabled) {
|
||||
background: var(--hover-bg, #2a2a2a);
|
||||
border-color: var(--border-hover, #444);
|
||||
}
|
||||
|
||||
.btn:disabled {
|
||||
opacity: 0.6;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.btn-login,
|
||||
.btn-primary {
|
||||
background: var(--accent, #3b82f6);
|
||||
border-color: var(--accent, #3b82f6);
|
||||
}
|
||||
|
||||
.btn-login:hover:not(:disabled),
|
||||
.btn-primary:hover:not(:disabled) {
|
||||
background: #2563eb;
|
||||
border-color: #2563eb;
|
||||
}
|
||||
|
||||
.btn-logout {
|
||||
background: transparent;
|
||||
}
|
||||
|
||||
.btn-block {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
/* Popup */
|
||||
.popup-backdrop {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
z-index: 1000;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 1rem;
|
||||
background: rgba(0, 0, 0, 0.6);
|
||||
backdrop-filter: blur(4px);
|
||||
}
|
||||
|
||||
.popup {
|
||||
width: 100%;
|
||||
max-width: 380px;
|
||||
padding: 1.5rem;
|
||||
background: var(--card-bg, #1a1a1a);
|
||||
border: 1px solid var(--border, #333);
|
||||
border-radius: 12px;
|
||||
box-shadow: 0 16px 48px rgba(0, 0, 0, 0.5);
|
||||
}
|
||||
|
||||
.popup-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
margin-bottom: 1.25rem;
|
||||
}
|
||||
|
||||
.popup-title {
|
||||
margin: 0;
|
||||
font-size: 1.25rem;
|
||||
font-weight: 600;
|
||||
color: var(--text-primary, #f0f0f0);
|
||||
}
|
||||
|
||||
.popup-close {
|
||||
width: 2rem;
|
||||
height: 2rem;
|
||||
padding: 0;
|
||||
font-size: 1.5rem;
|
||||
line-height: 1;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
background: transparent;
|
||||
color: var(--text-muted, #a0a0a0);
|
||||
cursor: pointer;
|
||||
transition: color 0.2s, background 0.2s;
|
||||
}
|
||||
|
||||
.popup-close:hover {
|
||||
color: var(--text-primary, #f0f0f0);
|
||||
background: var(--hover-bg, #222);
|
||||
}
|
||||
|
||||
.popup-tabs {
|
||||
display: flex;
|
||||
gap: 0.25rem;
|
||||
margin-bottom: 1.25rem;
|
||||
padding: 0.25rem;
|
||||
background: var(--bg-muted, #252525);
|
||||
border-radius: 8px;
|
||||
}
|
||||
|
||||
.tab {
|
||||
flex: 1;
|
||||
padding: 0.5rem 1rem;
|
||||
font-size: 0.9rem;
|
||||
font-weight: 500;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
background: transparent;
|
||||
color: var(--text-muted, #a0a0a0);
|
||||
cursor: pointer;
|
||||
transition: color 0.2s, background 0.2s;
|
||||
}
|
||||
|
||||
.tab:hover {
|
||||
color: var(--text-primary, #f0f0f0);
|
||||
}
|
||||
|
||||
.tab.active {
|
||||
background: var(--card-bg, #1a1a1a);
|
||||
color: var(--text-primary, #f0f0f0);
|
||||
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
|
||||
}
|
||||
|
||||
.popup-form {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.75rem;
|
||||
}
|
||||
|
||||
.input {
|
||||
width: 100%;
|
||||
padding: 0.6rem 0.75rem;
|
||||
font-size: 0.95rem;
|
||||
border: 1px solid var(--border, #333);
|
||||
border-radius: 6px;
|
||||
background: var(--bg, #0f0f0f);
|
||||
color: var(--text-primary, #f0f0f0);
|
||||
}
|
||||
|
||||
.input::placeholder {
|
||||
color: var(--text-muted, #a0a0a0);
|
||||
}
|
||||
|
||||
.input:focus {
|
||||
outline: none;
|
||||
border-color: var(--accent, #3b82f6);
|
||||
}
|
||||
|
||||
.auth-error {
|
||||
margin: 0;
|
||||
font-size: 0.85rem;
|
||||
color: #ef4444;
|
||||
}
|
||||
|
||||
.auth-message {
|
||||
margin: 0;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.auth-success {
|
||||
color: #22c55e;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,50 @@
|
||||
export const cards = [
|
||||
{
|
||||
id: 1,
|
||||
title: 'Alpha',
|
||||
description: 'First card with a short description for the grid.',
|
||||
imageUrl: 'https://picsum.photos/seed/alpha/400/240',
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
title: 'Beta',
|
||||
description: 'Second card. Another brief description here.',
|
||||
imageUrl: 'https://picsum.photos/seed/beta/400/240',
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
title: 'Gamma',
|
||||
description: 'Third card. Keep descriptions short and clear.',
|
||||
imageUrl: 'https://picsum.photos/seed/gamma/400/240',
|
||||
},
|
||||
{
|
||||
id: 4,
|
||||
title: 'Delta',
|
||||
description: 'Fourth card in the responsive grid layout.',
|
||||
imageUrl: 'https://picsum.photos/seed/delta/400/240',
|
||||
},
|
||||
{
|
||||
id: 5,
|
||||
title: 'Epsilon',
|
||||
description: 'Fifth card. Placeholder image from Picsum.',
|
||||
imageUrl: 'https://picsum.photos/seed/epsilon/400/240',
|
||||
},
|
||||
{
|
||||
id: 6,
|
||||
title: 'Zeta',
|
||||
description: 'Sixth card with a subtle shadow and dark theme.',
|
||||
imageUrl: 'https://picsum.photos/seed/zeta/400/240',
|
||||
},
|
||||
{
|
||||
id: 7,
|
||||
title: 'Eta',
|
||||
description: 'Seventh card. Full-width grid, 4+ columns on desktop.',
|
||||
imageUrl: 'https://picsum.photos/seed/eta/400/240',
|
||||
},
|
||||
{
|
||||
id: 8,
|
||||
title: 'Theta',
|
||||
description: 'Eighth card. Click Use to log the card name.',
|
||||
imageUrl: 'https://picsum.photos/seed/theta/400/240',
|
||||
},
|
||||
];
|
||||
@@ -0,0 +1,72 @@
|
||||
import { writable } from 'svelte/store';
|
||||
import { supabase } from '../supabase.js';
|
||||
|
||||
function createAuthStore() {
|
||||
const { subscribe, set, update } = writable({
|
||||
user: null,
|
||||
loading: true,
|
||||
error: null,
|
||||
});
|
||||
|
||||
function setSession(session) {
|
||||
const user = session?.user ?? null;
|
||||
set({
|
||||
user,
|
||||
loading: false,
|
||||
error: null,
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
subscribe,
|
||||
init: async () => {
|
||||
try {
|
||||
const { data: { session } } = await supabase.auth.getSession();
|
||||
setSession(session);
|
||||
} catch (e) {
|
||||
set({ user: null, loading: false, error: e?.message ?? 'Auth error' });
|
||||
}
|
||||
supabase.auth.onAuthStateChange((_event, session) => {
|
||||
setSession(session);
|
||||
});
|
||||
},
|
||||
login: async (email, password) => {
|
||||
update((s) => ({ ...s, error: null }));
|
||||
try {
|
||||
const { error } = await supabase.auth.signInWithPassword({ email, password });
|
||||
if (error) {
|
||||
update((s) => ({ ...s, error: error.message }));
|
||||
return false;
|
||||
}
|
||||
update((s) => ({ ...s, error: null }));
|
||||
return true;
|
||||
} catch (e) {
|
||||
update((s) => ({ ...s, error: e?.message ?? 'Login failed' }));
|
||||
return false;
|
||||
}
|
||||
},
|
||||
register: async (email, password) => {
|
||||
update((s) => ({ ...s, error: null }));
|
||||
try {
|
||||
const { data, error } = await supabase.auth.signUp({ email, password });
|
||||
if (error) {
|
||||
update((s) => ({ ...s, error: error.message }));
|
||||
return { success: false };
|
||||
}
|
||||
update((s) => ({ ...s, error: null }));
|
||||
const needsConfirmation = data?.user && !data?.session;
|
||||
return { success: true, needsConfirmation, data };
|
||||
} catch (e) {
|
||||
update((s) => ({ ...s, error: e?.message ?? 'Registration failed' }));
|
||||
return { success: false };
|
||||
}
|
||||
},
|
||||
logout: async () => {
|
||||
await supabase.auth.signOut();
|
||||
set({ user: null, loading: false, error: null });
|
||||
},
|
||||
clearError: () => update((s) => ({ ...s, error: null })),
|
||||
};
|
||||
}
|
||||
|
||||
export const auth = createAuthStore();
|
||||
@@ -0,0 +1,6 @@
|
||||
import { createClient } from '@supabase/supabase-js';
|
||||
|
||||
const url = import.meta.env.VITE_SUPABASE_URL;
|
||||
const anonKey = import.meta.env.VITE_SUPABASE_ANON_KEY;
|
||||
|
||||
export const supabase = createClient(url ?? '', anonKey ?? '');
|
||||
Reference in New Issue
Block a user