import { t } from "@lingui/macro"; import { CSSResult } from "lit"; import { TemplateResult, html } from "lit"; import { customElement } from "lit/decorators"; import AKGlobal from "../../../authentik.css"; import PFButton from "@patternfly/patternfly/components/Button/button.css"; import PFForm from "@patternfly/patternfly/components/Form/form.css"; import PFFormControl from "@patternfly/patternfly/components/FormControl/form-control.css"; import PFLogin from "@patternfly/patternfly/components/Login/login.css"; import PFTitle from "@patternfly/patternfly/components/Title/title.css"; import PFBase from "@patternfly/patternfly/patternfly-base.css"; import { PlexAuthenticationChallenge, PlexAuthenticationChallengeResponseRequest, } from "@goauthentik/api"; import { SourcesApi } from "@goauthentik/api"; import { DEFAULT_CONFIG } from "../../../api/Config"; import { PlexAPIClient, popupCenterScreen } from "../../../api/Plex"; import { MessageLevel } from "../../../elements/messages/Message"; import { showMessage } from "../../../elements/messages/MessageContainer"; import { BaseStage } from "../../stages/base"; @customElement("ak-flow-sources-plex") export class PlexLoginInit extends BaseStage< PlexAuthenticationChallenge, PlexAuthenticationChallengeResponseRequest > { static get styles(): CSSResult[] { return [PFBase, PFLogin, PFForm, PFFormControl, PFButton, PFTitle, AKGlobal]; } async firstUpdated(): Promise { const authInfo = await PlexAPIClient.getPin(this.challenge?.clientId || ""); const authWindow = popupCenterScreen(authInfo.authUrl, "plex auth", 550, 700); PlexAPIClient.pinPoll(this.challenge?.clientId || "", authInfo.pin.id).then((token) => { authWindow?.close(); new SourcesApi(DEFAULT_CONFIG) .sourcesPlexRedeemTokenCreate({ plexTokenRedeemRequest: { plexToken: token, }, slug: this.challenge?.slug || "", }) .then((r) => { window.location.assign(r.to); }) .catch((r: Response) => { r.json().then((body: { detail: string }) => { showMessage({ level: MessageLevel.error, message: body.detail, }); setTimeout(() => { window.location.assign("/"); }, 5000); }); }); }); } render(): TemplateResult { return html`

${t`Authenticating with Plex...`}

`; } }