From: Dessalines Date: Mon, 27 Jun 2022 20:50:47 +0000 (-0400) Subject: Fix site setup and login. Fixes #699 (#702) X-Git-Url: http://these/git/%7B%60/css/themes/static/%7Bs.link%7D?a=commitdiff_plain;h=130748a0c38beb4cd07102ab05129fbc73c1d23f;p=lemmy-ui.git Fix site setup and login. Fixes #699 (#702) --- diff --git a/src/shared/components/app/navbar.tsx b/src/shared/components/app/navbar.tsx index 604a90a..2408a76 100644 --- a/src/shared/components/app/navbar.tsx +++ b/src/shared/components/app/navbar.tsx @@ -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 { 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 { 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(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(msg, CommentResponse); diff --git a/src/shared/components/community/community-form.tsx b/src/shared/components/community/community-form.tsx index d1f5f75..cce144b 100644 --- a/src/shared/components/community/community-form.tsx +++ b/src/shared/components/community/community-form.tsx @@ -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)); diff --git a/src/shared/components/home/home.tsx b/src/shared/components/home/home.tsx index 1ff06a0..6618034 100644 --- a/src/shared/components/home/home.tsx +++ b/src/shared/components/home/home.tsx @@ -123,7 +123,10 @@ export class Home extends Component { 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 { 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(); diff --git a/src/shared/components/home/login.tsx b/src/shared/components/home/login.tsx index 7383a56..753e9d8 100644 --- a/src/shared/components/home/login.tsx +++ b/src/shared/components/home/login.tsx @@ -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 { 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) { diff --git a/src/shared/components/home/setup.tsx b/src/shared/components/home/setup.tsx index f524f4c..46d225e 100644 --- a/src/shared/components/home/setup.tsx +++ b/src/shared/components/home/setup.tsx @@ -39,7 +39,7 @@ export class Setup extends Component { honeypot: None, answer: None, }), - doneRegisteringUser: false, + doneRegisteringUser: UserService.Instance.myUserInfo.isSome(), userLoading: false, }; @@ -206,7 +206,6 @@ export class Setup extends Component { } else if (op == UserOperation.Register) { let data = wsJsonToRes(msg, LoginResponse); this.state.userLoading = false; - this.state.doneRegisteringUser = true; UserService.Instance.login(data); this.setState(this.state); } else if (op == UserOperation.CreateSite) { diff --git a/src/shared/components/home/signup.tsx b/src/shared/components/home/signup.tsx index 31d1e91..9e6e3c7 100644 --- a/src/shared/components/home/signup.tsx +++ b/src/shared/components/home/signup.tsx @@ -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 { 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) { diff --git a/src/shared/services/UserService.ts b/src/shared/services/UserService.ts index 678d112..8d591cc 100644 --- a/src/shared/services/UserService.ts +++ b/src/shared/services/UserService.ts @@ -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 = None; public jwtInfo: Option = None; - public jwtSub: Subject> = new Subject>(); public unreadInboxCountSub: BehaviorSubject = new BehaviorSubject(0); public unreadReportCountSub: BehaviorSubject = @@ -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 { @@ -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); } } diff --git a/src/shared/utils.ts b/src/shared/utils.ts index 222190a..1b632e3 100644 --- a/src/shared/utils.ts +++ b/src/shared/utils.ts @@ -1221,10 +1221,11 @@ export function setIsoData( 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;