Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create profile on signup and signin #48

Merged
merged 1 commit into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/routes/api/auth/signin/+server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { adminAuth } from '$lib/server/firebase';
import { adminAuth, adminDb } from '$lib/server/firebase';
import { json } from '@sveltejs/kit';
import { FieldValue } from 'firebase-admin/firestore';
import type { RequestHandler } from './$types';

export const POST: RequestHandler = async ({ request, cookies }) => {
Expand All @@ -19,9 +20,29 @@ export const POST: RequestHandler = async ({ request, cookies }) => {
maxAge: expiresIn / 1000 // Convert from milliseconds to seconds
});

// Check and create profile if needed
const decodedToken = await adminAuth.verifyIdToken(idToken);
const uid = decodedToken.uid;

const profileRef = adminDb.collection('profiles').doc(uid);
const profile = await profileRef.get();

if (!profile.exists) {
const user = await adminAuth.getUser(uid);
const defaultProfile = {
uid,
displayName: user.displayName || 'User',
email: user.email || '',
createdAt: FieldValue.serverTimestamp(),
updatedAt: FieldValue.serverTimestamp()
};

await profileRef.set(defaultProfile);
}

return json({ status: 'success' });
} catch (error) {
console.error('Error creating session:', error);
console.error('Error during sign in:', error);
return json({ status: 'error', message: 'Unauthorized request' }, { status: 401 });
}
};
25 changes: 17 additions & 8 deletions src/routes/profile/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ProfileSchema } from '$lib/schema/profile';
import { adminDb } from '$lib/server/firebase';
import { fail, redirect } from '@sveltejs/kit';
import { FieldValue } from 'firebase-admin/firestore';
import type { Actions, PageServerLoad } from './$types';

export const load: PageServerLoad = async ({ locals }) => {
Expand All @@ -20,10 +22,20 @@ export const actions = {

const data = await request.formData();
const displayName = data.get('displayName')?.toString();
const title = data.get('title')?.toString();
const bio = data.get('bio')?.toString();
const title = data.get('title')?.toString() || null;
const bio = data.get('bio')?.toString() || null;

if (!displayName) {
const profile = ProfileSchema.omit({
updatedAt: true,
createdAt: true
}).safeParse({
uid: locals.user.uid,
displayName,
title,
bio
});

if (!profile.success) {
return fail(400, { missing: true });
}

Expand All @@ -34,11 +46,8 @@ export const actions = {
.doc(locals.user.uid)
.set(
{
uid: locals.user.uid,
displayName,
title: title || null,
bio: bio || null,
updatedAt: new Date()
...profile.data,
updatedAt: FieldValue.serverTimestamp()
},
{ merge: true }
);
Expand Down
Loading