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 #204 from tipisai/feat/auth
Browse files Browse the repository at this point in the history
feat: update token storage
  • Loading branch information
Wangtaofeng authored May 13, 2024
2 parents 8b60573 + ebab99d commit 1c30eb5
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 45 deletions.
38 changes: 33 additions & 5 deletions apps/agent/src/components/Auth/AuthCheck/index.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,45 @@
import { isEqual } from "lodash-es"
import { FC } from "react"
import { Navigate } from "react-router-dom"
import { useGetAuthSessionQuery } from "@illa-public/user-data"
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 { AUTH_PAGE_PATH } from "@/router/constants"
import { BaseProtectComponentProps } from "@/router/interface"
import {
getSessionCurrentUserInfo,
setSessionCurrentUserInfo,
} from "@/utils/storage/user"

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

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

if (error && "status" in error) {
return <Navigate to="/404" />
}
if (!isLoading && !data?.session) {
return <Navigate to={AUTH_PAGE_PATH} />

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
}
}

return isSuccess ? props.children : null
Expand Down
2 changes: 1 addition & 1 deletion apps/agent/src/components/Auth/TeamCheck/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { FC } from "react"
import { Navigate, useParams } from "react-router-dom"
import { TipisTrack } from "@illa-public/track-utils"
import { useGetTeamsInfoAndCurrentIDQuery } from "@illa-public/user-data"
import { EMPTY_TEAM_PATH } from "@/router/constants"
import { BaseProtectComponentProps } from "@/router/interface"
import { EMPTY_TEAM_PATH } from "@/utils/routeHelper"
import {
getSessionCurrentTeamInfo,
setSessionCurrentTeamInfo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ 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 { PASSWORD_PATH } from "@/utils/routeHelper"
import { LinkCardProps } from "./interface"
import {
buttonWrapperStyle,
Expand Down Expand Up @@ -37,7 +37,7 @@ export const LinkCard: FC<LinkCardProps> = (props) => {
open: false,
})

navigate(PASSWORD_PATH)
navigate(SETTING_PASSWORD_PATH)
},
onCancel: () => {
tipsModal.update({
Expand Down
1 change: 1 addition & 0 deletions apps/agent/src/page/SettingPage/account/password/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { useGetUserInfoQuery } from "@illa-public/user-data"
import ChangePassword from "./components/ChangePassword"
import FirstSetPassword from "./components/FirstSetPassword"

// current not support yet
const PasswordSettingPage: FC = () => {
const { data: userInfo } = useGetUserInfoQuery(null)
const { t } = useTranslation()
Expand Down
3 changes: 2 additions & 1 deletion apps/agent/src/page/SettingPage/hooks/useLeaveTeamModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
useRemoveTeamMemberByIDMutation,
} from "@illa-public/user-data"
import store from "@/redux/store"
import { EMPTY_TEAM_PATH, getChatPath } from "@/utils/routeHelper"
import { EMPTY_TEAM_PATH } from "@/router/constants"
import { getChatPath } from "@/utils/routeHelper"
import {
removeLocalTeamIdentifier,
setLocalTeamIdentifier,
Expand Down
5 changes: 1 addition & 4 deletions apps/agent/src/page/SettingPage/layout/mobile/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import Icon from "@ant-design/icons"
import { FC } from "react"
import { useMatch, useNavigate } from "react-router-dom"
import { PreviousIcon } from "@illa-public/icon"
import { useGetCurrentTeamInfo } from "@/utils/team"
import { getChatPath } from "../../../../utils/routeHelper"
import { SettingLayoutProps } from "../interface"
import {
applyContentStyle,
Expand All @@ -16,11 +14,10 @@ const SettingMobileLayout: FC<SettingLayoutProps> = (props) => {
const { children, withoutPadding } = props
const navigate = useNavigate()
const matchMobileSettingNav = useMatch("/setting")
const currentTeamInfo = useGetCurrentTeamInfo()

const clickBackBtn = () => {
if (matchMobileSettingNav) {
navigate(getChatPath(currentTeamInfo?.identifier ?? ""))
navigate(-1)
} else {
navigate("/setting")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
useUpdateTeamPermissionConfigMutation,
} from "@illa-public/user-data"
import store from "@/redux/store"
import { EMPTY_TEAM_PATH, getChatPath } from "@/utils/routeHelper"
import { EMPTY_TEAM_PATH } from "@/router/constants"
import { getChatPath } from "@/utils/routeHelper"
import {
removeLocalTeamIdentifier,
setLocalTeamIdentifier,
Expand Down
4 changes: 3 additions & 1 deletion apps/agent/src/page/UserAuth/AuthRedirect/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Helmet } from "react-helmet-async"
import { useTranslation } from "react-i18next"
import { useNavigate, useSearchParams } from "react-router-dom"
import { useGetAuthSessionQuery } from "@illa-public/user-data"
import { setAuthToken } from "@illa-public/utils"
import TextAndLogo from "@/assets/public/textLogo.svg?react"
import { AUTH_PAGE_PATH } from "@/router/constants"
import { AUTH_ERROR, AUTH_ERROR_PARAMS_KEY } from "./constants"
Expand Down Expand Up @@ -47,7 +48,8 @@ const AuthRedirect: FC = () => {
if (isErrorRedirectRef.current) return
if (!data?.session && !isLoading) {
navigate(AUTH_PAGE_PATH)
} else {
} else if (data?.session?.access_token) {
setAuthToken(data.session.access_token)
// navigate to workspace
}
}, [data?.session, isLoading, navigate])
Expand Down
9 changes: 4 additions & 5 deletions apps/agent/src/router/buildRouter.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { LayoutAutoChange } from "@illa-public/layout-auto-change"
import { MobileForbidden } from "@illa-public/status-page"
import { RoutesObjectPro } from "./interface"
import { saveTokenToLocalStorageLoader } from "./loader/beautifyURLLoader"

export const buildRouter = (config: RoutesObjectPro[]) => {
return config.map((route) => {
Expand Down Expand Up @@ -29,10 +28,10 @@ export const buildRouter = (config: RoutesObjectPro[]) => {

newRouteItem.loader = async (args) => {
// check login
const saveTokenLoader = saveTokenToLocalStorageLoader(args)
if (saveTokenLoader) {
return saveTokenLoader
}
// const saveTokenLoader = saveTokenToLocalStorageLoader(args)
// if (saveTokenLoader) {
// return saveTokenLoader
// }

return typeof originLoader === "function"
? originLoader?.(args)
Expand Down
50 changes: 29 additions & 21 deletions apps/agent/src/router/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,17 @@ import {
CREATE_TIPI_TEMPLATE_PATH,
EDIT_FUNCTION_TEMPLATE_PATH,
EDIT_TIPI_TEMPLATE_PATH,
EMPTY_TEAM_PATH,
FUNCTIONS_DASHBOARD_TEMPLATE_PATH,
MARKETPLACE_DETAIL_PATH,
RUN_TIPI_TEMPLATE_PATH,
SETTING_ACCOUNT_PATH,
SETTING_BILLING_PATH,
SETTING_LANGUAGE_PATH,
SETTING_LINKED_PATH,
SETTING_MEMBERS_PATH,
SETTING_PATH,
SETTING_TEAM_INFO_PATH,
TEAM_IDENTIFIER_TEMPLATE_PATH,
TIPIS_DASHBOARD_TEMPLATE_PATH,
TIPI_DETAIL_TEMPLATE_PATH,
Expand Down Expand Up @@ -65,9 +73,9 @@ const PersonalSetting = lazy(
const LanguageSetting = lazy(
() => import("@/page/SettingPage/account/language"),
)
const PasswordSettingPage = lazy(
() => import("@/page/SettingPage/account/password"),
)
// const PasswordSettingPage = lazy(
// () => import("@/page/SettingPage/account/password"),
// )
const LinkedSettingPage = lazy(
() => import("@/page/SettingPage/account/linked"),
)
Expand Down Expand Up @@ -134,7 +142,7 @@ const ILLA_ROUTE_CONFIG: RoutesObjectPro[] = [
),
},
{
path: "/empty-workspace",
path: EMPTY_TEAM_PATH,
ProtectComponent: AuthCheck,
accessByMobile: true,
element: <WorkspaceLayout />,
Expand Down Expand Up @@ -253,7 +261,7 @@ const ILLA_ROUTE_CONFIG: RoutesObjectPro[] = [
},

{
path: "/setting",
path: SETTING_PATH,
accessByMobile: true,
ProtectComponent: AuthCheck,
element: <SettingLayout />,
Expand All @@ -268,7 +276,7 @@ const ILLA_ROUTE_CONFIG: RoutesObjectPro[] = [
accessByMobile: true,
},
{
path: "account",
path: SETTING_ACCOUNT_PATH,
element: (
<Suspense fallback={<FullSectionLoading />}>
<PersonalSetting />
Expand All @@ -277,25 +285,25 @@ const ILLA_ROUTE_CONFIG: RoutesObjectPro[] = [
accessByMobile: true,
},
{
path: "language",
path: SETTING_LANGUAGE_PATH,
element: (
<Suspense fallback={<FullSectionLoading />}>
<LanguageSetting />
</Suspense>
),
accessByMobile: true,
},
// {
// path: SETTING_PASSWORD_PATH,
// element: (
// <Suspense fallback={<FullSectionLoading />}>
// <PasswordSettingPage />
// </Suspense>
// ),
// accessByMobile: true,
// },
{
path: "password",
element: (
<Suspense fallback={<FullSectionLoading />}>
<PasswordSettingPage />
</Suspense>
),
accessByMobile: true,
},
{
path: "linked",
path: SETTING_LINKED_PATH,
element: (
<Suspense fallback={<FullSectionLoading />}>
<LinkedSettingPage />
Expand All @@ -307,13 +315,13 @@ const ILLA_ROUTE_CONFIG: RoutesObjectPro[] = [
},

{
path: "/setting/:teamIdentifier",
path: `${SETTING_PATH}/${TEAM_IDENTIFIER_TEMPLATE_PATH}`,
accessByMobile: true,
element: <SettingLayout />,
ProtectComponent: TeamAndLoginCheck,
children: [
{
path: "team-settings",
path: SETTING_TEAM_INFO_PATH,
element: (
<Suspense fallback={<FullSectionLoading />}>
<TeamSetting />
Expand All @@ -322,7 +330,7 @@ const ILLA_ROUTE_CONFIG: RoutesObjectPro[] = [
accessByMobile: true,
},
{
path: "members",
path: SETTING_MEMBERS_PATH,
element: (
<Suspense fallback={<FullSectionLoading />}>
<TeamMembers />
Expand All @@ -331,7 +339,7 @@ const ILLA_ROUTE_CONFIG: RoutesObjectPro[] = [
accessByMobile: true,
},
{
path: "billing",
path: SETTING_BILLING_PATH,
element: (
<Suspense fallback={<FullSectionLoading />}>
<TeamBilling />
Expand Down
9 changes: 9 additions & 0 deletions apps/agent/src/router/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,12 @@ export const CREATE_FUNCTION_TEMPLATE_PATH = `${TEAM_FUNCTION_TEMPLATE_PATH}/cre

export const AUTH_PAGE_PATH = "/auth"
export const AUTH_REDIRECT_PATH = "/authRedirect"
export const EMPTY_TEAM_PATH = "/empty-workspace"
export const SETTING_PATH = "/setting"
export const SETTING_ACCOUNT_PATH = "account"
export const SETTING_LANGUAGE_PATH = "language"
export const SETTING_PASSWORD_PATH = "password"
export const SETTING_LINKED_PATH = "linked"
export const SETTING_TEAM_INFO_PATH = "team-settings"
export const SETTING_MEMBERS_PATH = "members"
export const SETTING_BILLING_PATH = "billing"
2 changes: 1 addition & 1 deletion apps/agent/src/utils/routeHelper/hook.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useCallback } from "react"
import { useNavigate } from "react-router-dom"
import { useLazyGetTeamsInfoQuery } from "@illa-public/user-data"
import { EMPTY_TEAM_PATH } from "@/router/constants"
import {
CREATE_FUNCTION_FROM_SINGLE,
CREATE_FUNCTION_FROM_SINGLE_KEY,
CREATE_FUNCTION_FROM_TAB_KEY,
EMPTY_TEAM_PATH,
getChatPath,
getCreateFunctionPath,
getCreateTipiPath,
Expand Down
3 changes: 0 additions & 3 deletions apps/agent/src/utils/routeHelper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,6 @@ export const genTabNavigateLink = (
}
}

export const PASSWORD_PATH = "/setting/password"
export const EMPTY_TEAM_PATH = "/empty-workspace"

export enum CREATE_FUNCTION_FROM_SINGLE {
CREATE_TIPIS = "createTipis",
EDIT_TIPIS = "editTipis",
Expand Down

0 comments on commit 1c30eb5

Please sign in to comment.