Feature/auth guard update (#230)

- add AuthGuard on myFeed
- make AuthGuard closable with redirect to MainPage
This commit is contained in:
Ilya Y 2023-09-24 17:55:46 +03:00 committed by GitHub
parent bfc6599149
commit 8a71cb41ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 182 additions and 166 deletions

View File

@ -60,7 +60,6 @@ export const FullArticle = (props: Props) => {
}, 'bookmark') }, 'bookmark')
} }
console.log('!!! props.article.media:', props.article.media)
const body = createMemo(() => { const body = createMemo(() => {
if (props.article.layout === 'literature') { if (props.article.layout === 'literature') {
try { try {

View File

@ -4,11 +4,16 @@ import { hideModal, showModal } from '../../stores/ui'
type Props = { type Props = {
children: JSX.Element children: JSX.Element
disabled?: boolean
} }
export const AuthGuard = (props: Props) => { export const AuthGuard = (props: Props) => {
const { isAuthenticated, isSessionLoaded } = useSession() const { isAuthenticated, isSessionLoaded } = useSession()
createEffect(() => { createEffect(() => {
if (props.disabled) {
return
}
if (isSessionLoaded()) { if (isSessionLoaded()) {
if (isAuthenticated()) { if (isAuthenticated()) {
hideModal() hideModal()
@ -18,5 +23,5 @@ export const AuthGuard = (props: Props) => {
} }
}) })
return <Show when={isSessionLoaded() && isAuthenticated()}>{props.children}</Show> return <Show when={(isSessionLoaded() && isAuthenticated()) || props.disabled}>{props.children}</Show>
} }

View File

@ -5,6 +5,8 @@ import { hideModal, useModalStore } from '../../../stores/ui'
import { useEscKeyDownHandler } from '../../../utils/useEscKeyDownHandler' import { useEscKeyDownHandler } from '../../../utils/useEscKeyDownHandler'
import styles from './Modal.module.scss' import styles from './Modal.module.scss'
import { redirectPage } from '@nanostores/router'
import { router } from '../../../stores/router'
interface Props { interface Props {
name: string name: string
@ -22,9 +24,11 @@ export const Modal = (props: Props) => {
const allowClose = createMemo(() => props.allowClose !== false) const allowClose = createMemo(() => props.allowClose !== false)
const handleHide = () => { const handleHide = () => {
if (modal() && allowClose()) { if (modal() && allowClose()) {
hideModal()
props.onClose && props.onClose() props.onClose && props.onClose()
} else {
redirectPage(router, 'home')
} }
hideModal()
} }
useEscKeyDownHandler(handleHide) useEscKeyDownHandler(handleHide)
@ -45,7 +49,6 @@ export const Modal = (props: Props) => {
onClick={(event) => event.stopPropagation()} onClick={(event) => event.stopPropagation()}
> >
{props.children} {props.children}
<Show when={allowClose()}>
<div class={styles.close} onClick={handleHide}> <div class={styles.close} onClick={handleHide}>
<svg <svg
class={styles.icon} class={styles.icon}
@ -60,7 +63,6 @@ export const Modal = (props: Props) => {
/> />
</svg> </svg>
</div> </div>
</Show>
</div> </div>
</div> </div>
</Show> </Show>

View File

@ -18,6 +18,9 @@ import stylesTopic from '../Feed/CardTopic.module.scss'
import stylesBeside from '../../components/Feed/Beside.module.scss' import stylesBeside from '../../components/Feed/Beside.module.scss'
import { CommentDate } from '../Article/CommentDate' import { CommentDate } from '../Article/CommentDate'
import { Loading } from '../_shared/Loading' import { Loading } from '../_shared/Loading'
import { ConditionalWrapper } from '../_shared/ConditionalWrapper'
import { AuthGuard } from '../AuthGuard'
import { useSession } from '../../context/session'
export const FEED_PAGE_SIZE = 20 export const FEED_PAGE_SIZE = 20
@ -37,9 +40,17 @@ const getOrderBy = (by: FeedSearchParams['by']) => {
return '' return ''
} }
const routesWithAuthGuard = new Set([
'feedMy',
'feedNotifications',
'feedBookmarks',
'feedCollaborations',
'feedDiscussions'
])
export const FeedView = () => { export const FeedView = () => {
const { t } = useLocalize() const { t } = useLocalize()
const { page, searchParams } = useRouter<FeedSearchParams>() const { page, searchParams } = useRouter<FeedSearchParams>()
const { isAuthenticated } = useSession()
const [isLoading, setIsLoading] = createSignal(false) const [isLoading, setIsLoading] = createSignal(false)
// state // state
@ -69,7 +80,6 @@ export const FeedView = () => {
{ defer: true } { defer: true }
) )
) )
const loadFeedShouts = () => { const loadFeedShouts = () => {
const options: LoadShoutsOptions = { const options: LoadShoutsOptions = {
limit: FEED_PAGE_SIZE, limit: FEED_PAGE_SIZE,
@ -82,7 +92,7 @@ export const FeedView = () => {
options.order_by = orderBy options.order_by = orderBy
} }
if (page().route === 'feedMy') { if (routesWithAuthGuard.has(page().route) && isAuthenticated()) {
return loadMyFeed(options) return loadMyFeed(options)
} }
@ -116,6 +126,7 @@ export const FeedView = () => {
return ( return (
<div> <div>
<AuthGuard disabled={!routesWithAuthGuard.has(page().route)}>
<div class="wide-container feed"> <div class="wide-container feed">
<div class="row"> <div class="row">
<div class={clsx('col-md-5 col-xl-4', styles.feedNavigation)}> <div class={clsx('col-md-5 col-xl-4', styles.feedNavigation)}>
@ -265,6 +276,7 @@ export const FeedView = () => {
</aside> </aside>
</div> </div>
</div> </div>
</AuthGuard>
</div> </div>
) )
} }

View File

@ -39,8 +39,6 @@ export const GrowingTextarea = (props: Props) => {
} }
} }
console.log('!!! initialValue:', props.initialValue)
return ( return (
<div <div
class={clsx(styles.GrowingTextarea, { class={clsx(styles.GrowingTextarea, {