File tree Expand file tree Collapse file tree 4 files changed +22
-10
lines changed Expand file tree Collapse file tree 4 files changed +22
-10
lines changed Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ export const TenantsSchema = z.object({
5
5
name : z . string ( )
6
6
} ) ;
7
7
8
- export type LibrePermissions = z . infer < typeof TenantsSchema > ;
8
+ export type Tenants = z . infer < typeof TenantsSchema > ;
9
9
10
10
export const TenantsSuccessResponseSchema = z . array ( TenantsSchema ) ;
11
11
Original file line number Diff line number Diff line change @@ -50,21 +50,27 @@ export class UnityAuthServiceImpl
50
50
}
51
51
52
52
async login ( email : string , password : string ) : Promise < CompleteLoginResponse > {
53
+ let completeLoginRes : CompleteLoginResponse ;
54
+
53
55
const res = await this . axiosInstance . post ( '/api/login' , {
54
56
username : email ,
55
57
password : password
56
58
} ) ;
57
59
58
60
const loginRes = UnityAuthLoginResponseSchema . parse ( res . data ) ;
59
61
60
- const tenantsRes = await this . tenantsResolver . getTenants ( loginRes ) ;
62
+ completeLoginRes = { ... loginRes } ;
61
63
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
+ }
66
69
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
+ }
68
74
69
75
this . loginData = completeLoginRes ;
70
76
Original file line number Diff line number Diff line change 1
1
import { z } from 'zod' ;
2
2
import type { TenantsResolver } from '../TenantsResolver/TenantsResolver' ;
3
+ import { TenantsSchema } from '../TenantsResolver/shared' ;
3
4
4
5
export const UnityAuthServicePropsSchema = z . object ( {
5
6
baseURL : z . string ( )
@@ -13,7 +14,8 @@ export const UnityAuthLoginResponseSchema = z.object({
13
14
access_token : z . string ( ) ,
14
15
token_type : z . string ( ) ,
15
16
expires_in : z . number ( ) ,
16
- username : z . string ( )
17
+ username : z . string ( ) ,
18
+ tenants : z . array ( TenantsSchema ) . optional ( )
17
19
} ) ;
18
20
19
21
export type UnityAuthLoginResponse = z . infer < typeof UnityAuthLoginResponseSchema > ;
Original file line number Diff line number Diff line change 1
1
<script lang =" ts" >
2
2
import { createInput , emailValidator } from ' $lib/utils/validation' ;
3
- import { useUnityAuthService } from ' $lib/context/UnityAuthContext' ;
3
+ import { useUnityAuthContext , useUnityAuthService } from ' $lib/context/UnityAuthContext' ;
4
4
import { goto } from ' $app/navigation' ;
5
5
import { checkHasMessage , isHateoasErrorResponse } from ' $lib/services/ServerErrors/ServerErrors' ;
6
6
import { isAxiosError } from ' axios' ;
7
7
import Login from ' $lib/components/Login/Login.svelte' ;
8
8
import type { EventDispatchTypeMap } from ' $lib/components/Login/login' ;
9
9
10
+ const unityAuthContext = useUnityAuthContext ();
10
11
const authService = useUnityAuthService ();
12
+ const user = unityAuthContext .user ;
11
13
12
14
let emailInput = createInput (' ' );
13
15
let passwordInput = createInput (' ' );
29
31
if (emailInput .value && passwordInput .value ) {
30
32
try {
31
33
await authService .login (emailInput .value , passwordInput .value );
32
- goto (' /tenant' );
34
+
35
+ if ($user ?.tenants ) goto (' /tenant' );
36
+ else goto (' /settings' );
33
37
} catch (error : unknown ) {
34
38
if (isAxiosError (error ) && isHateoasErrorResponse (error .response ?.data )) {
35
39
const hateoasError = error .response .data ;
You can’t perform that action at this time.
0 commit comments