|
@@ -0,0 +1,49 @@
|
|
|
|
+import { Request as ExpressRequest, Response as ExpressResponse } from "express";
|
|
|
|
+import { OAuth2 } from "src/utils/util";
|
|
|
|
+import { ENVIRONMENT } from "src/utils/environment";
|
|
|
|
+import { Option } from "@shared/common/async_utils";
|
|
|
|
+import { logger } from "src/utils/logging";
|
|
|
|
+
|
|
|
|
+export const get = async (req: ExpressRequest, res: ExpressResponse): Promise<void> => {
|
|
|
|
+ res.redirect(OAuth2.getAuthUrl({
|
|
|
|
+ client_id: ENVIRONMENT.clientId,
|
|
|
|
+ redirect_url: ENVIRONMENT.redirectUrl,
|
|
|
|
+ response_type: "code",
|
|
|
|
+ scope: "identify",
|
|
|
|
+ }));
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+type AuthResult = Option<unknown, {error: string}>;
|
|
|
|
+
|
|
|
|
+export const post = async (req: ExpressRequest, res: ExpressResponse):
|
|
|
|
+ Promise<ExpressResponse<AuthResult>> => {
|
|
|
|
+ if (!req.session) {
|
|
|
|
+ logger.error("WEB: req.session is not set up correctly!");
|
|
|
|
+ return res.json({
|
|
|
|
+ ok: false,
|
|
|
|
+ error: "No session is set up. This is a server error!",
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ if (!req.session.authTokenCode) {
|
|
|
|
+ logger.error("WEB: attempted to join with no authTokenCode set!");
|
|
|
|
+ return res.json({
|
|
|
|
+ ok: false,
|
|
|
|
+ error: "Authentication token is missing. Please try logging in again.",
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ const result = await OAuth2.getToken({
|
|
|
|
+ client_id: ENVIRONMENT.clientId,
|
|
|
|
+ client_secret: ENVIRONMENT.clientSecret,
|
|
|
|
+ grant_type: "authorization_code",
|
|
|
|
+ code: req.session.authTokenCode,
|
|
|
|
+ scope: "identify",
|
|
|
|
+ redirect_uri: ENVIRONMENT.redirectUrl,
|
|
|
|
+ });
|
|
|
|
+ if (!result.ok) {
|
|
|
|
+ return res.json(result);
|
|
|
|
+ }
|
|
|
|
+ req.sessionOptions.maxAge = result.expires_in;
|
|
|
|
+ return res.json({
|
|
|
|
+ ok: true,
|
|
|
|
+ });
|
|
|
|
+};
|