Skip to content

Commit

Permalink
Fix dev mode login/logout redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Chew committed Jul 2, 2019
1 parent 02db7ba commit 30da724
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 17 deletions.
12 changes: 6 additions & 6 deletions cloudformation/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1121,20 +1121,20 @@ Resources:
SupportedIdentityProviders: [ "COGNITO" ] # should (eventually) allow people to add values
CallbackURL: !If [ DevelopmentMode,
[
'http://localhost:3000/login',
!Join [ '', [ 'https://', !GetAtt DevPortalSiteS3Bucket.RegionalDomainName ]]
'http://localhost:3000/index.html?action=login',
!Join [ '', [ 'https://', !GetAtt DevPortalSiteS3Bucket.RegionalDomainName, '/index.html?action=login' ]]
],
[
!Join [ '', [ 'https://', !If [ UseCustomDomainName, !Ref CustomDomainName, !GetAtt DefaultCloudfrontDistribution.DomainName ], '/login' ]]
!Join [ '', [ 'https://', !If [ UseCustomDomainName, !Ref CustomDomainName, !GetAtt DefaultCloudfrontDistribution.DomainName ], '/index.html?action=login' ]]
]
]
LogoutURL: !If [ DevelopmentMode,
[
'http://localhost:3000',
!Join [ '', [ 'https://', !GetAtt DevPortalSiteS3Bucket.RegionalDomainName]]
'http://localhost:3000/index.html?action=logout',
!Join [ '', [ 'https://', !GetAtt DevPortalSiteS3Bucket.RegionalDomainName, '/index.html?action=logout' ]]
],
[
!Join [ '', [ 'https://', !If [ UseCustomDomainName, !Ref CustomDomainName, !GetAtt DefaultCloudfrontDistribution.DomainName ]]]
!Join [ '', [ 'https://', !If [ UseCustomDomainName, !Ref CustomDomainName, !GetAtt DefaultCloudfrontDistribution.DomainName ], '/index.html?action=logout' ]]
]
]
AllowedOAuthFlowsUserPoolClient: true
Expand Down
25 changes: 22 additions & 3 deletions dev-portal/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dev-portal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"mobx": "^5.5.0",
"mobx-react": "^5.2.8",
"object-hash": "^1.3.1",
"query-string": "^6.8.1",
"react": "^16.8.4",
"react-dom": "^16.8.4",
"react-markdown": "^4.0.3",
Expand Down
4 changes: 2 additions & 2 deletions dev-portal/src/components/NavBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import React from 'react'
import { Link } from 'react-router-dom'
import { Menu, Image } from 'semantic-ui-react'

import { isAdmin, isAuthenticated, logout } from 'services/self'
import { isAdmin, isAuthenticated, logout, getLoginRedirectUrl } from 'services/self'

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

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

Expand Down
5 changes: 3 additions & 2 deletions dev-portal/src/components/Register.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import React from 'react'
import {Menu} from 'semantic-ui-react'
import {Redirect} from 'react-router-dom'

import {getLoginRedirectUrl} from 'services/self'
import {cognitoDomain, cognitoClientId} from '../services/api'

export default class Register extends React.Component {
redirectUri = `${window.location.protocol}//${window.location.host}/login`
redirectUri = getLoginRedirectUrl()

render() {
return this.props.signedIn ? <Redirect to='/apis'/> : (
<Menu.Item key="register" as="a"
href={`${cognitoDomain}/signup?response_type=token&client_id=${cognitoClientId}&redirect_uri=${this.redirectUri}`}>Register</Menu.Item>)
}
}
}
14 changes: 11 additions & 3 deletions dev-portal/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter, Route, Switch, Redirect } from 'react-router-dom'

import * as queryString from 'query-string'

// content-fragments (import here to start this ASAP)
import 'services/get-fragments'

Expand Down Expand Up @@ -62,9 +64,15 @@ class App extends React.Component {
<GlobalModal />
<Switch>
<Route exact path="/" component={Home} />
<Route exact path="/index.html" component={() =>
<Redirect to="/"/>
} />
<Route exact path="/index.html" component={() => {
const { action } = queryString.parse(window.location.search)
if (action === 'login') {
login()
} else if (action === 'logout') {
logout()
}
return <Redirect to="/" />
}} />
<Route path="/getting-started" component={GettingStarted} />
<Route path="/dashboard" component={Dashboard} />
<AdminRoute path="/admin" component={Admin} />
Expand Down
6 changes: 5 additions & 1 deletion dev-portal/src/services/self.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ export function login() {
})
}

export const getLoginRedirectUrl = () => `${window.location.protocol}//${window.location.host}/index.html?action=login`
export const getLogoutRedirectUrl = () => `${window.location.protocol}//${window.location.host}/index.html?action=logout`

function setCredentials() {
let preferred_role = jwt_decode(store.idToken)['cognito:preferred_role']
let params = {
Expand Down Expand Up @@ -114,7 +117,8 @@ export function logout() {

if (cognitoDomain) {
// redirect to cognito to log out there, too
window.location = `${cognitoDomain}/logout?client_id=${cognitoClientId}&logout_uri=${window.location.protocol}//${window.location.host}`;
const redirectUrl = getLogoutRedirectUrl()
window.location = `${cognitoDomain}/logout?client_id=${cognitoClientId}&logout_uri=${redirectUrl}`
}
}
}

0 comments on commit 30da724

Please sign in to comment.