Fix login/register redirect flow

This commit is contained in:
Hakan Shehu
2024-10-27 16:19:24 +01:00
parent 2a2bbea93c
commit 5e297f327a
7 changed files with 63 additions and 9 deletions

View File

@@ -120,6 +120,8 @@ export class EmailLoginMutationHandler
return {
output: {
success: true,
account: data.account,
workspaces: data.workspaces,
},
changes: changedTables,
};

View File

@@ -121,6 +121,8 @@ export class EmailRegisterMutationHandler
return {
output: {
success: true,
account: data.account,
workspaces: data.workspaces,
},
changes: changedTables,
};

View File

@@ -1,3 +1,6 @@
import { Account } from '@/types/accounts';
import { WorkspaceOutput } from '@/types/workspaces';
export type EmailLoginMutationInput = {
type: 'email_login';
server: string;
@@ -5,8 +8,18 @@ export type EmailLoginMutationInput = {
password: string;
};
export type EmailLoginMutationOutput = {
success: boolean;
export type EmailLoginMutationOutput =
| EmailLoginMutationSuccessOutput
| EmailLoginMutationErrorOutput;
export type EmailLoginMutationSuccessOutput = {
success: true;
account: Account;
workspaces: WorkspaceOutput[];
};
export type EmailLoginMutationErrorOutput = {
success: false;
};
declare module '@/operations/mutations' {

View File

@@ -1,3 +1,6 @@
import { Account } from '@/types/accounts';
import { WorkspaceOutput } from '@/types/workspaces';
export type EmailRegisterMutationInput = {
type: 'email_register';
server: string;
@@ -6,8 +9,18 @@ export type EmailRegisterMutationInput = {
password: string;
};
export type EmailRegisterMutationOutput = {
success: boolean;
export type EmailRegisterMutationOutput =
| EmailRegisterMutationSuccessOutput
| EmailRegisterMutationErrorOutput;
export type EmailRegisterMutationSuccessOutput = {
success: true;
account: Account;
workspaces: WorkspaceOutput[];
};
export type EmailRegisterMutationErrorOutput = {
success: false;
};
declare module '@/operations/mutations' {

View File

@@ -46,8 +46,20 @@ export const EmailLogin = ({ server }: EmailLoginProps) => {
password: values.password,
server: server.domain,
},
onSuccess() {
navigate('/');
onSuccess(output) {
if (output.success) {
if (output.workspaces.length > 0) {
navigate(`/${output.workspaces[0].id}`);
} else {
navigate('/create');
}
} else {
toast({
title: 'Failed to login',
description: 'Invalid email or password.',
variant: 'destructive',
});
}
},
onError() {
toast({

View File

@@ -49,8 +49,20 @@ export const EmailRegister = ({ server }: EmailRegisterProps) => {
password: values.password,
server: server.domain,
},
onSuccess() {
navigate('/');
onSuccess(output) {
if (output.success) {
if (output.workspaces.length > 0) {
navigate(`/${output.workspaces[0].id}`);
} else {
navigate('/create');
}
} else {
toast({
title: 'Failed to register',
description: 'Email already in use.',
variant: 'destructive',
});
}
},
onError() {
toast({

View File

@@ -12,7 +12,7 @@ export const WorkspaceCreate = () => {
const { mutate, isPending } = useMutation();
const handleCancel =
account.workspaces.length > 1 ? () => navigate('/') : undefined;
account.workspaces.length > 0 ? () => navigate('/') : undefined;
return (
<div className="container flex flex-row justify-center">