Skip to content

Commit 964513b

Browse files
committed
登录结合OpenWrite
1 parent 32a113f commit 964513b

File tree

18 files changed

+244
-24
lines changed

18 files changed

+244
-24
lines changed

components/OpenWrite.js

+19-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { siteConfig } from '@/lib/config'
2+
import { useGlobal } from '@/lib/global'
23
import { isBrowser, loadExternalResource } from '@/lib/utils'
34
import { useRouter } from 'next/router'
45
import { useEffect } from 'react'
@@ -24,14 +25,10 @@ const OpenWrite = () => {
2425
// 白名单
2526
const whiteList = siteConfig('OPEN_WRITE_WHITE_LIST', '')
2627

27-
const loadOpenWrite = async () => {
28-
const existWhite = existedWhiteList(router.asPath, whiteList)
29-
30-
// 如果当前页面在白名单中,则屏蔽加锁
31-
if (existWhite) {
32-
return
33-
}
28+
// 登录信息
29+
const { isLoaded, isSignedIn } = useGlobal()
3430

31+
const loadOpenWrite = async () => {
3532
try {
3633
await loadExternalResource(
3734
'https://readmore.openwrite.cn/js/readmore-2.0.js',
@@ -74,11 +71,24 @@ const OpenWrite = () => {
7471
}
7572
}
7673
useEffect(() => {
74+
const existWhite = existedWhiteList(router.asPath, whiteList)
75+
// 白名单中,免检
76+
if (existWhite) {
77+
return
78+
}
79+
if (isSignedIn) {
80+
// 用户已登录免检
81+
console.log('用户已登录')
82+
return
83+
}
84+
85+
// 开发环境免检
7786
if (process.env.NODE_ENV === 'development') {
7887
console.log('开发环境:屏蔽OpenWrite')
7988
return
8089
}
81-
if (isBrowser && blogId) {
90+
91+
if (isBrowser && blogId && !isSignedIn) {
8292
toggleTocItems(true) // 禁止目录项的点击
8393

8494
// Check if the element with id 'read-more-wrap' already exists
@@ -89,7 +99,7 @@ const OpenWrite = () => {
8999
loadOpenWrite()
90100
}
91101
}
92-
})
102+
}, [isLoaded, router])
93103

94104
// 启动一个监听器,当页面上存在#read-more-wrap对象时,所有的 a .notion-table-of-contents-item 对象都禁止点击
95105

lib/global.js

+14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
initDarkMode,
66
saveDarkModeToLocalStorage
77
} from '@/themes/theme'
8+
import { useUser } from '@clerk/nextjs'
89
import { useRouter } from 'next/router'
910
import { createContext, useContext, useEffect, useState } from 'react'
1011
import {
@@ -13,6 +14,10 @@ import {
1314
redirectUserLang,
1415
saveLangToLocalStorage
1516
} from './lang'
17+
18+
/**
19+
* 全局上下文
20+
*/
1621
const GlobalContext = createContext()
1722

1823
export function GlobalContextProvider(props) {
@@ -37,6 +42,12 @@ export function GlobalContextProvider(props) {
3742
const [onLoading, setOnLoading] = useState(true) // 抓取文章数据
3843
const router = useRouter()
3944

45+
// 登录验证相关
46+
const enableClerk = process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY
47+
const { isLoaded, isSignedIn, user } = enableClerk
48+
? useUser()
49+
: { isLoaded: true, isSignedIn: false, user: false }
50+
4051
// 是否全屏
4152
const fullWidth = post?.fullWidth ?? false
4253

@@ -119,6 +130,9 @@ export function GlobalContextProvider(props) {
119130
return (
120131
<GlobalContext.Provider
121132
value={{
133+
isLoaded,
134+
isSignedIn,
135+
user,
122136
fullWidth,
123137
NOTION_CONFIG,
124138
THEME_CONFIG,

lib/lang/en-US.js

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ export default {
2121
},
2222
COMMON: {
2323
THEME: 'Theme',
24+
SIGN_IN: 'Sign In',
25+
SIGN_OUT: 'Sign Out',
2426
ARTICLE_LIST: 'Article List',
2527
RECOMMEND_POSTS: 'Recommend Posts',
2628
MORE: 'More',

lib/lang/zh-CN.js

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ export default {
2121
},
2222
COMMON: {
2323
THEME: 'Theme',
24+
SIGN_IN: '登录',
25+
SIGN_OUT: '登出',
2426
ARTICLE_LIST: '文章列表',
2527
RECOMMEND_POSTS: '推荐文章',
2628
MORE: '更多',

next.config.js

+2
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ const nextConfig = {
194194
},
195195
webpack: (config, { dev, isServer }) => {
196196
// 动态主题:添加 resolve.alias 配置,将动态路径映射到实际路径
197+
config.resolve.alias['@'] = path.resolve(__dirname)
198+
197199
if (!isServer) {
198200
console.log('[默认主题]', path.resolve(__dirname, 'themes', THEME))
199201
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"build-all-in-dev": "cross-env VERCEL_ENV=production next build"
2323
},
2424
"dependencies": {
25+
"@clerk/localizations": "^3.0.4",
2526
"@clerk/nextjs": "^5.1.5",
2627
"@headlessui/react": "^1.7.15",
2728
"@next/bundle-analyzer": "^12.1.1",

pages/_app.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import { getQueryParam } from '../lib/utils'
1717
import BLOG from '@/blog.config'
1818
import ExternalPlugins from '@/components/ExternalPlugins'
1919
import GlobalHead from '@/components/GlobalHead'
20+
import { zhCN } from '@clerk/localizations'
2021
import { ClerkProvider } from '@clerk/nextjs'
21-
2222
/**
2323
* App挂载DOM 入口文件
2424
* @param {*} param0
@@ -57,7 +57,15 @@ const MyApp = ({ Component, pageProps }) => {
5757
<ExternalPlugins {...pageProps} />
5858
</GlobalContextProvider>
5959
)
60-
return <>{enableClerk ? <ClerkProvider>{content}</ClerkProvider> : content}</>
60+
return (
61+
<>
62+
{enableClerk ? (
63+
<ClerkProvider localization={zhCN}>{content}</ClerkProvider>
64+
) : (
65+
content
66+
)}
67+
</>
68+
)
6169
}
6270

6371
export default MyApp

pages/sign-in/[[...index]].js

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// import BLOG from '@/blog.config'
21
import BLOG from '@/blog.config'
32
import { siteConfig } from '@/lib/config'
43
import { getGlobalData } from '@/lib/db/getSiteData'

themes/gitbook/components/ArticleInfo.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ export default function ArticleInfo({ post }) {
1010
return (
1111
<div className='pt-10 pb-6 text-gray-400 text-sm'>
1212
<i className='fa-regular fa-clock mr-1' />
13-
Last update: {post.date?.start_date}
13+
Last update:{' '}
14+
{post.date?.start_date || post?.publishDay || post?.lastEditedDay}
1415
</div>
1516
)
1617
}

themes/gitbook/components/Header.js

+16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Collapse from '@/components/Collapse'
22
import DarkModeButton from '@/components/DarkModeButton'
33
import { siteConfig } from '@/lib/config'
44
import { useGlobal } from '@/lib/global'
5+
import { SignInButton, SignedOut, UserButton } from '@clerk/nextjs'
56
import { useRef, useState } from 'react'
67
import CONFIG from '../config'
78
import LogoBar from './LogoBar'
@@ -59,6 +60,8 @@ export default function Header(props) {
5960
links = customMenu
6061
}
6162

63+
const enableClerk = process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY
64+
6265
return (
6366
<div id='top-nav' className={'fixed top-0 w-full z-20 ' + className}>
6467
{/* PC端菜单 */}
@@ -79,6 +82,19 @@ export default function Header(props) {
7982

8083
{/* 右侧 */}
8184
<div className='flex items-center gap-4'>
85+
{/* 登录相关 */}
86+
{enableClerk && (
87+
<>
88+
<SignedOut>
89+
<SignInButton mode='modal'>
90+
<button className='bg-green-500 hover:bg-green-600 text-white rounded-lg px-3 py-2'>
91+
{locale.COMMON.SIGN_IN}
92+
</button>
93+
</SignInButton>
94+
</SignedOut>
95+
<UserButton />
96+
</>
97+
)}
8298
<DarkModeButton className='text-sm items-center h-full hidden md:flex' />
8399
<SearchInput className='hidden md:flex md:w-52 lg:w-72' />
84100
{/* 折叠按钮、仅移动端显示 */}

themes/gitbook/index.js

+56-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { siteConfig } from '@/lib/config'
1111
import { useGlobal } from '@/lib/global'
1212
import { isBrowser } from '@/lib/utils'
1313
import { getShortId } from '@/lib/utils/pageId'
14+
import { SignIn, SignUp } from '@clerk/nextjs'
1415
import dynamic from 'next/dynamic'
1516
import Link from 'next/link'
1617
import { useRouter } from 'next/router'
@@ -164,7 +165,7 @@ const LayoutBase = props => {
164165
{/* 中间内容区域 */}
165166
<div
166167
id='center-wrapper'
167-
className='dark:bg-hexo-black-gray flex flex-col justify-between w-full relative z-10 pt-14 min-h-screen'>
168+
className='flex flex-col justify-between w-full relative z-10 pt-14 min-h-screen'>
168169
<div
169170
id='container-inner'
170171
className={`w-full ${fullWidth ? 'px-5' : 'max-w-3xl px-3 lg:px-0'} justify-center mx-auto`}>
@@ -473,6 +474,58 @@ const LayoutTagIndex = props => {
473474
)
474475
}
475476

477+
/**
478+
* 登录页面
479+
* @param {*} props
480+
* @returns
481+
*/
482+
const LayoutSignIn = props => {
483+
const { post } = props
484+
const enableClerk = process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY
485+
486+
return (
487+
<>
488+
<div className='grow mt-20'>
489+
{/* clerk预置表单 */}
490+
{enableClerk && (
491+
<div className='flex justify-center py-6'>
492+
<SignIn />
493+
</div>
494+
)}
495+
<div id='article-wrapper'>
496+
<NotionPage post={post} />
497+
</div>
498+
</div>
499+
</>
500+
)
501+
}
502+
503+
/**
504+
* 注册页面
505+
* @param {*} props
506+
* @returns
507+
*/
508+
const LayoutSignUp = props => {
509+
const { post } = props
510+
const enableClerk = process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY
511+
512+
return (
513+
<>
514+
<div className='grow mt-20'>
515+
{/* clerk预置表单 */}
516+
{enableClerk && (
517+
<div className='flex justify-center py-6'>
518+
<SignUp />
519+
</div>
520+
)}
521+
<div id='article-wrapper'>
522+
<NotionPage post={post} />
523+
</div>
524+
</div>
525+
</>
526+
)
527+
}
528+
476529
export {
477530
Layout404,
478531
LayoutArchive,
@@ -481,6 +534,8 @@ export {
481534
LayoutIndex,
482535
LayoutPostList,
483536
LayoutSearch,
537+
LayoutSignIn,
538+
LayoutSignUp,
484539
LayoutSlug,
485540
LayoutTagIndex,
486541
CONFIG as THEME_CONFIG

themes/magzine/components/Catalog.js

-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ const Catalog = ({ post, toc, className }) => {
2323
actionSectionScrollSpy()
2424
window.addEventListener('scroll', actionSectionScrollSpy)
2525
}
26-
setTimeout(() => {
27-
console.log('目录', post, toc)
28-
}, 1000)
2926
return () => {
3027
window.removeEventListener('scroll', actionSectionScrollSpy)
3128
}

themes/magzine/components/Header.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Collapse from '@/components/Collapse'
22
import { siteConfig } from '@/lib/config'
33
import { useGlobal } from '@/lib/global'
4+
import { SignInButton, SignedOut, UserButton } from '@clerk/nextjs'
45
import throttle from 'lodash.throttle'
56
import { useRouter } from 'next/router'
67
import { useEffect, useRef, useState } from 'react'
@@ -112,6 +113,8 @@ export default function Header(props) {
112113
return null
113114
}
114115

116+
const enableClerk = process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY
117+
115118
return (
116119
<div
117120
id='top-navbar-wrapper'
@@ -154,7 +157,7 @@ export default function Header(props) {
154157
</>
155158
)}
156159

157-
{/* 右侧移动端折叠按钮 */}
160+
{/* 右侧按钮 */}
158161
<div className='flex items-center gap-x-2 pr-2'>
159162
{/* 搜索按钮 */}
160163
<div
@@ -167,6 +170,8 @@ export default function Header(props) {
167170
: 'fa-solid fa-magnifying-glass' + ' align-middle'
168171
}></i>
169172
</div>
173+
174+
{/* 移动端显示开关 */}
170175
<div className='mr-1 flex md:hidden justify-end items-center text-lg space-x-4 font-serif dark:text-gray-200'>
171176
<div onClick={toggleMenuOpen} className='cursor-pointer p-2'>
172177
{isOpen ? (
@@ -176,6 +181,20 @@ export default function Header(props) {
176181
)}
177182
</div>
178183
</div>
184+
185+
{/* 登录相关 */}
186+
{enableClerk && (
187+
<>
188+
<SignedOut>
189+
<SignInButton mode='modal'>
190+
<button className='bg-gray-800 hover:bg-gray-900 text-white rounded-lg px-3 py-2'>
191+
{locale.COMMON.SIGN_IN}
192+
</button>
193+
</SignInButton>
194+
</SignedOut>
195+
<UserButton />
196+
</>
197+
)}
179198
</div>
180199
</div>
181200

0 commit comments

Comments
 (0)