Skip to content

Commit b9a909b

Browse files
authored
Change Authentication (codesandbox#58)
* Use localStorage auth * Add sign out function * Remove Pricing api change * Add notice for sign out * Use an exclamation mark
1 parent c98ac21 commit b9a909b

File tree

4 files changed

+107
-11
lines changed

4 files changed

+107
-11
lines changed

src/app/containers/Modal.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const ModalTitle = styled.h1`
7474
margin: 0;
7575
font-size: 1.25rem;
7676
text-align: center;
77-
background-image: linear-gradient(-225deg, #31B0FF 0%, #47A8E5 100%);
77+
background-image: linear-gradient(-225deg, #31b0ff 0%, #47a8e5 100%);
7878
`;
7979

8080
const ModalBody = styled.div`
@@ -130,9 +130,10 @@ class ModalContainer extends React.PureComponent {
130130
>
131131
{modal.open &&
132132
<BaseModal>
133-
<ModalTitle>
134-
{modal.title}
135-
</ModalTitle>
133+
{modal.title &&
134+
<ModalTitle>
135+
{modal.title}
136+
</ModalTitle>}
136137
<ModalBody>
137138
{modal.Body}
138139
</ModalBody>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import React from 'react';
2+
import styled from 'styled-components';
3+
import { connect } from 'react-redux';
4+
import { bindActionCreators } from 'redux';
5+
6+
import userActionCreators from 'app/store/user/actions';
7+
import modalActionCreators from 'app/store/modal/actions';
8+
import Button from '../../components/buttons/Button';
9+
import Row from '../../components/flex/Row';
10+
11+
const Container = styled.div`
12+
background-color: ${props => props.theme.background};
13+
padding: 1rem;
14+
margin: 0;
15+
color: rgba(255, 255, 255, 0.8);
16+
`;
17+
18+
const Heading = styled.h2`margin-top: 0;`;
19+
20+
const Explanation = styled.p`
21+
line-height: 1.3;
22+
margin-bottom: 2rem;
23+
`;
24+
25+
const mapDispatchToProps = dispatch => ({
26+
userActions: bindActionCreators(userActionCreators, dispatch),
27+
modalActions: bindActionCreators(modalActionCreators, dispatch),
28+
});
29+
const SignOutNotice = ({
30+
userActions,
31+
modalActions,
32+
}: {
33+
userActions: typeof userActionCreators,
34+
modalActions: typeof modalActionCreators,
35+
}) =>
36+
<Container>
37+
<Heading>You have been signed out</Heading>
38+
<Explanation>
39+
CodeSandbox has migrated to a system where authorization tokens can be
40+
managed and revoked, and we had to sign everyone out for this.
41+
<br />
42+
<br />But don{"'"}t worry, you can sign in right again!
43+
</Explanation>
44+
45+
<Row justifyContent="space-around">
46+
<Button
47+
block
48+
style={{ marginRight: '.5rem' }}
49+
red
50+
onClick={modalActions.closeModal}
51+
>
52+
Close
53+
</Button>
54+
<Button
55+
block
56+
style={{ marginLeft: '.5rem' }}
57+
onClick={() => userActions.signIn().then(modalActions.closeModal)}
58+
>
59+
Sign in
60+
</Button>
61+
</Row>
62+
</Container>;
63+
64+
export default connect(null, mapDispatchToProps)(SignOutNotice);

src/app/store/user/actions.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
// @flow
2+
import React from 'react';
23
import { signInUrl } from 'app/utils/url-generator';
34
import { identify } from 'app/utils/analytics';
45

6+
import SignOutNotice from 'app/containers/modals/SignOutNotice';
7+
58
import { createAPIActions, doRequest } from '../api/actions';
69
import notifActions from '../notifications/actions';
10+
import modalActions from '../modal/actions';
711

812
import openPopup from './utils/popup';
13+
import { resetJwt, setJwt } from './utils/jwt';
914
import { jwtSelector } from './selectors';
1015

1116
export const SIGN_IN = 'SIGN_IN';
@@ -14,6 +19,7 @@ export const SET_CURRENT_USER = 'SET_CURRENT_USER';
1419
export const SIGN_OUT = 'SIGN_OUT';
1520
export const SET_USER_SANDBOXES = 'SET_USER_SANDBOXES';
1621

22+
export const SIGN_OUT_API = createAPIActions('SIGN_OUT_API', 'DELETE');
1723
export const GET_CURRENT_USER_API = createAPIActions('CURRENT_USER', 'FETCH');
1824
export const UPDATE_CURRENT_USER_API = createAPIActions(
1925
'CURRENT_USER',
@@ -26,12 +32,16 @@ export const LOAD_USER_SANDBOXES = createAPIActions(
2632
export const SEND_FEEDBACK_API = createAPIActions('FEEDBACK', 'SEND');
2733
export const GET_AUTH_TOKEN_API = createAPIActions('AUTH_TOKEN', 'FETCH');
2834

29-
const deleteCookie = (name: string) => {
30-
document.cookie = `${name}=; Path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT;`;
31-
};
35+
const signOut = (apiRequest = true) => async (dispatch: Function) => {
36+
if (apiRequest) {
37+
await dispatch(
38+
doRequest(SIGN_OUT_API, 'users/signout', {
39+
method: 'DELETE',
40+
}),
41+
);
42+
}
3243

33-
const signOut = () => async (dispatch: Function) => {
34-
deleteCookie('jwt');
44+
await resetJwt();
3545

3646
dispatch({
3747
type: SIGN_OUT,
@@ -51,7 +61,13 @@ const getCurrentUser = () => async (dispatch: Function, getState: Function) => {
5161
dispatch({ type: SET_CURRENT_USER, data });
5262
return data;
5363
} catch (e) {
54-
dispatch(signOut());
64+
if (e.response.status === 401) {
65+
dispatch(
66+
modalActions.openModal({ Body: <SignOutNotice />, width: 500 }),
67+
);
68+
await dispatch(signOut(false));
69+
}
70+
return null;
5571
}
5672
}
5773
};
@@ -66,6 +82,7 @@ const signIn = () => (dispatch: Function) =>
6682
window.addEventListener('message', function(e) {
6783
if (e.data.type === 'signin') {
6884
const jwt = e.data.data.jwt;
85+
setJwt(jwt);
6986
window.removeEventListener('message', this);
7087
popup.close();
7188

src/app/store/user/utils/jwt.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
import store from 'store/dist/store.modern';
2+
13
export default function getJwt() {
2-
return (document.cookie.match(/[; ]?jwt=([^\s;]*)/) || [])[1];
4+
// TODO remove cookie
5+
return (
6+
store.get('jwt') || (document.cookie.match(/[; ]?jwt=([^\s;]*)/) || [])[1]
7+
);
8+
}
9+
10+
export function resetJwt() {
11+
document.cookie = `jwt=; Path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT;`;
12+
return store.set('jwt', null);
13+
}
14+
15+
export function setJwt(jwt: string) {
16+
return store.set('jwt', jwt);
317
}

0 commit comments

Comments
 (0)