feat(auth): replace email/password registration with OAuth2 (Google + GitHub)
Some checks failed
CI / Test backend (pull_request) Successful in 19s
Release / Test backend (push) Successful in 18s
CI / Check ui (pull_request) Successful in 41s
Release / Check ui (push) Successful in 21s
CI / Docker / backend (pull_request) Successful in 1m43s
CI / Docker / runner (pull_request) Successful in 1m28s
Release / Docker / backend (push) Successful in 1m40s
CI / Docker / caddy (pull_request) Successful in 6m45s
Release / Docker / runner (push) Successful in 1m48s
Release / Docker / caddy (push) Successful in 7m12s
CI / Docker / ui (pull_request) Successful in 1m20s
Release / Docker / ui (push) Successful in 1m19s
Release / Gitea Release (push) Failing after 2s

- New /auth/[provider] route: generates state cookie, redirects to provider
- New /auth/[provider]/callback: exchanges code, fetches profile, auto-creates
  or links account, sets auth cookie
- pocketbase.ts: add oauth_provider/oauth_id to User; new getUserByOAuth(),
  createOAuthUser(), linkOAuthToUser() helpers; loginUser() drops email_verified gate
- pb-init-v3.sh: add oauth_provider + oauth_id fields (schema + migration)
- docker-compose.yml: GOOGLE/GITHUB client ID/secret env vars (replaces SMTP vars)
- Login page: two OAuth buttons (Google, GitHub) — register form removed
- /verify-email route and email.ts removed (provider handles email verification)
- /api/auth/register returns 410 (OAuth-only from now on)
This commit is contained in:
Admin
2026-03-24 22:01:51 +05:00
parent 920ac0d41b
commit 4a7009989c
12 changed files with 495 additions and 650 deletions

View File

@@ -1,66 +1,12 @@
import { json, error } from '@sveltejs/kit';
import { error } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { createUser } from '$lib/server/pocketbase';
import { sendVerificationEmail } from '$lib/server/email';
import { log } from '$lib/server/logger';
/**
* POST /api/auth/register
* Body: { username: string, email: string, password: string }
* Returns: { pending_verification: true, email: string }
*
* Account is created but NOT activated until the user clicks the verification
* link sent to their email. The iOS app should show a "check your inbox" screen.
* Username/password registration has been replaced by OAuth2 (Google & GitHub).
* This endpoint is no longer supported.
*/
export const POST: RequestHandler = async ({ request }) => {
let body: { username?: string; email?: string; password?: string };
try {
body = await request.json();
} catch {
error(400, 'Invalid JSON body');
}
const username = (body.username ?? '').trim();
const email = (body.email ?? '').trim().toLowerCase();
const password = body.password ?? '';
if (!username || !email || !password) {
error(400, 'Username, email and password are required');
}
if (username.length < 3 || username.length > 32) {
error(400, 'Username must be between 3 and 32 characters');
}
if (!/^[a-zA-Z0-9_-]+$/.test(username)) {
error(400, 'Username may only contain letters, numbers, underscores and hyphens');
}
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
error(400, 'Please enter a valid email address');
}
if (password.length < 8) {
error(400, 'Password must be at least 8 characters');
}
let user;
try {
user = await createUser(username, password, email);
} catch (e: unknown) {
const msg = e instanceof Error ? e.message : 'Registration failed.';
if (msg.includes('Username already taken')) {
error(409, 'That username is already taken');
}
if (msg.includes('Email already in use')) {
error(409, 'That email address is already registered');
}
log.error('api/auth/register', 'unexpected error', { username, err: String(e) });
error(500, 'An error occurred. Please try again.');
}
// Send verification email (non-fatal)
try {
await sendVerificationEmail(email, user.verification_token!);
} catch (e) {
log.error('api/auth/register', 'failed to send verification email', { username, email, err: String(e) });
}
return json({ pending_verification: true, email });
export const POST: RequestHandler = async () => {
error(410, 'Username/password registration is no longer supported. Please sign in with Google or GitHub.');
};