Skip to content

Commit

Permalink
feat: create profile on signup and signin (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobLinCool authored Dec 22, 2024
1 parent cb3ec8a commit 329cb67
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
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

0 comments on commit 329cb67

Please sign in to comment.