/
index.ts
166 lines (146 loc) · 4.37 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { CommonResponseCode } from '../Api/StApi';
import { favoritesCache, tripsDetailedCache, userCache } from '../Cache';
import { reset as resetChanges } from '../Changes';
import * as Dao from './DataAccess';
import {
AuthenticationResponseCode,
AuthResponse,
RegistrationResponseCode,
ResetPasswordResponseCode,
Session
} from './Session';
import {
PrivacyConsentPayload,
ThirdPartyAuthType,
UserInfo,
UserLicense,
UserSettings
} from './User';
export {
AuthenticationResponseCode,
AuthResponse,
RegistrationResponseCode,
ResetPasswordResponseCode,
ThirdPartyAuthType,
Session,
UserInfo,
UserLicense,
UserSettings
};
export async function getSession(): Promise<Session | null> {
return Dao.getUserSession();
}
export async function getUserSettings(): Promise<UserSettings> {
return Dao.getUserSettings();
}
export async function updateUserSettings(settings: UserSettings): Promise<UserSettings> {
return Dao.updateUserSettings(settings);
}
export async function setSession(userSession: Session | null): Promise<void> {
await userCache.reset();
await favoritesCache.reset();
await tripsDetailedCache.reset();
resetChanges();
await Dao.setUserSession(userSession);
}
export async function handleSettingsChange(): Promise<void> {
return Dao.handleSettingsChange();
}
export async function signInWithDeviceId(
deviceId: string,
devicePlatform: string
): Promise<AuthenticationResponseCode> {
const authResponse: AuthResponse = await Dao.getSessionWithDeviceId(deviceId, devicePlatform);
if (authResponse.code === AuthenticationResponseCode.OK) {
setSession(authResponse.session);
}
return authResponse.code;
}
export async function signInWithCredentials(
email: string,
password: string,
deviceId?: string,
devicePlatform?: string
): Promise<AuthenticationResponseCode> {
const authResponse: AuthResponse = await Dao.getSessionWithPassword(email, password, deviceId, devicePlatform);
if (authResponse.code === 'OK') {
await setSession(authResponse.session);
}
return authResponse.code;
}
export async function signInWithFacebookAccessToken(
accessToken: string | null,
deviceId?: string,
devicePlatform?: string
): Promise<AuthenticationResponseCode> {
const authResponse: AuthResponse = await Dao.getSessionWithThirdPartyAuth(
ThirdPartyAuthType.facebook, accessToken, deviceId, devicePlatform
);
if (authResponse.code === AuthenticationResponseCode.OK) {
await setSession(authResponse.session);
}
return authResponse.code;
}
export async function signInWithGoogleIdToken(
accessToken: string | null,
deviceId?: string,
devicePlatform?: string
): Promise<AuthenticationResponseCode> {
const authResponse: AuthResponse = await Dao.getSessionWithThirdPartyAuth(
ThirdPartyAuthType.google, accessToken, deviceId, devicePlatform
);
if (authResponse.code === AuthenticationResponseCode.OK) {
await setSession(authResponse.session);
}
return authResponse.code;
}
export async function signInWithJwtToken(
jwt: string,
deviceId?: string,
devicePlatform?: string
): Promise<AuthenticationResponseCode> {
const authResponse: AuthResponse = await Dao.getSessionWithJwt(
jwt, deviceId, devicePlatform
);
if (authResponse.code === AuthenticationResponseCode.OK) {
await setSession(authResponse.session);
}
return authResponse.code;
}
export async function register(
email: string,
password: string,
name: string
): Promise<RegistrationResponseCode> {
await Dao.registerUser(email, password, name);
return RegistrationResponseCode.OK;
}
export async function getUserInfo(): Promise<UserInfo> {
const session = await getSession();
if (!session) {
throw new Error('User session is not set');
}
return Dao.getUserInfo();
}
export async function deleteAccount(id: string, hash: string): Promise<void> {
await Dao.deleteAccount(id, hash);
}
export async function requestCancelAccount(): Promise<void> {
await Dao.requestCancelAccount();
}
export async function resetPassword(email: string): Promise<ResetPasswordResponseCode> {
return Dao.resetPassword(email);
}
export async function unsubscribeEmail(hash?: string): Promise<CommonResponseCode> {
if (hash) {
return Dao.unsubscribeEmail(hash);
}
const session = await getSession();
if (!session) {
throw new Error('User session is not set');
}
return Dao.unsubscribeEmail();
}
export async function setPrivacyConsent(payload: PrivacyConsentPayload): Promise<void> {
return Dao.setPrivacyConsent(payload);
}