]> Untitled Git - lemmy-ui.git/commitdiff
Fix site setup and login. Fixes #699 (#702)
authorDessalines <dessalines@users.noreply.github.com>
Mon, 27 Jun 2022 20:50:47 +0000 (16:50 -0400)
committerGitHub <noreply@github.com>
Mon, 27 Jun 2022 20:50:47 +0000 (16:50 -0400)
src/shared/components/app/navbar.tsx
src/shared/components/community/community-form.tsx
src/shared/components/home/home.tsx
src/shared/components/home/login.tsx
src/shared/components/home/setup.tsx
src/shared/components/home/signup.tsx
src/shared/services/UserService.ts
src/shared/utils.ts

index 604a90a57f82b09218e43d33de656e7a0b5273f7..2408a76b84306fa76476d4e4a023090831bcafeb 100644 (file)
@@ -22,12 +22,10 @@ import {
   amAdmin,
   auth,
   donateLemmyUrl,
-  getLanguages,
   isBrowser,
   notifyComment,
   notifyPrivateMessage,
   numToSI,
-  setTheme,
   showAvatars,
   toast,
   wsClient,
@@ -90,18 +88,13 @@ export class Navbar extends Component<NavbarProps, NavbarState> {
             auth: auth().unwrap(),
           })
         );
-        this.fetchUnreads();
-      }
 
-      this.userSub = UserService.Instance.jwtSub.subscribe(res => {
-        // A login
-        if (res.isSome()) {
-          this.requestNotificationPermission();
-          WebSocketService.Instance.send(
-            wsClient.getSite({ auth: res.map(r => r.jwt) })
-          );
+        if (this.props.siteRes.site_view.isSome()) {
+          this.fetchUnreads();
         }
-      });
+      }
+
+      this.requestNotificationPermission();
 
       // Subscribe to unread count changes
       this.unreadInboxCountSub =
@@ -628,18 +621,6 @@ export class Navbar extends Component<NavbarProps, NavbarState> {
       this.state.unreadApplicationCount = data.registration_applications;
       this.setState(this.state);
       this.sendApplicationUnread();
-    } else if (op == UserOperation.GetSite) {
-      // This is only called on a successful login
-      let data = wsJsonToRes<GetSiteResponse>(msg, GetSiteResponse);
-      UserService.Instance.myUserInfo = data.my_user;
-      UserService.Instance.myUserInfo.match({
-        some: mui => {
-          setTheme(mui.local_user_view.local_user.theme);
-          i18n.changeLanguage(getLanguages()[0]);
-          this.setState(this.state);
-        },
-        none: void 0,
-      });
     } else if (op == UserOperation.CreateComment) {
       let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
 
index d1f5f7534c18f6eebeafc0806509d7d66f62d46a..cce144bf07b2de1b2f264cfe73c90af626d19a73 100644 (file)
@@ -85,7 +85,7 @@ export class CommunityForm extends Component<
           posting_restricted_to_mods: Some(
             cv.community.posting_restricted_to_mods
           ),
-          auth: auth().unwrap(),
+          auth: undefined,
         });
       },
       none: void 0,
@@ -293,6 +293,7 @@ export class CommunityForm extends Component<
     event.preventDefault();
     i.state.loading = true;
     let cForm = i.state.communityForm;
