aboutsummaryrefslogtreecommitdiff
path: root/src/client/apis/AuthenticationManager.tsx
blob: 360554b8e35dc6329b8f92b412507033c905522b (plain)
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
import { observable, action, reaction, runInAction } from "mobx";
import { observer } from "mobx-react";
import * as React from "react";
import MainViewModal from "../views/MainViewModal";
import { Opt } from "../../new_fields/Doc";
import { Identified } from "../Network";
import { RouteStore } from "../../server/RouteStore";

const AuthenticationUrl = "https://accounts.google.com/o/oauth2/v2/auth";

@observer
export default class AuthenticationManager extends React.Component<{}> {
    public static Instance: AuthenticationManager;
    @observable private openState = false;
    private authenticationLink: Opt<string> = undefined;
    @observable private authenticationCode: Opt<string> = undefined;
    @observable private clickedState = false;

    private get isOpen() {
        return this.openState;
    }

    private set isOpen(value: boolean) {
        runInAction(() => this.openState = value);
    }

    private get hasBeenClicked() {
        return this.clickedState;
    }

    private set hasBeenClicked(value: boolean) {
        runInAction(() => this.clickedState = value);
    }

    public executeFullRoutine = async (service: string) => {
        let response = await Identified.FetchFromServer(`/read${service}AccessToken`);
        // if this is an authentication url, activate the UI to register the new access token
        if (new RegExp(AuthenticationUrl).test(response)) {
            this.isOpen = true;
            this.authenticationLink = response;
            return new Promise<string>(async resolve => {
                const disposer = reaction(
                    () => this.authenticationCode,
                    authenticationCode => {
                        if (authenticationCode) {
                            Identified.PostToServer(`/write${service}AccessToken`, { authenticationCode }).then(token => {
                                this.isOpen = false;
                                this.hasBeenClicked = false;
                                resolve(token);
                                disposer();
                            });
                        }
                    }
                );
            });
        }
        // otherwise, we already have a valid, stored access token
        return response;
    }

    constructor(props: {}) {
        super(props);
        AuthenticationManager.Instance = this;
    }

    private handleClick = () => {
        window.open(this.authenticationLink);
        this.hasBeenClicked = true;
    }

    private handlePaste = action((e: React.ChangeEvent<HTMLInputElement>) => {
        this.authenticationCode = e.currentTarget.value;
    });

    private get renderPrompt() {
        return (
            <div style={{ display: "flex", flexDirection: "column" }}>
                <button onClick={this.handleClick}>Please click here to authorize a Google account...</button>
                {this.clickedState ? <input
                    onChange={this.handlePaste}
                    placeholder={"Please paste the external authetication code here..."}
                    style={{ marginTop: 15 }}
                /> : (null)}
            </div>
        );
    }

    render() {
        return (
            <MainViewModal
                isDisplayed={this.openState}
                interactive={true}
                contents={this.renderPrompt}
            />
        );
    }

}