Skip to content

Commit 30da724

Browse files
author
Alex Chew
committed
Fix dev mode login/logout redirect
1 parent 02db7ba commit 30da724

File tree

7 files changed

+50
-17
lines changed

7 files changed

+50
-17
lines changed

cloudformation/template.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,20 +1121,20 @@ Resources:
11211121
SupportedIdentityProviders: [ "COGNITO" ] # should (eventually) allow people to add values
11221122
CallbackURL: !If [ DevelopmentMode,
11231123
[
1124-
'http://localhost:3000/login',
1125-
!Join [ '', [ 'https://', !GetAtt DevPortalSiteS3Bucket.RegionalDomainName ]]
1124+
'http://localhost:3000/index.html?action=login',
1125+
!Join [ '', [ 'https://', !GetAtt DevPortalSiteS3Bucket.RegionalDomainName, '/index.html?action=login' ]]
11261126
],
11271127
[
1128-
!Join [ '', [ 'https://', !If [ UseCustomDomainName, !Ref CustomDomainName, !GetAtt DefaultCloudfrontDistribution.DomainName ], '/login' ]]
1128+
!Join [ '', [ 'https://', !If [ UseCustomDomainName, !Ref CustomDomainName, !GetAtt DefaultCloudfrontDistribution.DomainName ], '/index.html?action=login' ]]
11291129
]
11301130
]
11311131
LogoutURL: !If [ DevelopmentMode,
11321132
[
1133-
'http://localhost:3000',
1134-
!Join [ '', [ 'https://', !GetAtt DevPortalSiteS3Bucket.RegionalDomainName]]
1133+
'http://localhost:3000/index.html?action=logout',
1134+
!Join [ '', [ 'https://', !GetAtt DevPortalSiteS3Bucket.RegionalDomainName, '/index.html?action=logout' ]]
11351135
],
11361136
[
1137-
!Join [ '', [ 'https://', !If [ UseCustomDomainName, !Ref CustomDomainName, !GetAtt DefaultCloudfrontDistribution.DomainName ]]]
1137+
!Join [ '', [ 'https://', !If [ UseCustomDomainName, !Ref CustomDomainName, !GetAtt DefaultCloudfrontDistribution.DomainName ], '/index.html?action=logout' ]]
11381138
]
11391139
]
11401140
AllowedOAuthFlowsUserPoolClient: true

dev-portal/package-lock.json

Lines changed: 22 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dev-portal/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"mobx": "^5.5.0",
1212
"mobx-react": "^5.2.8",
1313
"object-hash": "^1.3.1",
14+
"query-string": "^6.8.1",
1415
"react": "^16.8.4",
1516
"react-dom": "^16.8.4",
1617
"react-markdown": "^4.0.3",

dev-portal/src/components/NavBar.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import React from 'react'
55
import { Link } from 'react-router-dom'
66
import { Menu, Image } from 'semantic-ui-react'
77

8-
import { isAdmin, isAuthenticated, logout } from 'services/self'
8+
import { isAdmin, isAuthenticated, logout, getLoginRedirectUrl } from 'services/self'
99

1010
import { cognitoDomain, cognitoClientId } from '../services/api'
1111

@@ -21,7 +21,7 @@ import Register from './Register'
2121
export const NavBar = observer(
2222
class NavBar extends React.Component {
2323
getCognitoUrl = (type) => {
24-
let redirectUri = `${window.location.protocol}//${window.location.host}/login`
24+
let redirectUri = getLoginRedirectUrl()
2525
return `${cognitoDomain}/${type}?response_type=token&client_id=${cognitoClientId}&redirect_uri=${redirectUri}`
2626
}
2727

dev-portal/src/components/Register.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import React from 'react'
55
import {Menu} from 'semantic-ui-react'
66
import {Redirect} from 'react-router-dom'
77

8+
import {getLoginRedirectUrl} from 'services/self'
89
import {cognitoDomain, cognitoClientId} from '../services/api'
910

1011
export default class Register extends React.Component {
11-
redirectUri = `${window.location.protocol}//${window.location.host}/login`
12+
redirectUri = getLoginRedirectUrl()
1213

1314
render() {
1415
return this.props.signedIn ? <Redirect to='/apis'/> : (
1516
<Menu.Item key="register" as="a"
1617
href={`${cognitoDomain}/signup?response_type=token&client_id=${cognitoClientId}&redirect_uri=${this.redirectUri}`}>Register</Menu.Item>)
1718
}
18-
}
19+
}

dev-portal/src/index.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import React from 'react';
55
import ReactDOM from 'react-dom';
66
import { BrowserRouter, Route, Switch, Redirect } from 'react-router-dom'
77

8+
import * as queryString from 'query-string'
9+
810
// content-fragments (import here to start this ASAP)
911
import 'services/get-fragments'
1012

@@ -62,9 +64,15 @@ class App extends React.Component {
6264
<GlobalModal />
6365
<Switch>
6466
<Route exact path="/" component={Home} />
65-
<Route exact path="/index.html" component={() =>
66-
<Redirect to="/"/>
67-
} />
67+
<Route exact path="/index.html" component={() => {
68+
const { action } = queryString.parse(window.location.search)
69+
if (action === 'login') {
70+
login()
71+
} else if (action === 'logout') {
72+
logout()
73+
}
74+
return <Redirect to="/" />
75+
}} />
6876
<Route path="/getting-started" component={GettingStarted} />
6977
<Route path="/dashboard" component={Dashboard} />
7078
<AdminRoute path="/admin" component={Admin} />

dev-portal/src/services/self.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ export function login() {
7777
})
7878
}
7979

80+
export const getLoginRedirectUrl = () => `${window.location.protocol}//${window.location.host}/index.html?action=login`
81+
export const getLogoutRedirectUrl = () => `${window.location.protocol}//${window.location.host}/index.html?action=logout`
82+
8083
function setCredentials() {
8184
let preferred_role = jwt_decode(store.idToken)['cognito:preferred_role']
8285
let params = {
@@ -114,7 +117,8 @@ export function logout() {
114117

115118
if (cognitoDomain) {
116119
// redirect to cognito to log out there, too
117-
window.location = `${cognitoDomain}/logout?client_id=${cognitoClientId}&logout_uri=${window.location.protocol}//${window.location.host}`;
120+
const redirectUrl = getLogoutRedirectUrl()
121+
window.location = `${cognitoDomain}/logout?client_id=${cognitoClientId}&logout_uri=${redirectUrl}`
118122
}
119123
}
120124
}

0 commit comments

Comments
 (0)