aboutsummaryrefslogtreecommitdiff
path: root/src/server/ApiManagers/GeneralGoogleManager.ts
blob: cb37b0dcec932ace2096fc5f59bdc0ede31011d3 (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
import ApiManager, { Registration } from "./ApiManager";
import { Method, _permission_denied } from "../RouteManager";
import { uploadDirectory } from "..";
import { path } from "animejs";
import { RouteStore } from "../RouteStore";
import { GoogleApiServerUtils } from "../apis/google/GoogleApiServerUtils";
import { Database } from "../database";

const deletionPermissionError = "Cannot perform specialized delete outside of the development environment!";

export default class GeneralGoogleManager extends ApiManager {

    protected initialize(register: Registration): void {

        register({
            method: Method.GET,
            subscription: RouteStore.readGoogleAccessToken,
            onValidation: async ({ user, res }) => {
                const userId = user.id;
                const token = await GoogleApiServerUtils.retrieveAccessToken(userId);
                if (!token) {
                    return res.send(GoogleApiServerUtils.generateAuthenticationUrl());
                }
                return res.send(token);
            }
        });

        register({
            method: Method.POST,
            subscription: RouteStore.writeGoogleAccessToken,
            onValidation: async ({ user, req, res }) => {
                res.send(await GoogleApiServerUtils.processNewUser(user.id, req.body.authenticationCode));
            }
        });

        register({
            method: Method.GET,
            subscription: "/deleteWithGoogleCredentials",
            onValidation: async ({ res, isRelease }) => {
                if (isRelease) {
                    return _permission_denied(res, deletionPermissionError);
                }
                await Database.Auxiliary.GoogleAuthenticationToken.DeleteAll();
                res.redirect(RouteStore.delete);
            }
        });
    }
}