Skip to content

Commit 902679b

Browse files
schwaaampclaude
andcommitted
Set timezone on initial user profile creation during email signup
Pass device timezone as user metadata in the signup request body. Updated handle_new_user() trigger reads it from raw_user_meta_data and writes timezone/tz_source/tz_updated_at on the initial INSERT. Google OAuth users get timezone set on next app launch via registerTimezone() which does .update() after the profile exists. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 1c5318d commit 902679b

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

mobile/src/utils/supabaseAuth.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,17 @@ export async function signUpWithEmail(
201201
console.log(tag, "email_redirect_to:", authRedirectUrl);
202202

203203
try {
204-
const requestBody = {
204+
// Include device timezone in user metadata so the handle_new_user()
205+
// trigger can write it to user_profiles on initial row creation.
206+
let deviceTimezone: string | undefined;
207+
try {
208+
deviceTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
209+
} catch { /* Intl unavailable — skip */ }
210+
211+
const requestBody: Record<string, unknown> = {
205212
email,
206213
password,
214+
...(deviceTimezone ? { data: { timezone: deviceTimezone } } : {}),
207215
};
208216
const signupUrl = `${supabaseUrl}/auth/v1/signup?redirect_to=${encodeURIComponent(authRedirectUrl)}`;
209217
console.log(tag, "POST signup URL:", signupUrl);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
-- Update handle_new_user() trigger to populate timezone from user metadata.
2+
-- Email signups pass { data: { timezone: "America/Los_Angeles" } } in the
3+
-- signup request. Google OAuth users get timezone set later via the mobile
4+
-- app's registerTimezone() on session restore.
5+
6+
CREATE OR REPLACE FUNCTION public.handle_new_user()
7+
RETURNS trigger
8+
LANGUAGE plpgsql SECURITY DEFINER
9+
SET search_path TO 'public'
10+
AS $$
11+
BEGIN
12+
INSERT INTO public.user_profiles (user_id, email, name, picture_url, google_sub, timezone, tz_source, tz_updated_at)
13+
VALUES (
14+
NEW.id,
15+
NEW.email,
16+
NEW.raw_user_meta_data->>'name',
17+
NEW.raw_user_meta_data->>'picture',
18+
NEW.raw_user_meta_data->>'sub',
19+
NEW.raw_user_meta_data->>'timezone',
20+
CASE WHEN NEW.raw_user_meta_data->>'timezone' IS NOT NULL THEN 'device' ELSE NULL END,
21+
CASE WHEN NEW.raw_user_meta_data->>'timezone' IS NOT NULL THEN now() ELSE NULL END
22+
);
23+
RETURN NEW;
24+
END;
25+
$$;

0 commit comments

Comments
 (0)