Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: #568 add more config to scaffolder and fix private route wrapper for check isFetching or not #775

Merged
merged 1 commit into from
Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/react-app-scaffolder/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ module.exports = class extends Generator {
* else current path
*/
if (this.answers.isFoundation) {
this.packagePath = path.resolve(__dirname, `../../${this.answers.name}`)
this.packagePath = path.resolve(__dirname, '../..', this.answers.name)
/**
* create directory if not
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"cognitoClientId": "<%= clientId %>",
"googleAnalyticsKey": "",
"cognitoOAuthUrl": "https://dev.connect.reapit.cloud",
"cognitoUserPoolId": ""
"cognitoUserPoolId": "eu-west-2_hbt0B7yys",
"platformApiUrl": "https://dev.platform.reapit.cloud"
}
3 changes: 2 additions & 1 deletion packages/react-app-scaffolder/app/templates/_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"cognitoClientId": "<%= clientId %>",
"googleAnalyticsKey": "",
"cognitoOAuthUrl": "https://dev.connect.reapit.cloud",
"cognitoUserPoolId": ""
"cognitoUserPoolId": "eu-west-2_hbt0B7yys",
"platformApiUrl": "https://dev.platform.reapit.cloud"
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const ResolveTSPathsToWebpackAlias = require('ts-paths-to-webpack-alias')
const {PATHS} = require('./constants')

module.exports = {
mode: 'development',
context: process.cwd(),
entry: PATHS.entryWeb,
output: {
Expand Down Expand Up @@ -86,6 +87,7 @@ module.exports = {
open: true,
clientLogLevel: 'warning',
historyApiFallback: true,
disableHostCheck: true,
stats: {
cached: false,
cachedAssets: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const FaviconsWebpackPlugin = require('favicons-webpack-plugin')
const ResolveTSPathsToWebpackAlias = require('ts-paths-to-webpack-alias')

module.exports = {
mode: 'development',
context: process.cwd(),
entry: './src/core/index.tsx',
output: {
Expand Down Expand Up @@ -132,6 +133,7 @@ module.exports = {
open: true,
clientLogLevel: 'warning',
historyApiFallback: true,
disableHostCheck: true,
stats: {
cached: false,
cachedAssets: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ import globalCss from 'raw-loader!sass-loader!@reapit/elements/dist/index.css'

const App = () => {
<% if (!redux && !graphql) { %>
const { loginSession, refreshParams, getLoginSession, ...rest } = useAuth()
if (!loginSession && refreshParams) {
const { loginSession, refreshParams, getLoginSession, isFetchSession, ...rest } = useAuth()
if (!loginSession && refreshParams && !isFetchSession) {
getLoginSession(refreshParams)
}
<% } %>
<% if (graphql) { %>
const { loginSession, refreshParams, getLoginSession, ...rest } = useAuth()
if (!loginSession && refreshParams) {
const { loginSession, refreshParams, getLoginSession, isFetchSession, ...rest } = useAuth()
if (!loginSession && refreshParams && !isFetchSession) {
getLoginSession(refreshParams)
}
const accessToken = loginSession?.accessToken || ''
<% } %>
return (
<% if (!redux) { %>
<AuthContext.Provider value={{ loginSession, refreshParams, getLoginSession, ...rest }}>
<AuthContext.Provider value={{ loginSession, refreshParams, getLoginSession, isFetchSession, ...rest }}>
<% } %>
<% if (graphql) { %>
<ApolloProvider client={getClient(accessToken, window.reapit.config.graphqlUri)}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const mockContext = {
cognitoClientId: '123',
refreshToken: 'mockRefreshToken',
},
isFetchSession: false,
logout: jest.fn(),
getLoginSession: jest.fn(),
refreshParams: null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import * as React from 'react'
import { withRouter, RouteComponentProps, Redirect } from 'react-router-dom'
import { withRouter, RouteComponentProps } from 'react-router-dom'
import Menu from '@/components/ui/menu'
import { Loader, AppNavContainer, Section } from '@reapit/elements'
import { redirectToOAuth } from '@reapit/cognito-auth'
import { AuthContext } from '@/context'
import Routes from '@/constants/routes'

const { Suspense } = React

Expand All @@ -13,23 +12,27 @@ export type PrivateRouteWrapperProps = RouteComponentProps & {
}

export const PrivateRouteWrapper: React.FunctionComponent<PrivateRouteWrapperProps> = ({ children }) => {
const { loginSession, refreshParams, getLoginSession } = React.useContext(AuthContext)
const { loginSession, refreshParams, getLoginSession, isFetchSession } = React.useContext(AuthContext)

if (!loginSession && !refreshParams) {
redirectToOAuth(window.reapit.config.cognitoClientId)
return null
}

if (!loginSession && refreshParams) {
if (!loginSession && refreshParams && !isFetchSession) {
getLoginSession(refreshParams)
}

if (!loginSession) {
return null
}

if (location.pathname === '/') {
return <Redirect to={Routes.HOME} />
if (isFetchSession) {
return (
<Section>
<Loader />
</Section>
)
}

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ export type AuthHook = {
logout: () => void
getLoginSession: (refreshParams: RefreshParams | null) => Promise<void>
refreshParams?: RefreshParams | null
isFetchSession: boolean
}

export const useAuth = (): AuthHook => {
const [isFetchSession, setFetchSession] = React.useState(false)
const [loginSession, setLoginSession] = React.useState<LoginSession | null>(null)
const urlParams: RefreshParams | null = getTokenFromQueryString(
window.location.search,
window.reapit.config.cognitoClientId
window.reapit.config.cognitoClientId,
)
const cookieParams = getSessionCookie(COOKIE_SESSION_KEY)
const refreshParams = cookieParams ? cookieParams : urlParams
Expand Down Expand Up @@ -51,6 +52,7 @@ export const useAuth = (): AuthHook => {
loginSession,
logout,
getLoginSession,
isFetchSession,
}
}

Expand Down