sst: Auth component does not work in dev mode (Live)
Auth component not working with sst dev
Issue
When running an sst.aws.Auth component in dev mode, the API returns “sst dev is not running”. The auth handler works fine when dev: false is set, but fails without it.
Reproduction
I have a basic email/password auth setup using the following handler:
import { AuthHandler } from "sst/auth";
import { createSessionBuilder } from "sst/auth";
import { Hono } from "hono";
import crypto from "crypto";
import type { Adapter } from "sst/auth/adapter";
import { handle } from "hono/aws-lambda";
const users = new Map<
string,
{ id: string; email: string; password: string }
>();
function hashPassword(password: string): string {
return crypto.createHash("sha256").update(password).digest("hex");
}
const randomFourDigits = (): string =>
Math.floor(1000 + Math.random() * 9000).toString();
const EmailPasswordAdapter = (): Adapter<{ email: string }> => {
return (router: Hono) => {
router.post("/auth", async (c) => {
const body = await c.req.json();
const { action, email, password } = body;
if (action === "signup") {
if (users.has(email)) {
return c.json({ error: "User already exists" }, 400);
}
const hashedPassword = hashPassword(password);
const id = randomFourDigits();
users.set(email, { id, email, password: hashedPassword });
return c.json({ claims: { id, email } });
}
if (action === "signin") {
const user = users.get(email);
if (!user) {
return c.json({ error: "User not found" }, 404);
}
const hashedPassword = hashPassword(password);
if (hashedPassword !== user.password) {
return c.json({ error: "Invalid password" }, 401);
}
return c.json({ claims: { id: user.id, email } });
}
return c.json({ error: "Invalid action" }, 400);
});
};
};
export const session = createSessionBuilder<{
user: {
email: string;
};
}>();
const auth = AuthHandler({
providers: {
emailPassword: EmailPasswordAdapter(),
},
callbacks: {
auth: {
async allowClient() {
return true;
},
async success(ctx, input) {
return ctx.session({
type: "user",
properties: {
email: input.email,
},
});
},
},
},
});
export const handler = handle(auth);
Not Working Config
export default $config({
app(input) {
return {
name: "sst-auth",
removal: input?.stage === "production" ? "retain" : "remove",
home: "aws",
};
},
async run() {
const auth = new sst.aws.Auth("Auth", {
authenticator: {
handler: "./src/auth.handler",
// dev: false,
url: true,
},
});
return {
auth: auth.authenticator.url,
};
},
});
Working Config
export default $config({
app(input) {
return {
name: "sst-auth",
removal: input?.stage === "production" ? "retain" : "remove",
home: "aws",
};
},
async run() {
const auth = new sst.aws.Auth("Auth", {
authenticator: {
handler: "./src/auth.handler",
dev: false, // This fixes it
url: true,
},
});
return {
auth: auth.authenticator.url,
};
},
});
Expected Behavior
The auth component should work in dev mode without requiring dev: false.
Actual Behavior
API returns “sst dev is not running” when dev: false is not set.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 15 (5 by maintainers)
Finally got it working on the following versions: sst 3.3.21 node v22.12.0
My issue was I was running an older version of node 16.20.0. Thanks @solomonduncan for the tip!
Just to save anyone else the embarrassment of the day I spent dealing with trying to figure out this issue:
Make sure your shell is using the correct Node version (
node -v).Problem is actually version 3.3.x, roll back to 3.2.76 will work around it. 3.3.x introduced a new appsync bridge and that seems likely where the problem lies.