Skip to content

Instantly share code, notes, and snippets.

@houko
Created December 23, 2020 09:22
Show Gist options
  • Save houko/f73f74459df58e27f5e256fff274e8fe to your computer and use it in GitHub Desktop.
Save houko/f73f74459df58e27f5e256fff274e8fe to your computer and use it in GitHub Desktop.
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 { GameConst } from '@/shared/constant/gameConst';
import { setCookie } from '@/client/common/cookie';
import { fetchAuthConfig } from '@/client/common/config';
import { showLoginLinkTip } from '@/client/common/popup';
import qs from 'qs';
let appleWebConfig: AppleWebConfig;
/**
* apple signIn handler
* @param data authInfo
*/
async function appleSignInSuccess(
data: AppleSignInAPI.SignInResponseI
): Promise<void> {
// eslint-disable-next-line camelcase
const { id_token, code } = data.authorization;
const params = {
appId: window.option.appId,
id_token,
code,
};
const loginResponse = await axios({
url: '/oauth/v1/login',
method: 'post',
headers: { 'content-type': 'application/x-www-form-urlencoded' },
params: { provider_type: 'apple' },
data: qs.stringify(params),
}).catch((error) => {
showLoginLinkTip(error);
});
if (loginResponse) {
const result = loginResponse.data as SNSLoginBindResponse;
showLoginLinkTip(result.code);
if (result.code === 'SNS_LOGIN_SUCCESS') {
reloadSession('appleLogin');
}
} else {
showLoginLinkTip('network timeout, please try again later');
}
}
/**
* apple link success handler
* @param data authInfo
*/
async function appleLinkSuccess(
data: AppleSignInAPI.SignInResponseI
): Promise<void> {
// eslint-disable-next-line camelcase
const { id_token, code } = data.authorization;
const params = {
appId: window.option.appId,
id_token,
code,
};
const linkResponse = await axios({
url: '/oauth/v1/link',
method: 'post',
headers: { 'content-type': 'application/x-www-form-urlencoded' },
params: { provider_type: 'apple' },
data: qs.stringify(params),
}).catch((error) => {
showLoginLinkTip(error);
});
if (linkResponse) {
const result = linkResponse.data as SNSLoginBindResponse;
showLoginLinkTip(result.code);
if (result.code === 'SNS_LINK_SUCCESS') {
reloadSession('appleLink');
}
} 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 initApple(): Promise<void> {
const cookie = getQueryVariable(GameConst.appleFeatureSwitchCookie);
if (cookie) {
setCookie(GameConst.appleFeatureSwitchCookie, cookie, 360000);
}
// get the apple config
const config: AuthConfig = await fetchAuthConfig();
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));
AppleID.auth.init({
clientId: appleWebConfig.identifier,
scope: appleWebConfig.scope,
redirectURI: appleWebConfig.login_callback,
usePopup: true,
});
console.info('apple environment ready!');
}
/**
* login listener
*/
$('#apple-login').on('click', async () => {
if (!AppleID) {
showLoginLinkTip();
return;
}
try {
const data: AppleSignInAPI.SignInResponseI = await AppleID.auth.signIn({
redirectURI: appleWebConfig.login_callback,
});
if (data) {
appleSignInSuccess(data);
}
} catch (e) {
appleSignInFail(e);
}
});
/**
* apple link listener
*/
$('#apple-link').on('click', async () => {
if (!AppleID) {
showLoginLinkTip();
return;
}
try {
const data: AppleSignInAPI.SignInResponseI = await AppleID.auth.signIn({
redirectURI: appleWebConfig.link_callback,
});
if (data) {
appleLinkSuccess(data);
}
} catch (e) {
appleSignInFail(e);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment