]> Untitled Git - lemmy-ui.git/commitdiff
Logging in working, languages working.
authorDessalines <tyhou13@gmx.com>
Wed, 9 Sep 2020 20:33:40 +0000 (15:33 -0500)
committerDessalines <tyhou13@gmx.com>
Wed, 9 Sep 2020 20:33:40 +0000 (15:33 -0500)
src/server/index.tsx
src/shared/components/comment-form.tsx
src/shared/components/create-community.tsx
src/shared/components/create-post.tsx
src/shared/components/navbar.tsx
src/shared/components/post-listing.tsx
src/shared/services/UserService.ts
src/shared/utils.ts

index 3eace286252f3f8344ce2f598eba553e15c3ff7e..0553192112360292fca97f1f0c4c95f8ea1dbe8d 100644 (file)
@@ -84,6 +84,12 @@ server.get('/*', async (req, res) => {
            </head>
 
            <body>
+             <noscript>
+               <nav class="navbar fixed-bottom navbar-light bg-light">
+                Javascript is disabled. Actions will not work.
+               </nav>
+             </noscript>
+            
              <div id='root'>${renderToString(wrapper)}</div>
              <script src='/static/js/client.js'></script>
            </body>
index 79115116019fc6ea27096bb1d4b7a092ac3bc847..b032a065c6dc869b312410462b88f2d3c1e266af 100644 (file)
@@ -99,7 +99,7 @@ export class CommentForm extends Component<CommentFormProps, CommentFormState> {
             </svg>
             <T i18nKey="must_login" class="d-inline">
               #
-              <Link class="alert-link" to="/login">
+              <Link className="alert-link" to="/login">
                 #
               </Link>
             </T>
index 07680b5d8c5d22edcbc55efd086febb233d2126a..26065373b7d777b5c95958c0af57bd8a152bf124 100644 (file)
@@ -43,8 +43,7 @@ export class CreateCommunity extends Component<any, CreateCommunityState> {
     this.parseMessage = this.parseMessage.bind(this);
     this.subscription = wsSubscribe(this.parseMessage);
 
-    // TODO not sure if this works
-    if (!UserService.Instance.user) {
+    if (!UserService.Instance.user && isBrowser()) {
       toast(i18n.t('not_logged_in'), 'danger');
       this.context.router.history.push(`/login`);
     }
index 25d4f593c2b4bdc8f19958ac4e73fc1fc21f057c..a3c87c3eab90df933b6d4374b2485c1877ca4264 100644 (file)
@@ -44,7 +44,7 @@ export class CreatePost extends Component<any, CreatePostState> {
     this.handlePostCreate = this.handlePostCreate.bind(this);
     this.state = this.emptyState;
 
-    if (!UserService.Instance.user) {
+    if (!UserService.Instance.user && isBrowser()) {
       toast(i18n.t('not_logged_in'), 'danger');
       this.context.router.history.push(`/login`);
     }
index 5c7c43879319efd7c943a8a30784679553245785..1cf89c03a0e71e495ee7bd32dc650eb54583f4cb 100644 (file)
@@ -1,7 +1,6 @@
 import { Component, linkEvent, createRef, RefObject } from 'inferno';
 import { Link } from 'inferno-router';
 import { Subscription } from 'rxjs';
-import { retryWhen, delay, take } from 'rxjs/operators';
 import { WebSocketService, UserService } from '../services';
 import {
   UserOperation,
@@ -30,6 +29,7 @@ import {
   notifyComment,
   notifyPrivateMessage,
   isBrowser,
+  wsSubscribe,
 } from '../utils';
 import { i18n } from '../i18next';
 
@@ -64,56 +64,56 @@ export class Navbar extends Component<NavbarProps, NavbarState> {
     searchParam: '',
     toggleSearch: false,
   };
+  subscription: any;
 
   constructor(props: any, context: any) {
     super(props, context);
     this.state = this.emptyState;
 
-    if (isBrowser()) {
-      this.wsSub = WebSocketService.Instance.subject
-        .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
-        .subscribe(
-          msg => this.parseMessage(msg),
-          err => console.error(err),
-          () => console.log('complete')
-        );
-
-      this.searchTextField = createRef();
-    }
+    this.parseMessage = this.parseMessage.bind(this);
+    this.subscription = wsSubscribe(this.parseMessage);
 
     // The login
     // TODO this needs some work
-    if (this.props.site.my_user) {
-      UserService.Instance.user = this.props.site.my_user;
-      // i18n.changeLanguage(getLanguage());
-
-      if (isBrowser()) {
-        WebSocketService.Instance.userJoin();
-        // On the first load, check the unreads
-        if (this.state.isLoggedIn == false) {
-          this.requestNotificationPermission();
-          this.fetchUnreads();
-          // setTheme(data.my_user.theme, true);
-          // i18n.changeLanguage(getLanguage());
-          // i18n.changeLanguage('de');
-        }
-      }
+    UserService.Instance.user = this.props.site.my_user;
+    i18n.changeLanguage(getLanguage());
+    setTheme();
+    if (UserService.Instance.user) {
     }
+
+    // if (!!this.props.site.my_user) {
+    //   UserService.Instance.this.props.site.my_user);
+    //   // UserService.Instance.user = this.props.site.my_user;
+    // } else {
+    //   UserService.Instance.setUser(undefined);
+    // }
   }
 
   componentDidMount() {
     // Subscribe to jwt changes
     if (isBrowser()) {
+      this.searchTextField = createRef();
+      console.log(`isLoggedIn = ${this.state.isLoggedIn}`);
+
+      // On the first load, check the unreads
+      if (this.state.isLoggedIn == false) {
+        // setTheme(data.my_user.theme, true);
+        // i18n.changeLanguage(getLanguage());
+        // i18n.changeLanguage('de');
+      } else {
+        this.requestNotificationPermission();
+        WebSocketService.Instance.userJoin();
+        this.fetchUnreads();
+      }
+
       this.userSub = UserService.Instance.jwtSub.subscribe(res => {
         // A login
         if (res !== undefined) {
           this.requestNotificationPermission();
+          WebSocketService.Instance.getSite();
         } else {
-          this.state.isLoggedIn = false;
+          this.setState({ isLoggedIn: false });
         }
-        console.log('a new login');
-        // WebSocketService.Instance.getSite();
-        this.setState(this.state);
       });
 
       // Subscribe to unread count changes
@@ -415,6 +415,12 @@ export class Navbar extends Component<NavbarProps, NavbarState> {
       this.state.unreadCount = this.calculateUnreadCount();
       this.setState(this.state);
       this.sendUnreadCount();
+    } else if (res.op == UserOperation.GetSite) {
+      // This is only called on a successful login
+      let data = res.data as GetSiteResponse;
+      UserService.Instance.user = data.my_user;
+      this.state.isLoggedIn = true;
+      this.setState(this.state);
     } else if (res.op == UserOperation.CreateComment) {
       let data = res.data as CommentResponse;
 
index 23eed736c7b6b88926d8dd0454bf17fd011fd265..b899e7555ea047b934669bf7dcee79d2cb0506fa 100644 (file)
@@ -143,18 +143,16 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
             this.props.post.embed_title && (
               <IFramelyCard post={this.props.post} />
             )}
-          {this.props.showBody && this.props.post.body && (
-            <>
-              {this.state.viewSource ? (
-                <pre>{this.props.post.body}</pre>
-              ) : (
-                <div
-                  className="md-div"
-                  dangerouslySetInnerHTML={mdToHtml(this.props.post.body)}
-                />
-              )}
-            </>
-          )}
+          {this.props.showBody &&
+            this.props.post.body &&
+            (this.state.viewSource ? (
+              <pre>{this.props.post.body}</pre>
+            ) : (
+              <div
+                className="md-div"
+                dangerouslySetInnerHTML={mdToHtml(this.props.post.body)}
+              />
+            ))}
         </div>
       </div>
     );
index 785ce3ff4c394899ba1c7863a47e29ed2269ab1d..760fb4e4dd8b1d81c1b7cce1034459a41a20d447 100644 (file)
@@ -20,9 +20,8 @@ export class UserService {
   );
 
   private constructor() {
-    let jwt = IsomorphicCookie.load('jwt');
-    if (jwt) {
-      this.setClaims(jwt);
+    if (this.auth) {
+      this.setClaims(this.auth);
     } else {
       // setTheme();
       console.log('No JWT cookie found.');
@@ -30,17 +29,17 @@ export class UserService {
   }
 
   public login(res: LoginResponse) {
-    this.setClaims(res.jwt);
     let expires = new Date();
     expires.setDate(expires.getDate() + 365);
-    IsomorphicCookie.save('jwt', res.jwt, { expires });
+    IsomorphicCookie.save('jwt', res.jwt, { expires, secure: false });
     console.log('jwt cookie set');
+    this.setClaims(res.jwt);
   }
 
   public logout() {
+    IsomorphicCookie.remove('jwt', { secure: false });
     this.claims = undefined;
     this.user = undefined;
-    IsomorphicCookie.remove('jwt');
     // setTheme();
     this.jwtSub.next();
     console.log('Logged out.');
index c8c8d935d3b123acf8aea5a7a2e2661f32170e47..5214f07d55e4be50bc77706af768afdb8acadbe9 100644 (file)
@@ -358,15 +358,14 @@ export function debounce(
 
 // TODO
 export function getLanguage(override?: string): string {
-  return 'en';
-  // let user = UserService.Instance.user;
-  // let lang = override || (user && user.lang ? user.lang : 'browser');
+  let user = UserService.Instance.user;
+  let lang = override || (user && user.lang ? user.lang : 'browser');
 
-  // if (lang == 'browser') {
-  //   return getBrowserLanguage();
-  // } else {
-  //   return lang;
-  // }
+  if (lang == 'browser' && isBrowser()) {
+    return getBrowserLanguage();
+  } else {
+    return lang;
+  }
 }
 
 // TODO
@@ -440,6 +439,11 @@ export function getMomentLanguage(): string {
 
 // TODO
 export function setTheme(theme: string = 'darkly', loggedIn: boolean = false) {
+  // require('../assets/css/themes/_variables.darkly.scss');
+  // require('../../node_modules/bootstrap/scss/bootstrap.scss');
+  // require('../assets/css/themes/_variables.darkly.scss');
+  // require('../assets/css/themes/_variables.darkly.scss');
+  // require('../assets/css/themes/_variables.darkly.scss');
   // unload all the other themes
   // for (var i = 0; i < themes.length; i++) {
   //   let styleSheet = document.getElementById(themes[i]);
@@ -1138,11 +1142,10 @@ export function setAuth(obj: any, auth: string) {
 }
 
 export function setIsoData(context: any): IsoData {
-  if (isBrowser()) {
-    return window.isoData;
-  } else {
-    return context.router.staticContext;
-  }
+  let isoData: IsoData = isBrowser()
+    ? window.isoData
+    : context.router.staticContext;
+  return isoData;
 }
 
 export function wsSubscribe(parseMessage: any): Subscription {