Skip to content

Commit 352fe13

Browse files
authored
feat(api): automatically log in after sign up (#219)
1 parent f4558db commit 352fe13

3 files changed

Lines changed: 71 additions & 6 deletions

File tree

internal/api/src/routes/auth/auth.server.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,43 @@ test("POST /signup validates password length", async () => {
834834
expect(res.status).toBe(400);
835835
});
836836

837+
test("POST /signup without email verification creates session and redirects to chat", async () => {
838+
const { url, bindings } = await serve({
839+
bindings: {
840+
sendEmail: undefined, // Disable email verification
841+
},
842+
});
843+
const email = "noeverify@example.com";
844+
const password = "securepassword123";
845+
846+
const res = await fetch(`${url}/api/auth/signup`, {
847+
method: "POST",
848+
headers: {
849+
"Content-Type": "application/json",
850+
},
851+
body: JSON.stringify({ email, password }),
852+
});
853+
854+
expect(res.status).toBe(200);
855+
const data = await res.json();
856+
expect(data.ok).toBe(true);
857+
expect(data.redirect_url).toBe("/chat");
858+
859+
// Verify user was created
860+
const db = await bindings.database();
861+
const user = await db.selectUserByEmail(email);
862+
expect(user).toBeDefined();
863+
expect(user?.email).toBe(email);
864+
865+
// Verify session cookie was set (auto-login)
866+
const cookies = res.headers.getSetCookie?.() || [
867+
res.headers.get("Set-Cookie") || "",
868+
];
869+
const cookieString = cookies.join("; ");
870+
expect(cookieString).toContain("blink_session_token=");
871+
expect(cookieString).toContain("last_login_provider=credentials");
872+
});
873+
837874
test("POST /resend-email-verification regenerates token", async () => {
838875
const { url, helpers, bindings } = await serve();
839876
const { user } = await helpers.createUser({

internal/api/src/routes/auth/auth.server.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,34 @@ export default function mountAuth(server: APIServer) {
10451045

10461046
return c.json({ ok: true, redirect_url: redirectUrl });
10471047
} else {
1048-
// No email verification needed - redirect directly to chat
1048+
// No email verification needed - create session and redirect to chat
1049+
const sessionToken = await encode({
1050+
secret: c.env.AUTH_SECRET,
1051+
token: {
1052+
sub: user.id,
1053+
id: user.id,
1054+
email: user.email,
1055+
name: user.display_name,
1056+
},
1057+
salt: SESSION_COOKIE_NAME,
1058+
});
1059+
1060+
setCookie(c, SESSION_COOKIE_NAME, sessionToken, {
1061+
path: "/",
1062+
httpOnly: true,
1063+
sameSite: "Lax",
1064+
secure: SESSION_SECURE,
1065+
maxAge: 30 * 24 * 60 * 60, // 30 days
1066+
});
1067+
1068+
setCookie(c, "last_login_provider", "credentials", {
1069+
path: "/",
1070+
httpOnly: true,
1071+
sameSite: "Lax",
1072+
secure: SESSION_SECURE,
1073+
maxAge: 60 * 60 * 24 * 180, // 180 days
1074+
});
1075+
10491076
return c.json({ ok: true, redirect_url: "/chat" });
10501077
}
10511078
}

internal/api/src/test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,12 @@ export const serve = async (options?: ServeOptions) => {
271271
...options?.bindings?.runtime,
272272
},
273273
sendEmail:
274-
options?.bindings?.sendEmail ??
275-
(async (email) => {
276-
// Mock email service for tests - just log
277-
console.log("Mock email sent:", email.type, email.email);
278-
}),
274+
options?.bindings && "sendEmail" in options.bindings
275+
? options.bindings.sendEmail
276+
: async (email) => {
277+
// Mock email service for tests - just log
278+
console.log("Mock email sent:", email.type, email.email);
279+
},
279280
sendTelemetryEvent:
280281
options?.bindings?.sendTelemetryEvent ??
281282
(async (event) => {

0 commit comments

Comments
 (0)