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
167
168
169
170
| import { AppleWebConfig } from '@/shared/models/AppleWebConfig';
import {
AuthConfig,
reloadSession,
SNSLoginBindResponse,
} from '@/client/auth/index';
import { loadScript } from '@/client/utils/scriptUtils';
import axios from 'redaxios';
import { PlatformReportType } from '@/shared/types/reportTypes/platform';
import { GameConst } from '@/shared/constant/gameConst';
import { showLoginLinkTip } from '../common/popup';
import { setCookie } from '../common/cookie';
let appleWebConfig: AppleWebConfig;
/**
* apple signIn handler
* @param data authInfo
*/
async function appleSignInSuccess(
data: AppleSignInAPI.SignInResponseI
): Promise<void> {
const idToken = data.authorization.id_token;
const params = {
appId: window.option.appId,
idToken,
provider_type: 'apple',
};
const loginResponse = await axios({
url: '/oauth/v1/login',
method: 'post',
params,
}).catch((error) => {
showLoginLinkTip(error);
});
if (loginResponse) {
const result = loginResponse.data as SNSLoginBindResponse;
showLoginLinkTip(result.code);
reloadSession('visibilitychange');
} else {
showLoginLinkTip('network timeout, please try again later');
}
}
/**
* apple link success handler
* @param data authInfo
*/
async function appleLinkSuccess(
data: AppleSignInAPI.SignInResponseI
): Promise<void> {
const idToken = data.authorization.id_token;
const params = {
appId: window.option.appId,
idToken,
provider_type: 'apple',
};
const linkResponse = await axios({
url: '/oauth/v1/link',
method: 'post',
params,
}).catch((error) => {
showLoginLinkTip(error);
});
if (linkResponse) {
const result = linkResponse.data as SNSLoginBindResponse;
showLoginLinkTip(result.code);
reloadSession('visibilitychange');
} else {
showLoginLinkTip('network timeout ,please try again later');
}
}
// eslint-disable-next-line @typescript-eslint/ban-types
function appleSignInFail(error: object): void {
console.error(error);
}
function getQueryVariable(variable: string): string | null {
return new URL(window.location.href).searchParams.get(variable);
}
/**
* apple init
*/
export async function appleInit(): Promise<void> {
const cookie = getQueryVariable(GameConst.appleFeatureSwitchCookie);
if (cookie) {
setCookie(GameConst.appleFeatureSwitchCookie, cookie, 360000);
}
// get the apple config
const response = await axios({
url: '/oauth/v1/config',
method: 'get',
});
const config: AuthConfig = response.data as AuthConfig;
if (!config.apple) {
console.error("can't get the apple config");
return;
}
$('#apple-login').removeClass('hidden');
$('#apple-link').removeClass('hidden');
appleWebConfig = new AppleWebConfig();
Object.assign(appleWebConfig, config.apple);
// use the config instance the AppleID
await loadScript(appleWebConfig.sdk_url).catch((e) => console.error(e));
console.info('apple environment ready!');
}
/**
* login listener
*/
$('#apple-login').on('click', async () => {
if (!AppleID) {
showLoginLinkTip();
return;
}
try {
AppleID.auth.init({
clientId: appleWebConfig.identifier,
scope: appleWebConfig.scope,
redirectURI: appleWebConfig.login_callback,
usePopup: true,
});
if (!AppleID) {
showLoginLinkTip();
return;
}
const data: AppleSignInAPI.SignInResponseI = await AppleID.auth.signIn();
if (data) {
appleSignInSuccess(data);
}
} catch (e) {
appleSignInFail(e);
}
});
/**
* apple link listener
*/
$('#apple-link').on('click', async () => {
if (!AppleID) {
showLoginLinkTip();
return;
}
try {
AppleID.auth.init({
clientId: appleWebConfig.identifier,
scope: appleWebConfig.scope,
redirectURI: appleWebConfig.link_callback,
usePopup: true,
});
if (!AppleID) {
showLoginLinkTip();
return;
}
const data: AppleSignInAPI.SignInResponseI = await AppleID.auth.signIn();
if (data) {
appleLinkSuccess(data);
}
} catch (e) {
appleSignInFail(e);
}
});
|