Skip to content

Commit 6240657

Browse files
redirecting user
Signed-off-by: jackdisalvatore <[email protected]>
1 parent 3a7cfaf commit 6240657

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

frontend/src/lib/services/TenantsResolver/shared.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export const TenantsSchema = z.object({
55
name: z.string()
66
});
77

8-
export type LibrePermissions = z.infer<typeof TenantsSchema>;
8+
export type Tenants = z.infer<typeof TenantsSchema>;
99

1010
export const TenantsSuccessResponseSchema = z.array(TenantsSchema);
1111

frontend/src/lib/services/UnityAuth/UnityAuth.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,27 @@ export class UnityAuthServiceImpl
5050
}
5151

5252
async login(email: string, password: string): Promise<CompleteLoginResponse> {
53+
let completeLoginRes: CompleteLoginResponse;
54+
5355
const res = await this.axiosInstance.post('/api/login', {
5456
username: email,
5557
password: password
5658
});
5759

5860
const loginRes = UnityAuthLoginResponseSchema.parse(res.data);
5961

60-
const tenantsRes = await this.tenantsResolver.getTenants(loginRes);
62+
completeLoginRes = { ...loginRes };
6163

62-
if (!isTenantsSuccessResponse(tenantsRes)) {
63-
throw new Error(tenantsRes.errorMessage);
64-
}
65-
console.log(tenantsRes);
64+
try {
65+
const tenantsRes = await this.tenantsResolver.getTenants(loginRes);
66+
if (!isTenantsSuccessResponse(tenantsRes)) {
67+
throw new Error(tenantsRes.errorMessage);
68+
}
6669

67-
const completeLoginRes: CompleteLoginResponse = { ...loginRes };
70+
completeLoginRes.tenants = tenantsRes;
71+
} catch (e) {
72+
console.log(e); // ignore users that do not belong to a tenant
73+
}
6874

6975
this.loginData = completeLoginRes;
7076

frontend/src/lib/services/UnityAuth/shared.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { z } from 'zod';
22
import type { TenantsResolver } from '../TenantsResolver/TenantsResolver';
3+
import { TenantsSchema } from '../TenantsResolver/shared';
34

45
export const UnityAuthServicePropsSchema = z.object({
56
baseURL: z.string()
@@ -13,7 +14,8 @@ export const UnityAuthLoginResponseSchema = z.object({
1314
access_token: z.string(),
1415
token_type: z.string(),
1516
expires_in: z.number(),
16-
username: z.string()
17+
username: z.string(),
18+
tenants: z.array(TenantsSchema).optional()
1719
});
1820

1921
export type UnityAuthLoginResponse = z.infer<typeof UnityAuthLoginResponseSchema>;

frontend/src/routes/login/+page.svelte

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<script lang="ts">
22
import { createInput, emailValidator } from '$lib/utils/validation';
3-
import { useUnityAuthService } from '$lib/context/UnityAuthContext';
3+
import { useUnityAuthContext, useUnityAuthService } from '$lib/context/UnityAuthContext';
44
import { goto } from '$app/navigation';
55
import { checkHasMessage, isHateoasErrorResponse } from '$lib/services/ServerErrors/ServerErrors';
66
import { isAxiosError } from 'axios';
77
import Login from '$lib/components/Login/Login.svelte';
88
import type { EventDispatchTypeMap } from '$lib/components/Login/login';
99
10+
const unityAuthContext = useUnityAuthContext();
1011
const authService = useUnityAuthService();
12+
const user = unityAuthContext.user;
1113
1214
let emailInput = createInput('');
1315
let passwordInput = createInput('');
@@ -29,7 +31,9 @@
2931
if (emailInput.value && passwordInput.value) {
3032
try {
3133
await authService.login(emailInput.value, passwordInput.value);
32-
goto('/tenant');
34+
35+
if ($user?.tenants) goto('/tenant');
36+
else goto('/settings');
3337
} catch (error: unknown) {
3438
if (isAxiosError(error) && isHateoasErrorResponse(error.response?.data)) {
3539
const hateoasError = error.response.data;

0 commit comments

Comments
 (0)