Skip to content
This repository has been archived by the owner on May 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #205 from tipisai/feat/auth
Browse files Browse the repository at this point in the history
Feat/auth
  • Loading branch information
Wangtaofeng authored May 14, 2024
2 parents 1c30eb5 + c62e69f commit ba54c69
Show file tree
Hide file tree
Showing 12 changed files with 265 additions and 211 deletions.
38 changes: 5 additions & 33 deletions apps/agent/src/components/Auth/AuthCheck/index.tsx
Original file line number Diff line number Diff line change
@@ -1,45 +1,17 @@
import { isEqual } from "lodash-es"
import { FC } from "react"
import { Navigate } from "react-router-dom"
import { TipisTrack } from "@illa-public/track-utils"
import { useGetUserInfoQuery } from "@illa-public/user-data"
import { getAuthToken } from "@illa-public/utils"
import i18n from "@/i18n"
import { useGetAuthSessionQuery } from "@illa-public/user-data"
import { AUTH_PAGE_PATH } from "@/router/constants"
import { BaseProtectComponentProps } from "@/router/interface"
import {
getSessionCurrentUserInfo,
setSessionCurrentUserInfo,
} from "@/utils/storage/user"

const AuthCheck: FC<BaseProtectComponentProps> = (props) => {
const token = getAuthToken()
const { data, isSuccess, error } = useGetUserInfoQuery(null)
const cacheCurrentUserInfo = getSessionCurrentUserInfo()
const { data, isLoading, error, isSuccess } = useGetAuthSessionQuery(null)

if (!token) {
return <Navigate to={AUTH_PAGE_PATH} />
}

if (error && "status" in error) {
if (error) {
return <Navigate to="/404" />
}

if (data) {
const currentLng = i18n.language
if (!isEqual(cacheCurrentUserInfo, data)) {
setSessionCurrentUserInfo(data)
TipisTrack.identify(data.userID, {
nickname: data.nickname,
email: data.email,
language: data.language,
})
}

if (data.language && data.language !== currentLng) {
i18n.changeLanguage(data.language)
return null
}
if (!isLoading && !data?.session) {
return <Navigate to={AUTH_PAGE_PATH} />
}

return isSuccess ? props.children : null
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// import { App, Button } from "antd"
// import { FC, useState } from "react"
// import { useTranslation } from "react-i18next"
// import { useNavigate } from "react-router-dom"
// import { useCancelLinkedMutation } from "@illa-public/user-data"
// import { useLazyGetOAuthURIQuery } from "@illa-public/user-data"
// import { SETTING_PASSWORD_PATH } from "@/router/constants"
// import { OAUTH_REDIRECT_URL } from "@/utils/oauth"
// import { LinkCardProps } from "./interface"
// import {
// buttonWrapperStyle,
// containerStyle,
// descriptionStyle,
// headerContainerStyle,
// iconContainerStyle,
// titleContainerStyle,
// titleStyle,
// } from "./style"

// export const LinkCard: FC<LinkCardProps> = (props) => {
// const { icon, title, description, type, handleClick } = props
// const { t } = useTranslation()
// const { message } = App.useApp()
// const [connectedLoading, setConnectedLoading] = useState(false)
// const [triggerGetOAuthURI] = useLazyGetOAuthURIQuery()
// const [cancelLinked] = useCancelLinkedMutation()

// const tipsNotHasPassword = () => {
// const tipsModal = modal.confirm({
// title: t("profile.setting.oauth.modal.title"),
// okText: t("profile.setting.oauth.modal.set_button"),
// cancelText: t("profile.setting.oauth.modal.cancel_button"),
// content: t("profile.setting.oauth.modal.description"),
// onOk: () => {
// tipsModal.update({
// open: false,
// })

// navigate(SETTING_PASSWORD_PATH)
// },
// onCancel: () => {
// tipsModal.update({
// open: false,
// })
// },
// })
// }

// const handleDisconnect = async () => {
// if (!hasPassword) {
// tipsNotHasPassword()
// return
// }
// try {
// setConnectedLoading(true)
// await cancelLinked(type)
// message.success({
// content: t("profile.setting.oauth.message.disconnect_suc"),
// })
// } catch (e) {
// message.error({
// content: t("profile.setting.oauth.message.disconnect_failed"),
// })
// } finally {
// setConnectedLoading(false)
// }
// }

// const handleConnect = async () => {
// try {
// setConnectedLoading(true)
// const OAuthURIResponse = await triggerGetOAuthURI({
// oauthAgency: type,
// landing: "connect",
// redirectURI: OAUTH_REDIRECT_URL,
// }).unwrap()
// const { uri } = OAuthURIResponse
// window.open(uri, "_self")
// } catch (e) {
// } finally {
// setConnectedLoading(false)
// }
// }

// return (
// <div css={containerStyle}>
// <div css={headerContainerStyle}>
// <div css={titleContainerStyle}>
// <span css={iconContainerStyle}>{icon}</span>
// <span css={titleStyle}>{title}</span>
// </div>
// <span css={descriptionStyle}>{description}</span>
// </div>
// <span css={buttonWrapperStyle}>
// {isConnected ? (
// <Button
// size="large"
// loading={connectedLoading}
// onClick={handleDisconnect}
// >
// {t("profile.setting.oauth.button.disconnect")}
// </Button>
// ) : (
// <Button
// type="primary"
// size="large"
// onClick={handleConnect}
// loading={connectedLoading}
// >
// {t("profile.setting.oauth.button.connect")}
// </Button>
// )}
// </span>
// </div>
// )
// }

export {}
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { App, Button } from "antd"
import { FC, useState } from "react"
import { Button } from "antd"
import { FC } from "react"
import { useTranslation } from "react-i18next"
import { useNavigate } from "react-router-dom"
import { useCancelLinkedMutation } from "@illa-public/user-data"
import { useLazyGetOAuthURIQuery } from "@illa-public/user-data"
import { SETTING_PASSWORD_PATH } from "@/router/constants"
import { OAUTH_REDIRECT_URL } from "@/utils/oauth"
import { LinkCardProps } from "./interface"
import {
buttonWrapperStyle,
Expand All @@ -18,70 +13,8 @@ import {
} from "./style"

export const LinkCard: FC<LinkCardProps> = (props) => {
const { icon, title, description, type, isConnected, hasPassword } = props
const { icon, title, description, handleClick, isConnected } = props
const { t } = useTranslation()
const { message, modal } = App.useApp()
const [connectedLoading, setConnectedLoading] = useState(false)
const navigate = useNavigate()
const [triggerGetOAuthURI] = useLazyGetOAuthURIQuery()
const [cancelLinked] = useCancelLinkedMutation()

const tipsNotHasPassword = () => {
const tipsModal = modal.confirm({
title: t("profile.setting.oauth.modal.title"),
okText: t("profile.setting.oauth.modal.set_button"),
cancelText: t("profile.setting.oauth.modal.cancel_button"),
content: t("profile.setting.oauth.modal.description"),
onOk: () => {
tipsModal.update({
open: false,
})

navigate(SETTING_PASSWORD_PATH)
},
onCancel: () => {
tipsModal.update({
open: false,
})
},
})
}

const handleDisconnect = async () => {
if (!hasPassword) {
tipsNotHasPassword()
return
}
try {
setConnectedLoading(true)
await cancelLinked(type)
message.success({
content: t("profile.setting.oauth.message.disconnect_suc"),
})
} catch (e) {
message.error({
content: t("profile.setting.oauth.message.disconnect_failed"),
})
} finally {
setConnectedLoading(false)
}
}

const handleConnect = async () => {
try {
setConnectedLoading(true)
const OAuthURIResponse = await triggerGetOAuthURI({
oauthAgency: type,
landing: "connect",
redirectURI: OAUTH_REDIRECT_URL,
}).unwrap()
const { uri } = OAuthURIResponse
window.open(uri, "_self")
} catch (e) {
} finally {
setConnectedLoading(false)
}
}

return (
<div css={containerStyle}>
Expand All @@ -94,20 +27,11 @@ export const LinkCard: FC<LinkCardProps> = (props) => {
</div>
<span css={buttonWrapperStyle}>
{isConnected ? (
<Button
size="large"
loading={connectedLoading}
onClick={handleDisconnect}
>
<Button size="large" onClick={handleClick}>
{t("profile.setting.oauth.button.disconnect")}
</Button>
) : (
<Button
type="primary"
size="large"
onClick={handleConnect}
loading={connectedLoading}
>
<Button type="primary" size="large" onClick={handleClick}>
{t("profile.setting.oauth.button.connect")}
</Button>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export interface LinkCardProps {
title: string
description: string
icon: ReactNode
type: "google" | "github"
handleClick: () => void
isConnected: boolean
hasPassword: boolean
}
4 changes: 2 additions & 2 deletions apps/agent/src/page/SettingPage/account/linked/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
TIPIS_TRACK_CLOUD_PAGE_NAME,
TipisTrack,
} from "@illa-public/track-utils"
import { MobileLinkedSetting } from "./mobile"
// import { MobileLinkedSetting } from "./mobile"
import { PCLinkedSetting } from "./pc"

const LinkedSetting: FC = () => {
Expand All @@ -31,7 +31,7 @@ const LinkedSetting: FC = () => {
</Helmet>
<LayoutAutoChange
desktopPage={<PCLinkedSetting />}
mobilePage={<MobileLinkedSetting />}
// mobilePage={<MobileLinkedSetting />}
/>
</>
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type ILinkProvider = "google" | "github"
46 changes: 24 additions & 22 deletions apps/agent/src/page/SettingPage/account/linked/mobile/index.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import Icon from "@ant-design/icons"
import { FC } from "react"
import { useTranslation } from "react-i18next"
import { GithubIcon } from "@illa-public/icon"
import { useGetUserInfoQuery } from "@illa-public/user-data"
import { LinkCard } from "../components/LinkCard"
// import Icon from "@ant-design/icons"
// import { FC } from "react"
// import { useTranslation } from "react-i18next"
// import { GithubIcon } from "@illa-public/icon"
// import { useGetUserInfoQuery } from "@illa-public/user-data"
// import { LinkCard } from "../components/LinkCard"

export const MobileLinkedSetting: FC = () => {
const { t } = useTranslation()
// export const MobileLinkedSetting: FC = () => {
// const { t } = useTranslation()

const { data } = useGetUserInfoQuery(null)
const ssoVerified = data?.ssoVerified
const hasPassword = data?.isPasswordSet
// const { data } = useGetUserInfoQuery(null)
// const ssoVerified = data?.ssoVerified
// const hasPassword = data?.isPasswordSet

return (
<LinkCard
title="Github"
description={t("profile.setting.oauth.description.GitHub_unconnect")}
icon={<Icon component={GithubIcon} />}
type="github"
isConnected={ssoVerified?.github ?? false}
hasPassword={hasPassword ?? false}
/>
)
}
// return (
// <LinkCard
// title="Github"
// description={t("profile.setting.oauth.description.GitHub_unconnect")}
// icon={<Icon component={GithubIcon} />}
// type="github"
// isConnected={ssoVerified?.github ?? false}
// hasPassword={hasPassword ?? false}
// />
// )
// }

export {}
Loading

0 comments on commit ba54c69

Please sign in to comment.