+    cForm.auth = auth().unwrap();
 
     i.props.community_view.match({
       some: cv => {
@@ -304,7 +305,7 @@ export class CommunityForm extends Component<
           banner: cForm.banner,
           nsfw: cForm.nsfw,
           posting_restricted_to_mods: cForm.posting_restricted_to_mods,
-          auth: auth().unwrap(),
+          auth: cForm.auth,
         });
 
         WebSocketService.Instance.send(wsClient.editCommunity(form));
index 1ff06a0044011d35cdb3270ba74aacb97fbf3b96..6618034216b60d410ee73c4e8ce1159e93a57208 100644 (file)
@@ -123,7 +123,10 @@ export class Home extends Component<any, HomeState> {
     listingType: getListingTypeFromProps(
       this.props,
       ListingType[
-        this.isoData.site_res.site_view.unwrap().site.default_post_listing_type
+        this.isoData.site_res.site_view.match({
+          some: type_ => type_.site.default_post_listing_type,
+          none: ListingType.Local,
+        })
       ]
     ),
     dataType: getDataTypeFromProps(this.props),
@@ -186,7 +189,7 @@ export class Home extends Component<any, HomeState> {
 
   componentDidMount() {
     // This means it hasn't been set up yet
-    if (!this.state.siteRes.site_view) {
+    if (this.state.siteRes.site_view.isNone()) {
       this.context.router.history.push("/setup");
     }
     setupTippy();
index 7383a560767bd9186777640259dab772dd0be246..753e9d8166f9ba03b4df9e4ddd87ccfd93aba2ee 100644 (file)
@@ -13,7 +13,6 @@ import { Subscription } from "rxjs";
 import { i18n } from "../../i18next";
 import { UserService, WebSocketService } from "../../services";
 import {
-  auth,
   isBrowser,
   setIsoData,
   toast,
@@ -198,13 +197,6 @@ export class Login extends Component<any, State> {
         this.state = this.emptyState;
         this.setState(this.state);
         UserService.Instance.login(data);
-        WebSocketService.Instance.send(
-          wsClient.userJoin({
-            auth: auth().unwrap(),
-          })
-        );
-        toast(i18n.t("logged_in"));
-        this.props.history.push("/");
       } else if (op == UserOperation.PasswordReset) {
         toast(i18n.t("reset_password_mail_sent"));
       } else if (op == UserOperation.GetSite) {
index f524f4ceb9d865eb46ebf026d57525a9cf78d1fb..46d225e24639db3b75077fcc73ce9c12f106f239 100644 (file)
@@ -39,7 +39,7 @@ export class Setup extends Component<any, State> {
       honeypot: None,
       answer: None,
     }),
-    doneRegisteringUser: false,
+    doneRegisteringUser: UserService.Instance.myUserInfo.isSome(),
     userLoading: false,
   };
 
@@ -206,7 +206,6 @@ export class Setup extends Component<any, State> {
     } else if (op == UserOperation.Register) {
       let data = wsJsonToRes<LoginResponse>(msg, LoginResponse);
       this.state.userLoading = false;
-      this.state.doneRegisteringUser = true;
       UserService.Instance.login(data);
       this.setState(this.state);
     } else if (op == UserOperation.CreateSite) {
index 31d1e91eb6238da885a2c8502ed96a763a1a65cb..9e6e3c7569e111843dec32d76f9821688e4c8c19 100644 (file)
@@ -19,7 +19,6 @@ import { Subscription } from "rxjs";
 import { i18n } from "../../i18next";
 import { UserService, WebSocketService } from "../../services";
 import {
-  auth,
   isBrowser,
   joinLemmyUrl,
   mdToHtml,
@@ -537,13 +536,8 @@ export class Signup extends Component<any, State> {
         this.state = this.emptyState;
         this.setState(this.state);
         // Only log them in if a jwt was set
-        if (data.jwt) {
+        if (data.jwt.isSome()) {
           UserService.Instance.login(data);
-          WebSocketService.Instance.send(
-            wsClient.userJoin({
-              auth: auth().unwrap(),
-            })
-          );
           this.props.history.push("/communities");
         } else {
           if (data.verify_email_sent) {
index 678d1129026b23310dfa5a18f8cefca8be6407ed..8d591cc3ed3c0ffa2466b878683a7c374c227dbd 100644 (file)
@@ -3,7 +3,7 @@ import { Err, None, Ok, Option, Result, Some } from "@sniptt/monads";
 import IsomorphicCookie from "isomorphic-cookie";
 import jwt_decode from "jwt-decode";
 import { LoginResponse, MyUserInfo } from "lemmy-js-client";
-import { BehaviorSubject, Subject } from "rxjs";
+import { BehaviorSubject } from "rxjs";
 import { isHttps } from "../env";
 import { i18n } from "../i18next";
 import { isBrowser, toast } from "../utils";
@@ -23,7 +23,6 @@ export class UserService {
   private static _instance: UserService;
   public myUserInfo: Option<MyUserInfo> = None;
   public jwtInfo: Option<JwtInfo> = None;
-  public jwtSub: Subject<Option<JwtInfo>> = new Subject<Option<JwtInfo>>();
   public unreadInboxCountSub: BehaviorSubject<number> =
     new BehaviorSubject<number>(0);
   public unreadReportCountSub: BehaviorSubject<number> =
@@ -38,19 +37,23 @@ export class UserService {
   public login(res: LoginResponse) {
     let expires = new Date();
     expires.setDate(expires.getDate() + 365);
-    IsomorphicCookie.save("jwt", res.jwt, { expires, secure: isHttps });
-    console.log("jwt cookie set");
-    this.setJwtInfo();
+    res.jwt.match({
+      some: jwt => {
+        toast(i18n.t("logged_in"));
+        IsomorphicCookie.save("jwt", jwt, { expires, secure: isHttps });
+        this.setJwtInfo();
+        location.reload();
+      },
+      none: void 0,
+    });
   }
 
   public logout() {
     this.jwtInfo = None;
     this.myUserInfo = None;
-    this.jwtSub.next(this.jwtInfo);
     IsomorphicCookie.remove("jwt"); // TODO is sometimes unreliable for some reason
     document.cookie = "jwt=; Max-Age=0; path=/; domain=" + location.host;
-    location.reload(); // TODO may not be necessary anymore
-    console.log("Logged out.");
+    location.reload();
   }
 
   public auth(throwErr = true): Result<string, string> {
@@ -74,7 +77,6 @@ export class UserService {
     if (jwt) {
       let jwtInfo: JwtInfo = { jwt, claims: jwt_decode(jwt) };
       this.jwtInfo = Some(jwtInfo);
-      this.jwtSub.next(this.jwtInfo);
     }
   }
 
index 222190abdfdb212e579e232f4a29895770fef03f..1b632e3949428625f7be49b24f320a7e2002543b 100644 (file)
@@ -1221,10 +1221,11 @@ export function setIsoData<Type1, Type2, Type3, Type4, Type5>(
     if (routeData[4]) {
       routeDataOut[4] = convertWindowJson(cls5, routeData[4]);
     }
+    let site_res = convertWindowJson(GetSiteResponse, json.site_res);
 
     let isoData: IsoData = {
       path: json.path,
-      site_res: convertWindowJson(GetSiteResponse, json.site_res),
+      site_res,
       routeData: routeDataOut,
     };
     return isoData;