CWA 2.0 is in alpha: try it, build with it, give feedback. APIs can still change between releases; see Releases.
Auth
useLogin
State and a sign-in function for a custom login form, with a safe redirect after sign-in.
useLogin holds a login form's state and signs the user in. On success it navigates to the redirect target. The layer's /login page uses it, so a login page of your own keeps the same behaviour.
const { credentials, signIn, submitting, error } = useLogin()
Signature
useLogin(ops?: { redirect?: MaybeRefOrGetter<string | undefined> })
Parameters
| Parameter | Type | Description |
|---|---|---|
ops.redirect | MaybeRefOrGetter<string | undefined> | Where to go after signing in. A string, ref or getter, read at the moment of sign-in. When undefined, the route's redirect query parameter is used, then / |
Return Values
| Return | Type | Purpose |
|---|---|---|
credentials | { username: string, password: string } | A reactive() object. Bind the inputs to credentials.username and credentials.password |
signIn | () => Promise<void> | Calls cwa.auth.signIn() and, on success, navigates to the redirect target |
submitting | Ref<boolean> | true while the request runs |
error | Ref<string | undefined> | The API's message on failure, its status text, or Unknown/Network Error. Cleared when signIn() starts |
Redirect Target
Only a same-site path that starts with a single / is followed. An external URL, a protocol-relative //host, /\host, a relative path, or a repeated redirect query parameter all send the user to / instead, so the login page can't be used as an open redirect.
Example
Authentication → Customising the Login Page has a full login page, and Where the user goes after signing in shows the redirect option.