-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[recnet-web] RecNet Slack OAuth Flow (#361)
## Description <!--- Describe your changes in detail --> Finish slack oauth flow. ## Related Issue <!--- This project only accepts pull requests related to open issues --> <!--- If suggesting a new feature or change, please discuss it in an issue first --> <!--- If fixing a bug, there should be an issue describing it with steps to reproduce --> <!--- Please link to the issue here: --> - #261 - #64 ## Notes <!-- Other thing to say --> ## Test <!--- Please describe in detail how you tested your changes locally. --> Before test, update `.env.local` - `NEXT_PUBLIC_BASE_URL` should be `https://localhost:3000` - Add `SLACK_APP_CLIENT_ID` - Add `SLACK_OAUTH_APP_SCOPES` - Add`SLACK_OAUTH_REDIRECT_URI="/api/slack/oauth/callback"` Run `nx dev:ssl recnet` and go to `https://localhost:3000` - Go to setting and subscription tab -> Click the button to install slack app to your workspace - Should redirect you to slack page - Should able to choose different workspace - After clicking "Allow", should redirect back to frontend and see the success message - Go to setting again, should see the workspace name of where you install the app ## Screenshots (if appropriate): <!--- Add screenshots of your changes here --> ![Screenshot 2024-11-21 at 5 59 40 PM](https://github.com/user-attachments/assets/76d381f6-4dfd-49ee-976a-944b686a6d55) ![Screenshot 2024-11-21 at 5 59 50 PM](https://github.com/user-attachments/assets/092f53a1-81aa-4914-bebb-2ebc6f27f1d5) ![Screenshot 2024-11-21 at 6 00 07 PM](https://github.com/user-attachments/assets/cae362f9-58b8-4b0a-9f2a-ea4c1eb7dfc4) ## TODO - [ ] Clear `console.log` or `console.error` for debug usage - [ ] Update the documentation `recnet-docs` if needed
- Loading branch information
Showing
15 changed files
with
305 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,7 @@ Thumbs.db | |
|
||
# Next.js | ||
.next | ||
certificates | ||
|
||
# env | ||
.env | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { redirect } from "next/navigation"; | ||
import { type NextRequest } from "next/server"; | ||
|
||
import { serverClient } from "@recnet/recnet-web/app/_trpc/serverClient"; | ||
|
||
export async function GET(req: NextRequest) { | ||
const searchParams = req.nextUrl.searchParams; | ||
const code = searchParams.get("code"); | ||
const errorDesc = searchParams.get("error_description"); | ||
|
||
if (!code) { | ||
redirect( | ||
`/feeds?slackOAuthStatus=error${errorDesc ? `&error_description=${errorDesc}` : ""}` | ||
); | ||
} | ||
let isSuccess = true; | ||
let workspaceName = ""; | ||
try { | ||
const data = await serverClient.slackOAuth2FA({ code }); | ||
workspaceName = data.workspaceName; | ||
} catch (e) { | ||
isSuccess = false; | ||
} | ||
redirect( | ||
`/feeds?slackOAuthStatus=${isSuccess ? `success&workspace_name=${workspaceName}` : "error"}${errorDesc ? `&error_description=${errorDesc}` : ""}` | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { redirect } from "next/navigation"; | ||
|
||
import { generateOAuthLink } from "../slackAppInstallHelper"; | ||
|
||
export async function GET(req: Request) { | ||
redirect(generateOAuthLink()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { serverEnv } from "@recnet/recnet-web/serverEnv"; | ||
|
||
export function generateOAuthLink(): string { | ||
return `https://slack.com/oauth/v2/authorize?scope=${serverEnv.SLACK_OAUTH_APP_SCOPES}&client_id=${serverEnv.SLACK_APP_CLIENT_ID}&redirect_uri=${serverEnv.SLACK_OAUTH_REDIRECT_URI}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
"use client"; | ||
import { Button, Dialog } from "@radix-ui/themes"; | ||
import { useSearchParams, useRouter, usePathname } from "next/navigation"; | ||
import { useEffect, useState } from "react"; | ||
|
||
/** | ||
* Modal to display the result of slack OAuth flow | ||
*/ | ||
export function SlackOAuthModal() { | ||
const [shouldShow, setShouldShow] = useState(false); | ||
const [oauthStatus, setOAuthStatus] = useState<"success" | "error" | null>( | ||
null | ||
); | ||
const pathname = usePathname(); | ||
const router = useRouter(); | ||
|
||
const searchParams = useSearchParams(); | ||
|
||
useEffect(() => { | ||
const status = searchParams.get("slackOAuthStatus"); | ||
if (status) { | ||
setShouldShow(true); | ||
setOAuthStatus(status as "success" | "error"); | ||
} | ||
}, [searchParams]); | ||
|
||
if (!shouldShow || !oauthStatus) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Dialog.Root | ||
open={shouldShow} | ||
onOpenChange={(open) => { | ||
// when closed, remove the search param | ||
if (!open) { | ||
router.replace(pathname); | ||
} | ||
setShouldShow(open); | ||
}} | ||
> | ||
<Dialog.Content | ||
maxWidth={{ initial: "450px", md: "450px" }} | ||
style={{ | ||
maxHeight: "75vh", | ||
padding: "0", | ||
}} | ||
> | ||
<div className="flex flex-col px-6 pb-6 pt-8"> | ||
<Dialog.Title> | ||
{oauthStatus === "success" | ||
? "✅ You are all set!" | ||
: "❌ Slack OAuth flow failed"} | ||
</Dialog.Title> | ||
<Dialog.Description className="text-gray-11" size="2"> | ||
{oauthStatus === "success" | ||
? `Successfully installed the Slack app! You can now receive message from us in workspace: ${searchParams.get("workspace_name")}.` | ||
: searchParams.get("error_description") || | ||
"Slack OAuth flow failed. Please try again or contact us."} | ||
</Dialog.Description> | ||
<div className="flex flex-row justify-end items-center mt-8"> | ||
<Button | ||
className="mr-4" | ||
onClick={() => { | ||
setShouldShow(false); | ||
router.replace(pathname); | ||
}} | ||
> | ||
{oauthStatus === "success" ? "Got it!" : "Close"} | ||
</Button> | ||
</div> | ||
</div> | ||
</Dialog.Content> | ||
</Dialog.Root> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.