// @ts-nocheck import { fetchService } from "./fetch"; import AsyncStorage from "@react-native-community/async-storage"; import { getConfig } from "../config"; export const AUTH_PROVIDERS = { debug: "debug", battlenet: "battlenet", bowltv: "bowltv", coachella: "coachella", cornell: "cornell", facebook: "facebook", faceit: "faceit", livexlive: "livexlive", maestro: "maestro", openid: "openid", origin: "origin", playstation: "playstation", steam: "steam", twitch: "twitch", uplay: "uplay", wargaming: "wargaming", xbox: "xbox", } as any; export const AUTH_LOGIN_BUTTON = { ...Object.keys(AUTH_PROVIDERS).reduce( (all, current) => ({ ...all, [current]: `Login with ${current}` }), {} ), [AUTH_PROVIDERS.bowltv]: "USBC Community Login", }; export const AUTH_LOGIN_BUTTON_STYLE = { ...Object.keys(AUTH_PROVIDERS).reduce( (all, current) => ({ ...all, [current]: { backgroundColor: "#fff", color: "#222", }, }), {} ), facebook: { backgroundColor: "#415a93", color: "#fff", }, twitch: { backgroundColor: "#5e46a0", color: "#fff", }, }; const TOKEN_KEY = "token"; const REFRESH_TOKEN_KEY = "refreshToken"; export interface AuthResult { jwt: string; refreshToken: string; service: string; } const AuthEndpoints = { exchange: "auth/v2/exchange", login: "auth/v2/login", exchangeBowlTvToken: "auth/v1/login/bowltv", }; class AuthService { private statusSubscribers: ((authenticated: boolean) => any)[] = []; private removeTokenInterceptor: () => any; public jwt?: string; constructor() { AsyncStorage.getItem(TOKEN_KEY).then((jwt) => { this.addTokenInterceptor(jwt); }); } private handleAuthResult = (payload: AuthResult) => { let { jwt, refreshToken } = payload; AsyncStorage.setItem(TOKEN_KEY, jwt || "no-token"); AsyncStorage.setItem(REFRESH_TOKEN_KEY, refreshToken || "no-token"); this.addTokenInterceptor(jwt); this.statusSubscribers.map((fn) => fn(!!jwt)); return payload; }; private handleAuthResultBowltv = (payload: any) => { let { jwt, refreshToken } = payload.accountCollectionTokens; AsyncStorage.setItem(TOKEN_KEY, jwt || "no-token"); AsyncStorage.setItem(REFRESH_TOKEN_KEY, refreshToken || "no-token"); this.addTokenInterceptor(jwt); this.statusSubscribers.map((fn) => fn(!!jwt)); return payload; }; private addTokenInterceptor = (jwt: string) => { this.jwt = jwt; if (this.removeTokenInterceptor) { this.removeTokenInterceptor(); } this.removeTokenInterceptor = fetchService.addInterceptor((request) => { if (!jwt) { return request; } return { ...request, headers: { ...request.headers, Authorization: `Bearer ${jwt}`, }, }; }); }; exchangeToken = (token: string) => { return fetchService .post(AuthEndpoints.exchange, { token }) .then(this.handleAuthResult); }; exchangeBowlTvToken = (token: string) => { return fetchService .post(AuthEndpoints.exchangeBowlTvToken, { access_token: token, site_id: getConfig().SITE_ID, firebase: "maestro-04", }) .then(this.handleAuthResultBowltv); }; maestroLogin = (email: string, password: string) => { return fetchService .post(AuthEndpoints.login, { email, password }) .then(this.handleAuthResult); }; generateAuthUrl = (siteId: string, provider: keyof typeof AUTH_PROVIDERS) => { if (provider === "debug") { return "http://umbra.ddns.net:8080/"; } return `${ getConfig().API_URL }/auth/v2/login/oauth/link/${siteId}/${provider}`; }; isAuthenticated = () => { return AsyncStorage.getItem(TOKEN_KEY).then( (token) => token && token !== "no-token" ); }; subscribe = (fn: (status: boolean) => any) => { this.statusSubscribers = [...this.statusSubscribers, fn]; }; logout = () => { this.handleAuthResult({ jwt: "", refreshToken: "", service: "", }); }; // TODO: Remove debugAuthState = () => { return Promise.resolve( this.handleAuthResult({ jwt: "fake-jwt", refreshToken: "fake-refresh-token", service: "fake-service", }) ); }; } export const authService = new AuthService();