1
0
mirror of synced 2026-02-03 18:01:02 -05:00

Compare commits

...

6 Commits

Author SHA1 Message Date
Brandon Bayer
0816950fb6 work with no JS 2021-02-09 19:29:27 -05:00
Brandon Bayer
57c70795e8 more cleanup 2021-02-09 19:04:47 -05:00
Brandon Bayer
12785484c0 cleanup 2021-02-09 19:02:47 -05:00
Brandon Bayer
2b65ce2206 refactor 2021-02-09 19:01:57 -05:00
Brandon Bayer
ad58be3f52 Merge branch 'canary' into use-authorize 2021-02-09 18:53:55 -05:00
Brandon Bayer
f9dca5c60a wip 2021-02-08 19:49:56 -05:00
6 changed files with 65 additions and 8 deletions

View File

@@ -1,9 +1,10 @@
import React from "react"
import {useRouter, BlitzPage} from "blitz"
import {useRouter, BlitzPage, useRedirectAuthenticatedUser} from "blitz"
import Layout from "app/core/layouts/Layout"
import {LoginForm} from "app/auth/components/LoginForm"
const LoginPage: BlitzPage = () => {
useRedirectAuthenticatedUser("/projects")
const router = useRouter()
return (

View File

@@ -1,5 +1,4 @@
import {
withBlitzAppRoot,
AppProps,
ErrorComponent,
useRouter,

View File

@@ -1,5 +1,5 @@
import {Suspense} from "react"
import {Head, Link, usePaginatedQuery, useRouter, BlitzPage} from "blitz"
import {Head, Link, usePaginatedQuery, useRouter, BlitzPage, useAuthorize} from "blitz"
import Layout from "app/core/layouts/Layout"
import getProjects from "app/projects/queries/getProjects"
@@ -40,6 +40,8 @@ export const ProjectsList = () => {
}
const ProjectsPage: BlitzPage = () => {
useAuthorize()
return (
<>
<Head>

View File

@@ -1,10 +1,46 @@
import React from "react"
import {AppProps} from "."
import React, {useEffect} from "react"
import {AppProps, Head} from "."
const customCSS = `
body::before {
content: "";
display: block;
position: fixed;
width: 100%;
height: 100%;
top: 0;
left: 0;
z-index: 99999;
background-color: white;
}
.blitz-first-render-complete body::before {
display: none;
}
`
const noscriptCSS = `
body::before {
content: none
}
`
export function withBlitzAppRoot(WrappedComponent: React.ComponentType<any>) {
const BlitzAppRoot = (props: AppProps) => {
// props comes afterwards so the can override the default ones.
return <WrappedComponent {...(props as any)} />
useEffect(() => {
document.documentElement.classList.add("blitz-first-render-complete")
}, [])
return (
<>
<Head>
<style dangerouslySetInnerHTML={{__html: customCSS}} />
<noscript>
<style dangerouslySetInnerHTML={{__html: noscriptCSS}} />
</noscript>
</Head>
<WrappedComponent {...(props as any)} />
</>
)
}
BlitzAppRoot.displayName = `BlitzAppRoot`
return BlitzAppRoot

View File

@@ -30,6 +30,8 @@ export {
export {
getAntiCSRFToken,
useSession,
useAuthorize,
useRedirectAuthenticatedUser,
SessionConfig, // new
SessionContext,
AuthenticatedSessionContext,

View File

@@ -1,5 +1,6 @@
import {useState} from "react"
import {useEffect, useState} from "react"
import {COOKIE_CSRF_TOKEN} from "./constants"
import {AuthenticationError} from "./errors"
import {Ctx} from "./middleware"
import {publicDataStore} from "./public-data-store"
import {IsAuthorizedArgs, PublicData} from "./types"
@@ -82,3 +83,19 @@ export const useSession = (options: UseSessionOptions = {}): PublicDataWithLoadi
return {...publicData, isLoading}
}
export const useAuthorize = () => {
useEffect(() => {
if (!publicDataStore.getData().userId) {
const error = new AuthenticationError()
delete error.stack
throw error
}
})
}
export const useRedirectAuthenticatedUser = (to: string) => {
if (typeof window !== "undefined" && publicDataStore.getData().userId) {
window.location.replace(to)
}
}