Skip to content

Commit

Permalink
fix: Change login flow
Browse files Browse the repository at this point in the history
  • Loading branch information
melsener committed Apr 1, 2024
1 parent 4f96122 commit 70468f5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 71 deletions.
74 changes: 50 additions & 24 deletions src/client/containers/LoginContainer/LoginContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import React, { FunctionComponent } from "react";
import React, { FunctionComponent, useState } from "react";

import { useInitialize } from "../../hooks";
import { useLogin } from "./hooks";
import { authentication } from "@microsoft/teams-js";
import { Login } from "./components";
import { Loader } from "@fluentui/react-northstar";
import { useRouter } from "next/router";
import { url, requester, storage } from "../../lib";

const errorToText = (error?: string): string => {
switch (error) {
case "CancelledByUser":
case "access_denied":
return "You need to authorize Microsoft Teams app to connect your Zeplin projects and styleguides.";
default:
return "Authorization failed due to an API related connectivity issue. Please retry logging in.";
}
};

export const LoginContainer: FunctionComponent = () => {
const {
query: {
Expand All @@ -20,30 +30,46 @@ export const LoginContainer: FunctionComponent = () => {
} = useRouter();

const { isInitializeLoading } = useInitialize();
const [login, { loginError }] = useLogin({
onSuccess: async (code?: string) => {
try {
const { accessToken, refreshToken } = await requester.createAuthToken(String(code));
storage.setAccessToken(accessToken);
storage.setRefreshToken(refreshToken);
} catch (err) {
// TODO: log to sentry
const [loginError, setLoginError] = useState<string | undefined>();

async function authenticate() {
try {
const code = await authentication.authenticate({
height: 476,
url: "/api/auth/authorize"
});
return code;
} catch (err) {
setLoginError(errorToText((err as unknown as Error).message));
}
}

async function login() {
try {
const code = await authenticate();
if (!code) {
throw Error("Authentication code is missing");
}
replace(id
? url.getConfigurationUpdateUrl({
channel: channel as string,
id: id as string,
resourceName: resourceName as string,
resourceType: resourceType as string,
theme: theme as string
})
: url.getConfigurationCreateUrl({

channel: channel as string,
theme: theme as string
}));
const { accessToken, refreshToken } = await requester.createAuthToken(String(code));
storage.setAccessToken(accessToken);
storage.setRefreshToken(refreshToken);
} catch (err) {
// TODO: log to sentry
}
});

replace(id
? url.getConfigurationUpdateUrl({
channel: channel as string,
id: id as string,
resourceName: resourceName as string,
resourceType: resourceType as string,
theme: theme as string
})
: url.getConfigurationCreateUrl({
channel: channel as string,
theme: theme as string
}));
}

if (isInitializeLoading) {
return <Loader styles={{ height: "100vh" }} />;
Expand Down
1 change: 0 additions & 1 deletion src/client/containers/LoginContainer/hooks/index.ts

This file was deleted.

46 changes: 0 additions & 46 deletions src/client/containers/LoginContainer/hooks/useLogin.ts

This file was deleted.

0 comments on commit 70468f5

Please sign in to comment.