Skip to content

Commit

Permalink
feat: add error handling for login flow WD-10252
Browse files Browse the repository at this point in the history
Signed-off-by: David Edler <david.edler@canonical.com>
  • Loading branch information
edlerd authored and natalian98 committed Apr 23, 2024
1 parent 1e98e09 commit f96567e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 deletions.
34 changes: 21 additions & 13 deletions ui/components/Flow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface Props<T> {
interface State<T> {
values: T;
isLoading: boolean;
error?: string;
}

export class Flow<T extends Values> extends Component<Props<T>, State<T>> {
Expand Down Expand Up @@ -104,10 +105,8 @@ export class Flow<T extends Values> extends Component<Props<T>, State<T>> {
if (this.state.isLoading) {
return Promise.resolve();
}
// console.log("State", this.state);

this.setState((state) => {
// console.log("Set state", state);
return {
...state,
isLoading: true,
Expand All @@ -118,19 +117,28 @@ export class Flow<T extends Values> extends Component<Props<T>, State<T>> {
? { ...this.state.values, method }
: this.state.values;

return this.props.onSubmit(values).finally(() => {
// We wait for reconciliation and update the state after 50ms
// Done submitting - update loading status
this.setState((state) => ({
...state,
isLoading: false,
}));
});
return this.props
.onSubmit(values)
.catch((e) => {
const backendError = e as { response?: { data?: string } };
this.setState((state) => ({
...state,
error: backendError?.response?.data?.toString(),
}));
})
.finally(() => {
// We wait for reconciliation and update the state after 50ms
// Done submitting - update loading status
this.setState((state) => ({
...state,
isLoading: false,
}));
});
};

render() {
const { flow } = this.props;
const { values, isLoading } = this.state;
const { values, isLoading, error } = this.state;

// Filter the nodes - only show the ones we want
const nodes = this.filterNodes();
Expand All @@ -148,20 +156,20 @@ export class Flow<T extends Values> extends Component<Props<T>, State<T>> {
action={flow.ui.action}
method={flow.ui.method}
onSubmit={void this.handleSubmit}
className={error && "is-error"}
>
{nodes.map((node, k) => {
const id = getNodeId(node) as keyof Values;
return (
<Node
key={`${id}-${k}`}
disabled={isLoading}
error={error}
node={node}
value={values[id]}
dispatchSubmit={this.handleSubmit}
setValue={(value) => {
console.log("Value", value);
return new Promise((resolve) => {
console.log("NODE ID", getNodeId(node));
this.setState(
(state) => ({
...state,
Expand Down
2 changes: 2 additions & 0 deletions ui/components/Node.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const Node: FC<Omit<NodeInputProps, "attributes">> = ({
setValue,
disabled,
dispatchSubmit,
error,
}) => {
if (isUiNodeImageAttributes(node.attributes)) {
return <NodeImage node={node} attributes={node.attributes} />;
Expand All @@ -46,6 +47,7 @@ export const Node: FC<Omit<NodeInputProps, "attributes">> = ({
node={node}
disabled={disabled}
attributes={node.attributes}
error={error}
/>
);
}
Expand Down
2 changes: 2 additions & 0 deletions ui/components/NodeInputPassword.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ export const NodeInputPassword: FC<NodeInputProps> = ({
setValue,
disabled,
dispatchSubmit,
error,
}) => {
return (
<Input
type="password"
label={getNodeLabel(node)}
disabled={disabled}
defaultValue={node.messages.map(({ text }) => text).join(" ")}
error={error}
onChange={(e) => void setValue(e.target.value)}
onKeyDown={(e) => {
if (e.key === "Enter") {
Expand Down
1 change: 1 addition & 0 deletions ui/components/helpers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export interface NodeInputProps {
disabled: boolean;
dispatchSubmit: FormDispatcher;
setValue: ValueSetter;
error?: string;
}
1 change: 0 additions & 1 deletion ui/pages/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ const Login: NextPage = () => {
]);
const handleSubmit = useCallback(
(values: UpdateLoginFlowBody) => {
console.log("Values", values);
return kratos
.updateLoginFlow({
flow: String(flow?.id),
Expand Down

0 comments on commit f96567e

Please sign in to comment.