-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: neil <[email protected]>
- Loading branch information
Showing
32 changed files
with
410 additions
and
184 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
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 |
---|---|---|
|
@@ -52,6 +52,7 @@ | |
"signout": "退出", | ||
"btn": { | ||
"save": "保存", | ||
"cancel": "取消" | ||
"cancel": "取消", | ||
"func_disabled": "该功能临时关闭" | ||
} | ||
} |
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
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,58 @@ | ||
import React, { PropsWithChildren } from 'react'; | ||
import router from 'next/router'; | ||
import { useSnapshot } from 'valtio'; | ||
import { userInfoStore } from './UserInfoStore'; | ||
|
||
interface Props { | ||
className?: string; | ||
loadingUi?: React.ReactNode; | ||
redirectTo?: string; | ||
} | ||
|
||
const AuthRequire: React.FC<PropsWithChildren<Props>> = ({ | ||
children, | ||
className, | ||
loadingUi, | ||
redirectTo, | ||
}) => { | ||
const { currentUser, loading } = useSnapshot(userInfoStore); | ||
|
||
if (!loading && !currentUser) { | ||
let redirectUrl = redirectTo ?? window.location.pathname; | ||
router.replace( | ||
`/auth/signin?redirect_to=${encodeURIComponent(redirectUrl)}` | ||
); | ||
} | ||
|
||
if (loading && loadingUi) { | ||
return <>{loadingUi}</>; | ||
} | ||
|
||
if (loading) { | ||
return ( | ||
<div className={className}> | ||
<div className="flex-1 space-y-4"> | ||
<div className="h-6 rounded bg-slate-200"></div> | ||
|
||
<div className="grid grid-cols-3 gap-4"> | ||
<div className="col-span-2 h-6 rounded bg-slate-200"></div> | ||
<div className="col-span-1 h-6 rounded bg-slate-200"></div> | ||
</div> | ||
|
||
<div className="h-6 rounded bg-slate-200"></div> | ||
|
||
<div className="grid grid-cols-3 gap-4"> | ||
<div className="col-span-1 h-6 rounded bg-slate-200"></div> | ||
<div className="col-span-2 h-6 rounded bg-slate-200"></div> | ||
</div> | ||
|
||
<div className="h-6 rounded bg-slate-200"></div> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
return <>{children}</>; | ||
}; | ||
|
||
export default AuthRequire; |
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 was deleted.
Oops, something went wrong.
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,64 @@ | ||
import React, { useEffect } from 'react'; | ||
import { useSnapshot } from 'valtio'; | ||
import { useToggle } from 'ahooks'; | ||
import { UserinfoQuery } from '@graphql/generated'; | ||
import { getAuthProvider, setAuthProvider } from '@common/utils/cookie'; | ||
import { userInfoStore } from '@modules/auth/UserInfoStore'; | ||
import { ReadonlyDeep } from 'type-fest'; | ||
|
||
type LoginBinds = ReadonlyDeep< | ||
NonNullable<UserinfoQuery['currentUser']>['loginBinds'] | ||
>; | ||
|
||
function findSpecifyProvider({ | ||
loginBinds, | ||
provider, | ||
}: { | ||
loginBinds?: LoginBinds; | ||
provider?: string; | ||
}) { | ||
let providerUser; | ||
|
||
if (provider && loginBinds && loginBinds.length > 1) { | ||
providerUser = loginBinds?.find( | ||
(bindInfo) => bindInfo.provider === provider | ||
); | ||
} else { | ||
providerUser = loginBinds?.[0]; | ||
} | ||
|
||
if (providerUser) { | ||
providerUser = { | ||
...providerUser, | ||
// todo Let the backend modify | ||
// The naming of the returned fields in the interface data is reversed. | ||
account: providerUser?.nickname, | ||
nickname: providerUser?.account, | ||
}; | ||
} | ||
|
||
return providerUser || null; | ||
} | ||
|
||
const toggleProviders = ['github', 'gitee']; | ||
const getAnother = (p?: string) => toggleProviders.filter((i) => i !== p)[0]; | ||
|
||
const useProviderInfo = () => { | ||
const { currentUser: user } = useSnapshot(userInfoStore); | ||
|
||
const login = getAuthProvider() || 'github'; | ||
const [provider, { toggle }] = useToggle(login, getAnother(login)); | ||
|
||
useEffect(() => { | ||
setAuthProvider(provider); | ||
}, [provider]); | ||
|
||
const showUser = findSpecifyProvider({ | ||
provider: provider, | ||
loginBinds: user?.loginBinds, | ||
}); | ||
|
||
return { providerUser: showUser, loginBinds: user?.loginBinds, toggle }; | ||
}; | ||
|
||
export default useProviderInfo; |
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.