Przeglądaj źródła

Use custom domain next to its input. Closes #15.

Pouria Ezzati 7 lat temu
rodzic
commit
1c1d16b8bb

+ 5 - 3
client/actions/index.js

@@ -77,7 +77,7 @@ export const generateApiKey = () => dispatch => {
 };
 
 /* Login & signup actions */
-export const authUser = payload => ({ type: types.AUTH_USER, payload: decodeJwt(payload).sub });
+export const authUser = payload => ({ type: types.AUTH_USER, payload });
 export const unauthUser = () => ({ type: types.UNAUTH_USER });
 export const sentVerification = payload => ({ type: types.SENT_VERIFICATION, payload });
 export const showAuthError = payload => ({ type: types.AUTH_ERROR, payload });
@@ -104,7 +104,8 @@ export const loginUser = body => dispatch => {
       const { token } = res.data;
       cookie.set('token', token, { expires: 7 });
       dispatch(authRenew());
-      dispatch(authUser(token));
+      dispatch(authUser(decodeJwt(token).sub));
+      dispatch(setDomain(decodeJwt(token).domain));
       dispatch(showPageLoading());
       Router.push('/');
     })
@@ -126,7 +127,8 @@ export const renewAuthUser = () => (dispatch, getState) => {
       const { token } = res.data;
       cookie.set('token', token, { expires: 7 });
       dispatch(authRenew());
-      dispatch(authUser(token));
+      dispatch(authUser(decodeJwt(token).sub));
+      dispatch(setDomain(decodeJwt(token).domain));
     })
     .catch(() => {
       cookie.remove('token');

+ 12 - 1
client/components/Shortener/Shortener.js

@@ -49,12 +49,19 @@ class Shortener extends Component {
   }
 
   shouldComponentUpdate(nextProps, nextState) {
-    const { isAuthenticated, shortenerError, shortenerLoading, url: { isShortened } } = this.props;
+    const {
+      isAuthenticated,
+      domain,
+      shortenerError,
+      shortenerLoading,
+      url: { isShortened },
+    } = this.props;
     return (
       isAuthenticated !== nextProps.isAuthenticated ||
       shortenerError !== nextProps.shortenerError ||
       isShortened !== nextProps.url.isShortened ||
       shortenerLoading !== nextProps.shortenerLoading ||
+      domain !== nextProps.domain ||
       this.state.isCopied !== nextState.isCopied
     );
   }
@@ -120,6 +127,7 @@ class Shortener extends Component {
           isAuthenticated={isAuthenticated}
           handleSubmit={this.handleSubmit}
           setShortenerFormError={this.props.setShortenerFormError}
+          domain={this.props.domain}
         />
       </Wrapper>
     );
@@ -128,6 +136,7 @@ class Shortener extends Component {
 
 Shortener.propTypes = {
   isAuthenticated: PropTypes.bool.isRequired,
+  domain: PropTypes.string.isRequired,
   createShortUrl: PropTypes.func.isRequired,
   shortenerError: PropTypes.string.isRequired,
   shortenerLoading: PropTypes.bool.isRequired,
@@ -142,9 +151,11 @@ const mapStateToProps = ({
   auth: { isAuthenticated },
   error: { shortener: shortenerError },
   loading: { shortener: shortenerLoading },
+  settings: { customDomain: domain },
   url,
 }) => ({
   isAuthenticated,
+  domain,
   shortenerError,
   shortenerLoading,
   url,

+ 3 - 1
client/components/Shortener/ShortenerInput.js

@@ -52,7 +52,7 @@ const Icon = styled(SVG)`
   }
 `;
 
-const ShortenerInput = ({ isAuthenticated, handleSubmit, setShortenerFormError }) => (
+const ShortenerInput = ({ isAuthenticated, domain, handleSubmit, setShortenerFormError }) => (
   <ShortenerForm id="shortenerform" onSubmit={handleSubmit}>
     <TextInput id="target" name="target" placeholder="Paste your long URL" autoFocus />
     <Submit onClick={handleSubmit}>
@@ -62,6 +62,7 @@ const ShortenerInput = ({ isAuthenticated, handleSubmit, setShortenerFormError }
     <ShortenerOptions
       isAuthenticated={isAuthenticated}
       setShortenerFormError={setShortenerFormError}
+      domain={domain}
     />
   </ShortenerForm>
 );
@@ -69,6 +70,7 @@ const ShortenerInput = ({ isAuthenticated, handleSubmit, setShortenerFormError }
 ShortenerInput.propTypes = {
   handleSubmit: PropTypes.func.isRequired,
   isAuthenticated: PropTypes.bool.isRequired,
+  domain: PropTypes.string.isRequired,
   setShortenerFormError: PropTypes.func.isRequired,
 };
 

+ 4 - 2
client/components/Shortener/ShortenerOptions.js

@@ -69,6 +69,7 @@ class ShortenerOptions extends Component {
     return (
       this.props.isAuthenticated !== nextProps.isAuthenticated ||
       customurlCheckbox !== nextState.customurlCheckbox ||
+      this.props.domain !== nextProps.domain ||
       passwordCheckbox !== nextState.passwordCheckbox
     );
   }
@@ -84,10 +85,10 @@ class ShortenerOptions extends Component {
 
   render() {
     const { customurlCheckbox, passwordCheckbox } = this.state;
-    const { isAuthenticated } = this.props;
+    const { isAuthenticated, domain } = this.props;
     const customUrlInput = customurlCheckbox && (
       <div>
-        <Label htmlFor="customurl">{window.location.hostname}/</Label>
+        <Label htmlFor="customurl">{domain || window.location.hostname}/</Label>
         <TextInput id="customurl" type="text" placeholder="custom name" small />
       </div>
     );
@@ -126,6 +127,7 @@ class ShortenerOptions extends Component {
 
 ShortenerOptions.propTypes = {
   isAuthenticated: PropTypes.bool.isRequired,
+  domain: PropTypes.string.isRequired,
   setShortenerFormError: PropTypes.func.isRequired,
 };
 

+ 1 - 0
server/controllers/authController.js

@@ -32,6 +32,7 @@ const signToken = user =>
     {
       iss: 'ApiAuth',
       sub: user.email,
+      domain: user.domain || '',
       iat: new Date().getTime(),
       exp: new Date().setDate(new Date().getDate() + 7),
     },