export const AuthLive = HttpApiBuilder.group(PublicApi, "auth", (handlers) =>
Effect.gen(function* () {
const { Login, Logout, createUser } = yield* AuthRepository;
return handlers
.handle(
"Login",
Effect.fn(function* ({ payload }) {
const token = yield* Login(payload.username, payload.password).pipe(
Effect.catchTag("SqlError", Effect.die)
);
// we need to set the cookie!!
yield* HttpApiBuilder.securitySetCookie(ApiKey, token);
})
)
.handle(
"Register",
Effect.fn(function* ({ payload }) {
const user = yield* createUser(
payload.username,
Redacted.value(payload.password)
).pipe(Effect.catchTag("SqlError", Effect.die));
const token = yield* Login(payload.username, payload.password).pipe(
Effect.catchTag("SqlError", Effect.die)
);
// we need to set the cookie!!
yield* HttpApiBuilder.securitySetCookie(ApiKey, token);
})
)
.handle("Logout", () =>
Logout.pipe(Effect.catchTag("SqlError", Effect.die))
)
.handle("User", () => CurrentUser);
})
);
export const AuthLive = HttpApiBuilder.group(PublicApi, "auth", (handlers) =>
Effect.gen(function* () {
const { Login, Logout, createUser } = yield* AuthRepository;
return handlers
.handle(
"Login",
Effect.fn(function* ({ payload }) {
const token = yield* Login(payload.username, payload.password).pipe(
Effect.catchTag("SqlError", Effect.die)
);
// we need to set the cookie!!
yield* HttpApiBuilder.securitySetCookie(ApiKey, token);
})
)
.handle(
"Register",
Effect.fn(function* ({ payload }) {
const user = yield* createUser(
payload.username,
Redacted.value(payload.password)
).pipe(Effect.catchTag("SqlError", Effect.die));
const token = yield* Login(payload.username, payload.password).pipe(
Effect.catchTag("SqlError", Effect.die)
);
// we need to set the cookie!!
yield* HttpApiBuilder.securitySetCookie(ApiKey, token);
})
)
.handle("Logout", () =>
Logout.pipe(Effect.catchTag("SqlError", Effect.die))
)
.handle("User", () => CurrentUser);
})
);