Update lurker corner to switch back to session
This commit is contained in:
parent
24168f556e
commit
b8243f8a87
|
@ -28,7 +28,7 @@ import { clearNotifications } from '../lib/badge'
|
||||||
import { useServiceWorker } from './serviceworker'
|
import { useServiceWorker } from './serviceworker'
|
||||||
import SubSelect from './sub-select'
|
import SubSelect from './sub-select'
|
||||||
import { useShowModal } from './modal'
|
import { useShowModal } from './modal'
|
||||||
import SwitchAccountDialog from './switch-account'
|
import SwitchAccountDialog, { useAccounts } from './switch-account'
|
||||||
|
|
||||||
function WalletSummary ({ me }) {
|
function WalletSummary ({ me }) {
|
||||||
if (!me) return null
|
if (!me) return null
|
||||||
|
@ -177,6 +177,8 @@ function StackerCorner ({ dropNavKey }) {
|
||||||
function LurkerCorner ({ path }) {
|
function LurkerCorner ({ path }) {
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const strike = useLightning()
|
const strike = useLightning()
|
||||||
|
const { isAnon } = useAccounts()
|
||||||
|
const showModal = useShowModal()
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!window.localStorage.getItem('striked')) {
|
if (!window.localStorage.getItem('striked')) {
|
||||||
|
@ -193,6 +195,23 @@ function LurkerCorner ({ path }) {
|
||||||
query: { callbackUrl: window.location.origin + router.asPath }
|
query: { callbackUrl: window.location.origin + router.asPath }
|
||||||
}), [router])
|
}), [router])
|
||||||
|
|
||||||
|
if (isAnon) {
|
||||||
|
return (
|
||||||
|
<div className='d-flex ms-auto'>
|
||||||
|
<Dropdown className={styles.dropdown} align='end'>
|
||||||
|
<Dropdown.Toggle className='nav-link nav-item' id='profile' variant='custom'>
|
||||||
|
<Nav.Link eventKey='anon' as='span' className='p-0'>
|
||||||
|
<AnonIcon className='me-1 fill-muted' width={20} height={20} />@anon
|
||||||
|
</Nav.Link>
|
||||||
|
</Dropdown.Toggle>
|
||||||
|
<Dropdown.Menu>
|
||||||
|
<Dropdown.Item onClick={() => showModal(onClose => <SwitchAccountDialog onClose={onClose} />)}>switch account</Dropdown.Item>
|
||||||
|
</Dropdown.Menu>
|
||||||
|
</Dropdown>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
return path !== '/login' && path !== '/signup' && !path.startsWith('/invites') &&
|
return path !== '/login' && path !== '/signup' && !path.startsWith('/invites') &&
|
||||||
<div className='ms-auto'>
|
<div className='ms-auto'>
|
||||||
<Button
|
<Button
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { createContext, useCallback, useContext, useEffect, useState } from 'react'
|
import { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react'
|
||||||
import AnonIcon from '../svgs/spy-fill.svg'
|
import AnonIcon from '../svgs/spy-fill.svg'
|
||||||
import { useRouter } from 'next/router'
|
import { useRouter } from 'next/router'
|
||||||
import cookie from 'cookie'
|
import cookie from 'cookie'
|
||||||
|
@ -34,10 +34,16 @@ export const AccountProvider = ({ children }) => {
|
||||||
setAccounts(accounts => accounts.filter(({ id }) => id !== userId))
|
setAccounts(accounts => accounts.filter(({ id }) => id !== userId))
|
||||||
}, [setAccounts])
|
}, [setAccounts])
|
||||||
|
|
||||||
return <AccountContext.Provider value={{ accounts, addAccount, removeAccount }}>{children}</AccountContext.Provider>
|
const isAnon = useMemo(() => {
|
||||||
|
const { 'multi_auth.user-id': multiAuthUserIdCookie } = cookie.parse(document.cookie)
|
||||||
|
if (!multiAuthUserIdCookie) return false
|
||||||
|
return multiAuthUserIdCookie === 'anonymous'
|
||||||
|
}, [document.cookie])
|
||||||
|
|
||||||
|
return <AccountContext.Provider value={{ accounts, addAccount, removeAccount, isAnon }}>{children}</AccountContext.Provider>
|
||||||
}
|
}
|
||||||
|
|
||||||
const useAccounts = () => useContext(AccountContext)
|
export const useAccounts = () => useContext(AccountContext)
|
||||||
|
|
||||||
const AnonAccount = () => {
|
const AnonAccount = () => {
|
||||||
const me = useMe()
|
const me = useMe()
|
||||||
|
|
|
@ -19,9 +19,14 @@ const multiAuthMiddleware = (request) => {
|
||||||
const sessionCookieName = '__Secure-next-auth.session-token'
|
const sessionCookieName = '__Secure-next-auth.session-token'
|
||||||
const hasSession = request.cookies?.has(sessionCookieName)
|
const hasSession = request.cookies?.has(sessionCookieName)
|
||||||
if (userId && hasSession) {
|
if (userId && hasSession) {
|
||||||
|
if (userId === 'anonymous') {
|
||||||
|
// user switched to anon
|
||||||
|
request.cookies.delete(sessionCookieName)
|
||||||
|
} else {
|
||||||
const userJWT = request.cookies.get(`multi_auth.${userId}`)?.value
|
const userJWT = request.cookies.get(`multi_auth.${userId}`)?.value
|
||||||
if (userJWT) request.cookies.set(sessionCookieName, userJWT)
|
if (userJWT) request.cookies.set(sessionCookieName, userJWT)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
const response = NextResponse.next({ request })
|
const response = NextResponse.next({ request })
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue